5

I have a project where I have 4 environments (dev, test, staging and prod) and we have branches for each (develop, test, staging master respectively). We use npm version to bump version in package.json but also add a git tag. After that we run the build and on success of that, we push the commit and tag created by the npm version command. So in my pipeline job, I have this (simplified):

dev build:
  stage: build
  only:
   - develop@username/reponame
  script:
   - npm version patch -m "[ci skip] %s"
   - git add -A
   - echo "Do build here before pushing the version bump"
   - git push git@my-repo-url:$CI_PROJECT_PATH.git HEAD:develop --follow-tags

Notice with the npm version, I also specify a message for the git commit so I can add the “[ci skip]” which is how we stop the infinite loop but then we have pipeline runs listed as skipped under the status column. Not the worst thing in the world but wanted to see if there is a better way to do this sort of thing? Have a version git commit and tag pushed to the repo without triggering another pipeline run.

3 Answers 3

5

After talking with a colleague (thanks Lucas Still), he had the idea and pointed it out in Gitlab's documentation to check variables for what user is pushing. This was a great idea since I already had a bot gitlab user that does the git push so all I had to do is have an except and check if the user is that bot account:

dev build:
  stage: build
  except:
    variables:
      - $GITLAB_USER_LOGIN == "my-bot"
  only:
    - develop@username/reponame
  script:
    - npm version patch
    - echo "Do build here before pushing the version bump"
    - git push git@my-repo-url:$CI_PROJECT_PATH.git HEAD:$CI_COMMIT_REF_NAME --follow-tags

So the only thing that is important here is to change "my-bot" to be the username of the bot account. Could use $GITLAB_USER_ID or even $GITLAB_USER_EMAIL also but the user name is more descriptive to other people that come across the yml file.

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

1 Comment

Thanks for the answer, I got into the same problem recently. Though, it is not clear to me how you are able to make this git push from your Bot GitLab user (I'm quite new to GitLab). As far as I can get with the snippet above, I cannot understand how you configure this. Could you please add more details?
2

I ran into the same problem and came up with a neat solution that uses Gitlab variables (as pointed out by Mitchell) but does not need to have a git bot (though you'll need credentials to git push, maybe your own).

Basically I check the variable CI_COMMIT_MESSAGE, particularly if it contains a predefined keyword(s) to not run the CI pipeline if there is a match. So the yml file looks something like:

ci_runner_job: # I want this job to run whenever someone else pushes something
    script:
      - echo Running ci job...
      ... (your stuff, e.g. git clone, some code, git add --all) ...
      - git commit -m "optional message SOME_KEYWORD"  # this keyword will avoid the loop
      - git push (your repo info)
    only:
      variables:
        - $CI_COMMIT_MESSAGE !~ /SOME_KEYWORD/

some_other_job:
    script:
      - echo This job is running because SOME_KEYWORD was found in the commit message...
      ... your stuff here, probably something without git push ...
    only:
      variables:
        - $CI_COMMIT_MESSAGE =~ /SOME_KEYWORD/

Two remarks:

  1. This also allows for remote users to avoid the CI pipeline to run, if they include the SOME_KEYWORD in their commit message
  2. The SOME_KEYWORD can be potentially stored in a Gitlab variable, therefore be modified easily in the future.

1 Comment

Note that (at least today) GitLab already suppresses pipeline runs when [skip ci] or [ci skip] is present in a commit message. There's also a git push option available: docs.gitlab.com/ee/ci/pipelines/#skip-a-pipeline
1

https://semantic-release.gitbook.io/semantic-release/ uses a workflow that bumps version in package.json based on git tags and doesn't require a commit.

Comments

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.