简体   繁体   English

Gitlab CI/CD 自动标记发布

[英]Gitlab CI/CD auto tagging release

I'm trying to have my GitLab pipeline automatically tag the master branch but with no luck.我试图让我的 GitLab 管道自动标记master分支,但没有运气。

What I want to do我想做的事

Since the project is a composer package, what I want to do is get the version number from the composer.json file, store it in a variable, and then use this variable with git to tag the branch.由于项目是composer package,所以我要做的是从composer.json文件中获取版本号,将其存储在一个变量中,然后将此变量与ZBA9F11ECC3497D9993B933FDC2BD61EZ一起使用来标记分支。

What I'm doing我在做什么

Here is the pipeline job part from my .gitlab-ci.yml :这是我的.gitlab-ci.yml中的管道作业部分:

tagging:
  stage: publish
  image: alpine
  only:
    - master
  script:
    - version=$(cat composer.json | grep version | grep -Eo "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+")
    - git tag "$(version)"
    - git push --tags

The error错误

I can't actually tell what the problem is since no output is displayed in the GitLab job output as show in the picture below我实际上无法说出问题所在,因为在 GitLab 作业 output 中没有显示 output,如下图所示

在此处输入图像描述

Ok, didn't know exactly why this didn't work, but i found out i wrote Version (with the capital V) in the first grep command instead of version : this shouldn't be the cause of the problem since in local the same command pipeline return a 0 but not an error.好的,不知道为什么这不起作用,但我发现我在第一个 grep 命令中写了Version (大写 V)而不是version :这不应该是问题的原因,因为在本地相同的命令管道返回 0 但不是错误。

I prefer to not install additional cli commands on the pipeline job image as @davide-madrisan suggested since I wanted to keep it as simple as possible.我不想像@davide-madrisan 建议的那样在管道作业映像上安装额外的 cli 命令,因为我想让它尽可能简单。

Tips and tricks技巧和窍门

Moreover i found this interesting gitlab repo with exactly what I needed:此外,我发现这个有趣的 gitlab repo 正是我所需要的:
https://gitlab.com/guided-explorations/gitlab-ci-yml-tips-tricks-and-hacks/commit-to-repos-during-ci/commit-to-repos-during-ci https://gitlab.com/guided-explorations/gitlab-ci-yml-tips-tricks-and-hacks/commit-to-repos-during-ci/commit-to-repos-during-ci

The result结果

So in the end I came up with this pipeline job:所以最后我想出了这个管道工作:

tagging:
  stage: publish
  only:
    - master
  script:
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - tag=$(cat composer.json | grep version | grep -Eo "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+")
    - git tag "$tag"
    - git push --tags http://root:$ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git HEAD:master

I just needed to create a personal access token and add tree pipeline variables with the git creadentials to create the tag and push it to the master branch from within the pipeline, but it actually work now.我只需要创建一个个人访问令牌并使用 git 凭据添加树管道变量来创建标签并将其从管道内推送到主分支,但它现在实际上可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM