简体   繁体   中英

How can I check in interval loop if the tags exist before doing the `git checkout $tag` command?

In a bash script, I am waiting for a particular remote git tag to be released before using it in my script.

How can I check in interval loop if the tags exist before doing the git checkout $tag command?

Ex:

 while sleep 3; do git fetch && git rev-parse --verify <tag> && break; done

Edit

I have created the following bash functions:


function wait_for_tag() {
  tag=v${1#v}
  interval=${2:-20}
    while :; do

        echo "Waiting for tag ${tag}..."
        git remote update > /dev/null 2>&1
        git rev-parse --verify --quiet "${tag}" && break
        sleep ${interval}
    done
}

function git_checkout() {
  tag=v${1#v}
    is_release && wait_for_tag "${tag}"
    git checkout ${tag} || echo "testing"
    npm install
}

I expect when I do git_checkout v2.0.13-bs-redux-saga-router-dom-intl to checkout the tag if it is already existing, otherwise, to fetch for new tags and try again later.

This seems to work nice in a local environment, but when I do that in Gitlab-CI, the parallel pipelines are never seeing the new tag, even if they are tagged and existing on Gitlab UI.

How can I ensure that the wait_for_tag function does really retrieve tags and why does git remote update fail to do that?

You can use git rev-parse to check if the tag exists. Something like this:

while :; do
  git remote update
  git rev-parse --verify --quiet SomeInterestingTag && break

  # the tag did not exist
  sleep 10
done

You can use git rev-parse to check weather the tag exists or not:

git rev-parse -q --verify "refs/tags/$tag" >/dev/null

In a loop you've mentioned and in combination with git checkout it could look like this:

tag="foo"
while true; do
    git fetch --all
    if git rev-parse -q --verify "refs/tags/$tag" >/dev/null; then
        git checkout "$tag" && break
    fi
    sleep 5
done

In this case you really have to pass a tag name as $tag - no commit hash or branch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM