使用Gitlab管道和git push防止无限循环 [英] Prevent infinite loop with Gitlab pipeline and git pushing

查看:201
本文介绍了使用Gitlab管道和git push防止无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中有4个环境(开发,测试,登台和正式版),每个都有分支(分别是开发,测试和登台母版).我们使用npm version来更改package.json中的版本,但还要添加一个git标签.之后,我们运行构建并成功完成构建,然后推送由npm version命令创建的提交和标记.所以在我的管道工作中,我有这个(简体):

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

关于npm version的通知,我还为git commit指定了一条消息,因此我可以添加"[ci skip]" ,这是我们停止无限循环的方式,但随后我们有了管道在状态"列下跳过列出的运行.不是世界上最糟糕的事情,而是想看看是否有更好的方法来做这种事情?在不触发另一个管道运行的情况下,将版本git commit和tag推送到仓库中.

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.

推荐答案

与同事交谈(感谢Lucas Still)之后,他有了这个想法,并在Gitlab的文档中指出了该想法,以检查用户所推动的变量.这是一个好主意,因为我已经有一个执行git push的bot gitlab用户,所以我要做的就是拥有一个except并检查该用户是否是该bot帐户:

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

因此,这里唯一重要的事情是将"my-bot"更改为机器人帐户的用户名.也可以使用$GITLAB_USER_ID甚至$GITLAB_USER_EMAIL,但是用户名对yml文件中的其他人更具描述性.

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.

这篇关于使用Gitlab管道和git push防止无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆