【问题标题】:How to version or tag incrementally in Gitlab CI/CD when merging from Production to Master branch从生产分支合并到主分支时如何在 Gitlab CI/CD 中增量版本或标记
【发布时间】:2020-12-17 09:14:27
【问题描述】:

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

【问题讨论】:

    标签: git gitlab versioning git-tag gitversion


    【解决方案1】:

    对于未来的任何人:

    This offers an approach to achieve that

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

    【讨论】:

      【解决方案2】:

      您可以将其用于此类目的——semantic release 工具。它通过提交前缀自动检测要增加的版本(主要、次要、补丁)。它不仅可以更新 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"(将增加路径版本)

      完整的工作示例可用here on gitlab

      【讨论】:

      • 谢谢@ujlbu。使用其他语言的其他服务怎么样?
      • @ILoveCode 此解决方案与语言无关(不依赖于服务语言)。您只需要配置文件、适当的 git 提交前缀和命令行实用程序语义发布
      • @ujlbu4,是否可以使用像詹金斯作业这样的增量构建号创建 gitlab 管道构建,如果是请告诉我
      • @Pradeepkumar 获取增量内部版本号,请查看Predefined environment variables,如CI_PIPELINE_IDCI_JOB_ID。顺便说一句,你不需要这篇文章中的语义发布工具来使用预定义的环境变量
      猜你喜欢
      • 2021-12-24
      • 1970-01-01
      • 2017-10-11
      • 2022-01-18
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      相关资源
      最近更新 更多