2

I'm trying to call shell command in .gitlab-ci.yml, whose relevant parts are:

image: docker:latest
services:
  - docker:dind

stages:
  - build
  - deploy

...

build:
  stage: build
  script:
    - apt-get update -y
    - GIT_TAG=$(git tag | tail -1)
    - GIT_TAG=$(/usr/bin/git tag | tail -1)
    - docker ...

However all top three shell command callings have failed, all with "command not found" error. The git command being failing is really odd, because it has to get the git repo first before start the script section. I.e., I can see that git is working, but I just can't use it myself.

Is there any way to make it working?

6
  • 1
    Are you sure docker:latest has git installed? Try adding apt-get install -y git. Commented May 20, 2019 at 20:23
  • 1
    Turns out docker:latest is based on alpine:3.9 so you need apk add --no-cache git. Commented May 20, 2019 at 20:43
  • As you're using GitLab, there are special variables available to get the branch/tag/user/commit/etc details already available. Use those instead of installing Git! Commented May 25, 2019 at 3:32
  • @rebelution, if you meant $CI_COMMIT_TAG then please see my earlier comment that I've tried and confirmed that it is not available for my specific case in OP. Commented May 26, 2019 at 0:05
  • @xpt - got it. are you creating a tag and experiencing this behavior? This is what I found in the documentation of GitLab "CI_COMMIT_TAG - The commit tag name. Present only when building tags" means - when you will push a commit to GitLab, then it will start a pipeline without CI_BUILD_TAG variable. When you make a tag on this commit and push this tag to GitLab, then another pipeline (this time for the tag, not for the commit) will be started. In that case CI_BUILD_TAG will be present. Commented May 27, 2019 at 6:13

2 Answers 2

2

You see git working in separate steps because GitLab is probably doing it in another container. They keep your container clean, so you have to install dependencies yourself.

Since the image you're using is based on Alpine Linux, the command to install git is:

apk add --no-cache git

You can also skip the whole thing and use the predefined environment variables if all you need is git information. $CI_COMMIT_TAG will contain the tag and $CI_COMMIT_SHA will contain the commit hash.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you elaborate on the $CI_COMMIT_TAG in the answer please? The doc says it is "commit tag name. Present only when building tags." Am I building tags in above .gitlab-ci.yml step? Would I be able to use it in my step if not? Moreover, the doc doesn't say it is the latest tag, while I have lots of them. Which on will be $CI_COMMIT_TAG? Thx!
I'm not familiar enough with GitLab, but usually CI systems build on every commit and new tag. When it's building a tag, that variable will have that tag name. That's usually what people need the tag for in CI, so they can mark the version with the tag name.
Thanks for the explanation, kichik. I've tried and confirmed that, for my specific case in OP, the $CI_COMMIT_TAG is empty.
0

from the documentation of GitLab, here is the definition of CI_COMMIT_TAG:

CI_COMMIT_TAG - The commit tag name. Present only when building tags

means - when you will push a commit to GitLab, then it will start a pipeline without CI_COMMIT_TAG variable. When you make a tag on this commit and push this tag to GitLab, then another pipeline (this time for the tag, not for the commit) will be started. In that case CI_COMMIT_TAG will be present.

@xpt - thanks for the vote confidence and asking to write up this as an answer, hope this helps the community!

3 Comments

thanks, let me try it out and get back to you. So you are saying that by using CI_COMMIT_REF_NAME, the above .gitlab-ci.yml in my OP will work without any more modifications, right?
I tried it just now, and it doesn't work, unless .gitlab-ci.yml in OP is modified -- CI_COMMIT_REF_NAME always gives me my project name, never the tag name. Please make sure it works at your end before post any answer, not just you think it'd work. thx.
@xpt - thanks, edited the answer, will test it out and share if I find anything around it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.