使用Gitlab管道和git推送防止无限循环
我有一个项目,其中我有4个环境(dev、test、staging和prod),每个环境都有分支(develope、test、staging master)。我们使用使用Gitlab管道和git推送防止无限循环,git,gitlab,pipeline,gitlab-ci,Git,Gitlab,Pipeline,Gitlab Ci,我有一个项目,其中我有4个环境(dev、test、staging和prod),每个环境都有分支(develope、test、staging master)。我们使用npm version在package.json中添加一个git标记。之后,我们运行构建,并在构建成功后,推送由npm version命令创建的commit和标记。因此,在我的管道工作中,我有一个(简化的): 请注意,对于npm版本,我还为git提交指定了一条消息,这样我就可以添加“[ci skip]”,这就是我们停止无限循环的方式,
npm version
在package.json
中添加一个git标记。之后,我们运行构建,并在构建成功后,推送由npm version
命令创建的commit和标记。因此,在我的管道工作中,我有一个(简化的):
请注意,对于
npm版本
,我还为git提交指定了一条消息,这样我就可以添加“[ci skip]”,这就是我们停止无限循环的方式,但是在status列下,管道运行被列为跳过。这不是世界上最糟糕的事情,但想看看是否有更好的方法来做这类事情?在不触发另一个管道运行的情况下,将git commit和tag版本推送到repo。在与同事交谈后(仍然感谢Lucas),他有了这个想法,并在Gitlab的文档中指出了这个想法,以检查用户推送的变量。这是一个好主意,因为我已经有了一个bot-gitlab用户来执行git推送
,所以我所要做的就是拥有一个,除了
,然后检查用户是否是该bot帐户:
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
因此,这里唯一重要的是将
“我的机器人”
更改为机器人帐户的用户名。也可以使用$GITLAB\u USER\u ID
或者甚至$GITLAB\u USER\u EMAIL
,但是用户名对于遇到yml文件的其他人来说更具描述性。使用基于git标记的package.json中的版本的工作流,并且不需要提交。我遇到了同样的问题,并想出了一个简洁的解决方案使用Gitlab变量(正如Mitchell所指出的),但不需要GitBot(尽管您需要GitPush的凭据,可能是您自己的)
基本上,我检查变量CI_COMMIT_消息,特别是如果它包含预定义的关键字,以便在存在匹配项时不运行CI管道。因此,yml文件看起来像:
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/
两点意见:
谢谢你的回答,我最近也遇到了同样的问题。尽管如此,我不清楚如何从Bot-GitLab用户(我对GitLab还很陌生)那里实现这个
git-push
。就我对上述代码片段的理解而言,我无法理解您是如何配置它的。你能补充更多细节吗?
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/