Prevent infinite loop with Gitlab pipeline and git pushing

时光总嘲笑我的痴心妄想 提交于 2020-08-08 05:43:10

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/50955931/prevent-infinite-loop-with-gitlab-pipeline-and-git-pushing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!