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

标签 git gitlab versioning git-tag gitversion

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

最佳答案

您可以用于此类目的 — semantic release工具。它通过提交前缀自动检测要增加的版本(主要、次要、补丁)。它不仅可以更新 gitlab 标签,还可以发送 slack 通知、更新版本文件或具有任何自定义逻辑
示例设置看起来像这样(完整的示例链接将在答案的末尾)

  • .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:.+$/
    
  • .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
    
  • 要测试它,请尝试进行调试提交:$ git commit --allow-empty -m "fix: fake release" (将提升路径版本)

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

    关于git - 从生产分支 merge 到主分支时如何在 Gitlab CI/CD 中增量版本或标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63633737/

    相关文章:

    macos - GIT - 如何在 OSX 上不使用鼠标复制 SHA?

    kubernetes - Gitlab CI-创建集群,运行Pod,在Pod中运行应用程序,运行测试,然后删除集群

    kubernetes - 证书对 ingress.local 有效,对 gitlab.mydomain 无效

    svn - 版本号名称规范的常用或类别是什么?

    versioning - 如何将页面版本从一个 CQ 实例迁移到另一个?

    versioning - 最佳实践 : Software Versioning

    git - 使用 url 链接到 Starteam 中的特定 CR 编号

    git - 如何直接将 github pull request merge 到我的私有(private)仓库?

    git - 为什么 git 标记一个 blob 或一棵树(或一个标签)?

    javascript - 通过 Gitlab 部署到 Firebase 来自动化 vue.js 应用程序