简体   繁体   English

在 Gitlab CI/CD 管道上获取最新的 git 标签

[英]Get latest git tags on Gitlab CI/CD pipeline

I want to set up a CI/CD pipelines in Gitlab that can read the latest tag and get that last tag to increment my next version application.我想在 Gitlab 中设置一个 CI/CD 管道,它可以读取最新标签并获取最后一个标签以增加我的下一个版本应用程序。 I came with this configuration:我带来了这个配置:

stages:
  - version

calculate_version:
  image:
    name: alpine/git:latest
    entrypoint: [""]
  stage: version
  script:
    - VERSION=$(git tag);test -z "$VERSION" && echo "no version tag found" && exit 1
    - CMDLINE="$VERSION";
    - echo $VERSION
    - echo $CMDLINE > cmdline
  artifacts:
    paths:
      - cmdline

But i get no tags listed there on $VERSION.但是我没有在 $VERSION 上列出任何标签。 Its look like Gitlab not passing the tags on the repository.它看起来像 Gitlab 没有传递存储库上的标签。 However if I create and push a new tag, it shows just that new tag, not all tag list I expected.但是,如果我创建并推送一个新标签,它只会显示那个新标签,而不是我期望的所有标签列表。

Is this the behaviour of the GitLab ci/cd?这是 GitLab ci/cd 的行为吗? If yes, how can I get all of the tags in my repo inside the pipeline?如果是,我怎样才能在管道中获取我的仓库中的所有标签?

You can obtain tags using Gitlab API您可以使用Gitlab API获取标签

By default, result are ordered by the last updated tags, so if you want to get the last one, you can modify your script block like this :默认情况下,结果按最后更新的标签排序,所以如果你想得到最后一个,你可以像这样修改你的脚本块:

  script:
    - VERSION=$(curl -Ss --request GET --header "PRIVATE-TOKEN: <REPLACE_BY_A_VARIABLE>" "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/repository/tags" | jq -r '.[0] | .name')
    - test -z "$VERSION" && echo "no version tag found" && exit 1

You can do this very easily without long script code.您可以很容易地做到这一点,而无需长的脚本代码。

 script: - git fetch --tags - git tag --list | sort -V | tail -n1

assign in variable ${VER} with your version:在变量 ${VER} 中分配您的版本:

 script: - VER=$(git tag --list | sort -V | tail -n1)

Without GitLab API, you can just use:没有 GitLab API,你可以只使用:

script: 
  git describe --abbrev=0 2>/dev/null || echo ''

assign in variable to compare with your version:分配变量以与您的版本进行比较:

script: 
  last_v=$(git describe --abbrev=0 2>/dev/null || echo '')
  

Return last tag or empty string if no tag found (using 2>/dev/null to hide error code)如果没有找到标签,则返回最后一个标签或空字符串(使用2>/dev/null隐藏错误代码)

⚠️ Warning: ⚠️警告:

After test, leaving Tag message blank creates a lightweight tag, and command above will not work.测试后,将 Tag 消息留空会创建一个轻量级标签,上面的命令将不起作用。

Unfortunately, git is not available in the docker:dind image.不幸的是, gitdocker:dind图像中不可用。

I therefore like to keep versions in a file .version , next to the git tag.因此,我喜欢将版本保存在.version文件中,位于 git 标签旁边。

eg:例如:

__version__ = '0.8.1'

This allows to read the version using simple cat in Gitlab CI:这允许使用 Gitlab CI 中的简单cat读取版本:

image: docker:dind

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: "/certs"

services:
  - docker:dind

build and push latest:
  stage: build
  script:
    # get latest version from file, so we do not need to install git
    - VERSION=v$(cat .version | cut -d"'" -f 2)
    - docker login --username gitlab-ci-token --password $CI_JOB_TOKEN $CI_REGISTRY
    - docker build --pull --tag $CI_REGISTRY_IMAGE:$VERSION .
    - docker push $CI_REGISTRY_IMAGE:$VERSION
  only:
    - master

Note that the specific syntax I use in the .version file is associated with python-semantic-release , which allows to automate semantic versioning.请注意,我在.version文件中使用的特定语法与python-semantic-release相关联,它允许自动化语义版本控制。 The above would work with or without semantic-release.以上将在有或没有语义释放的情况下工作。 Semantic-release simply helps to maintain versions in .version file and git tags.语义发布只是帮助维护.version文件和 git 标签中的版本。

The corresponding setup.cfg looks as follows:对应的setup.cfg如下所示:

[semantic_release]
version_variable: .version:__version__
upload_to_pypi: false
branch: master
hvcs: gitlab
build_command: echo Skipping wheel build

Semantic-release can be used locally (run semantic-release publish ), or integrated into the CI.语义发布可以在本地使用(运行semantic-release publish ),或者集成到 CI 中。 It also does not matter whether the project is Python or another language.该项目是 Python 还是其他语言也没有关系。

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

相关问题 当 gitlab CI/CD 触发时,如何通过 git 而不是从预定义的变量获取提交标签 - How to get the commit TAGS, via git and not from predefined vars, when gitlab CI/CD triggers GitLab CI/CD 管道 - 在 GitLab Runner 上交互式运行 Linux 命令 - GitLab CI/CD Pipeline - Run Linux commands interactively on GitLab Runner 无法访问 gitlab CI/CD 管道中的任何变量 - Unable to access any variables in gitlab CI/CD pipeline Gitlab CI/CD:仅计划特定作业并将其从管道中排除 - Gitlab CI/CD: Schedule only for a particular job and exclude it from the pipeline Gitlab CI/CD 管道已通过,但未对服务器应用任何更改 - Gitlab CI/CD pipeline passed, but no changes were applied to the server 如何在 gitlab CI/CD 管道中启动新的 EC2 实例 - How to spin up a new EC2 instance within a gitlab CI/CD pipeline 使用 Bash 和 cURL 通过 CI/CD 管道进入时生成 GitLab 合并请求 - Generate GitLab Merge Request when a push comes in via CI/CD Pipeline using Bash and cURL Gitlab CI管道解压问题 - Unzip problems in Gitlab CI pipeline Gitlab CI/CD 自动标记发布 - Gitlab CI/CD auto tagging release 如何在 GitLab CI/CD 中集成 terraform - How to integrate terraform in GitLab CI/CD
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM