从生产分支合并到主分支时如何在 Gitlab CI/CD 中增量版本或标记

问题描述 投票:0回答:4

我正在开发一个项目,我想标记或提供版本号。当合并发生并且我的 CI/CD 成功运行时,我希望 gitlab 在我的 gitci.yml 文件中标记 V 1.0、1.1 等。

git gitlab versioning git-tag gitversion
4个回答
10
投票

您可以使用语义发布工具来实现此类目的。它会自动检测要通过提交前缀增加的版本(主要版本、次要版本、补丁版本)。它不仅可以更新 gitlab 标签,还可以发送 slack 通知、更新版本文件或具有任何自定义逻辑

示例设置将如下所示(完整的示例链接将位于答案末尾)

  1. .gitlab-ci.yml
    文件
Build Release:
  image: node:dubnium
  stage: build release
  script:
    - npm i semantic-release @semantic-release/changelog @semantic-release/commit-analyzer @semantic-release/gitlab @semantic-release/git @semantic-release/npm @semantic-release/release-notes-generator semantic-release-slack-bot
    - npx semantic-release
  only:
    - master
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_TITLE =~ /^RELEASE:.+$/
  1. .releaserc.yaml
    文件(与 .gitlab-ci.yml 处于同一级别)
branches: ['master']
ci: true
debug: true
dryRun: false
tagFormat: '${version}'

# Global plugin options (will be passed to all plugins)
preset: 'conventionalcommits'
gitlabUrl: 'http://gitlab.mycomany.com/' # your gitlab url
slackWebhook: 'https://slack.xxx.com/hooks/q3dtkec6yjyg9x6616o3atgkkr' # if you need slack notifies

# Responsible for verifying conditions necessary to proceed with the release:
# configuration is correct, authentication token are valid, etc...
verifyConditions:
  - '@semantic-release/changelog'
  - '@semantic-release/git'
  - '@semantic-release/gitlab'
  - 'semantic-release-slack-bot'

# Responsible for determining the type of the next release (major, minor or patch).
# If multiple plugins with a analyzeCommits step are defined, the release type will be
# the highest one among plugins output.
# Look details at: https://github.com/semantic-release/commit-analyzer#configuration
analyzeCommits:
  - path: '@semantic-release/commit-analyzer'

# Responsible for generating the content of the release note.
# If multiple plugins with a generateNotes step are defined,
# the release notes will be the result of the concatenation of each plugin output.
generateNotes:
  - path: '@semantic-release/release-notes-generator'
    writerOpts:
      groupBy: 'type'
      commitGroupsSort: 'title'
      commitsSort: 'header'
    linkCompare: true
    linkReferences: true

# Responsible for preparing the release, for example creating or updating files
# such as package.json, CHANGELOG.md, documentation or compiled assets
# and pushing a commit.
prepare:
  - path: '@semantic-release/changelog'
  - path: '@semantic-release/git'
    message: 'RELEASE: ${nextRelease.version}'
    assets: ['CHANGELOG.md']

# Responsible for publishing the release.
publish:
  - path: '@semantic-release/gitlab'

success:
  - path: 'semantic-release-slack-bot'
    notifyOnSuccess: true
    markdownReleaseNotes: false

fail:
  - path: 'semantic-release-slack-bot'
    notifyOnFail: true
  1. 要测试它,请尝试进行调试提交:
    $ git commit --allow-empty -m "fix: fake release"
    (将提高路径版本)

可以在 gitlab 上找到完整的工作示例


1
投票
由于 npm 提供

version

 命令来根据语义版本控制规则更新版本,我们可以在 
gitlab-ci.yml
 中添加一个阶段来增加版本、提交、推送,然后继续 CICD 流程

示例:

stages: - unit-test - increment-version - other-steps variables: # CI_REPOSITORY_URL contains gitlab-ci-token. replace start of the string up to '@' with git@' and append a ':' before first '/' CI_REPOSITORY_URL=https://gitlab-ci-token:[email protected]/gitlab-examples/ci-debug-trace.git before_script: - apk add --no-cache npm curl openssl # unit-test or pre-increment goes here increment-version: stage: increment-version scripts: - export PUSH_REPO=$(echo "$CI_REPOSITORY_URL" | sed -e "s|.*@\(.*\)|git@\1|" -e "s|/|:/|" ) # increment PATCH, for instance - npm version patch - git add --all && git commit -m "auto-increment" - git remote set-url --push origin "${PUSH_REPO}" - git push origin dependencies: - unit-test # post-increment goes here
参考:

https://docs.npmjs.com/cli/v8/commands/npm-version


0
投票
# The release pipeline will run only if all jobs in the test pipeline are successful stages: - test - release before_script: - npm install node:10: image: node:10 stage: test script: - npm test node:12: image: node:12 stage: test script: - npm test publish: image: node:12 stage: release script: - npx semantic-release
欲了解更多信息

https://semantic-release.gitbook.io/semantic-release/recipes/ci-configurations/gitlab-ci
    

-2
投票
对于未来的任何人:

简而言之,您创建一个 CI/CD 变量 BUILD_NUMBER 并从 1 开始, 您可以在作业中使用该变量,并通过作业内的curl更新(增量)BUILD_NUMBER变量,因此需要生成ACCESS_TOKEN并将其保留为变量。

image: docker:latest stages: - init auto_increment: stage: init variables: VAR_NAME: BUILD_NUMBER TOKEN: ${CI_PIPELINE_IID_TOKEN} GITLAB_URL: "https://gitlab.com" before_script: - apk add --update curl jq script: - "VAR=$(curl -s -f --header \"PRIVATE-TOKEN: ${TOKEN}\" \"${GITLAB_URL}/api/v4/projects/${CI_PROJECT_ID}/variables/${VAR_NAME}\" | jq -r '.value' ) " - let VAR=VAR+1 - "curl -s -f --request PUT --header \"PRIVATE-TOKEN: ${TOKEN}\" \"${GITLAB_URL}/api/v4/projects/${CI_PROJECT_ID}/variables/${VAR_NAME}\" --form \"value=${VAR}\" "

这是从这里获取的

© www.soinside.com 2019 - 2024. All rights reserved.