我有一个项目,其中有4个环境(开发,测试,登台和正式版),并且每个都有分支(分别是开发,测试和登台母版)。我们使用npm version
来提高package.json
中的版本,但还要添加一个git标签。之后,我们运行构建并成功完成构建,我们推送由npm version
命令创建的提交和标记。所以在我的管道工作中,我有这个(简体):
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推送到仓库,
答案 0 :(得分:2)
与同事交谈(感谢卢卡斯·斯蒂尔)后,他有了这个主意,并在Gitlab的文档中指出了这一点,以便检查用户所推动的变量。这是一个好主意,因为我已经有一个机器人gitlab用户来执行git push
,所以我要做的就是拥有一个except
并检查该用户是否是该机器人帐户:
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文件中的其他人更具描述性。
答案 1 :(得分:0)
https://semantic-release.gitbook.io/semantic-release/使用的工作流基于git标签更改package.json中的版本,并且不需要提交。
答案 2 :(得分:0)
我遇到了同样的问题,并提出了一个巧妙的解决方案,它使用 Gitlab 变量(如 Mitchell 所指出的)但不需要有一个 git bot(尽管你需要凭据来 git push,也许你自己的).
基本上我会检查变量 CI_COMMIT_MESSAGE,特别是如果它包含一个预定义的关键字,以便在匹配时不运行 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/
两点说明: