A
A
Anton2018-11-12 10:11:22
git
Anton, 2018-11-12 10:11:22

How do you push the latest git tag to Gitlab CI?

There are 2 stages of Gitlab CI

release:
  stage: release
  image: "$build_img"
  script:
    - apt-get install -y openssh-client git
    - mkdir -p ~/.ssh
    - echo "$SSH" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - eval "$(ssh-agent -s)"
    - ssh-add ~/.ssh/id_rsa
    - ssh-keyscan gitlab >> ~/.ssh/known_hosts
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - git config user.name $PUSH_USER_NAME
    - git config user.email $PUSH_USER_EMAIL
    - git remote set-url origin $SSH_GIT_URL
    - git checkout master
    - git reset --hard origin/master
    - mvn $MAVEN_CLI_OPTS clean release:prepare -Dresume=false -DautoVersionSubmodules=true -DdryRun=false -Dmaven.test.skip=true -DskipITs -DscmCommentPrefix="Release pom [ci skip]"
  only:
    - master

build_docker_container:
  stage: dockerize
  script:
    - git status
    - git reset --hard $(git describe --abbrev=0 --tags)
    - git status
    - docker login -u "$CI_REGISTRY_USER" -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE/$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG:$(git describe --abbrev=0 --tags)" .
    - docker push "$CI_REGISTRY_IMAGE/$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG:$(git describe --abbrev=0 --tags)"

GitLab CI logs show the following
Fetching changes...
Removing assembly/target/
HEAD is now at 34ebda3 git reset --hard tag
From https://gitlab
   8d6b33c..1a4d902  master     -> origin/master
 * [new tag]         v1.0.74    -> v1.0.74
Checking out 34ebda34 as master...
Skipping Git submodules setup
Checking cache for master...
Successfully extracted cache
Downloading artifacts for build (12355)...
Downloading artifacts from coordinator... ok        id=12355 responseStatus=200 OK token=sMoApXN6
WARNING: assembly/target/: lchown assembly/target/: operation not permitted (suppressing repeats) 

$ git status
HEAD detached at 34ebda3
nothing to commit, working directory clean

$ git reset --hard $(git describe --abbrev=0 --tags)
HEAD is now at 1da6864 Release pom [ci skip]prepare release v1.0.72

$ git status
HEAD detached from 34ebda3
nothing to commit, working directory clean
$ docker login -u "$CI_REGISTRY_USER" -p $CI_JOB_TOKEN $CI_REGISTRY
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /home/gitlab-runner/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
$ docker build --pull -t "$CI_REGISTRY_IMAGE/$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG:$(git describe --abbrev=0 --tags)" .
Sending build context to Docker daemon  158.4MB

Step 1/5 : FROM docker-registry/java:8-jre
8-jre: Pulling from java
Digest: sha256:b91008e234402fc87e7889d6af1f36b6ece844c05989236d83d1f658a6f329b0
Status: Image is up to date for docker-registry/java:8-jre
 ---> e44d62cf8862
Step 2/5 : COPY XXXXXXXXX
 ---> 5f693fa6062d
Step 3/5 : WORKDIR XXXXXXXXX
 ---> Running in 351b6b37c6c1
Removing intermediate container 351b6b37c6c1
 ---> 5bf7d2fb6089
Step 4/5 : EXPOSE 8088
 ---> Running in 463d7158fb14
Removing intermediate container 463d7158fb14
 ---> 085a40b7f44a
Step 5/5 : CMD ["java", "-jar", "order.jar"]
 ---> Running in 95def8987caf
Removing intermediate container 95def8987caf
 ---> 9a7a7fdd0748
Successfully built 9a7a7fdd0748
Successfully tagged docker-registry/repository/XXXXXXXXX/XXXXXXXXX:v1.0.72

It can be seen that the docker image is v1.0.72, and the latest version is v1.0.74
How do you download the latest git tag in Gitlab CI ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2018-11-12
Patsev @chemtech

Here is the solution. Updated after correspondence with Georg

build_docker_container:
  stage: dockerize
  script:
    - git checkout master
    - git pull
    - export TAG_TO_BUILD=$(git describe --abbrev=0 --tags)
    - test -z "${TAG_TO_BUILD}" && echo "The TAG_TO_BUILD is empty" && exit 1
    - git reset --hard "${TAG_TO_BUILD}"
    - docker login -u "$CI_REGISTRY_USER" -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build --pull -t "$CI_REGISTRY_IMAGE/$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG:${TAG_TO_BUILD}" .
    - docker push "$CI_REGISTRY_IMAGE/$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG:${TAG_TO_BUILD}"

G
Georg Gaal, 2018-11-12
@gecube

I didn't quite understand the question.
The fact is that Gitlab-CI is not about that.
It creates a new pipeline for each new tag (when created via the web interface or when pushing to the repo). You can pull the tag either through the CI_COMMIT_TAG variable or CI_COMMIT_REF_NAME / CI_COMMIT_REF_SLUG.
If the tag is stored in the repository in a file, then you can pull it out of it (do you need to tell the repository head how to pull it?).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question