【问题标题】:How to automatically version npm package in Azure DevOps (without triggering new pipeline)?如何在 Azure DevOps 中自动对 npm 包进行版本控制(不触发新管道)?
【发布时间】:2021-08-20 23:45:04
【问题描述】:

我们正在尝试做的事情

我们正在使用 Azure Pipelines (azure-pipelines.yml) 来自动化 ci/cd。我们的部分配置完成了项目的版本控制,以发布到 Azure Artifacts。我们还尝试对此进行配置以更新 package.json 中的现有版本号,而不会触发 Azure DevOps 中的新管道。

这是我们azure-pipelines.yml 文件的相关部分:

  - script: |
      git config --global user.email "email@example.com"
      git config --global user.name "User name"
      npm version patch -m "Bump version to %s [skip ci]" --force
    displayName: 'Bump release version'
  - script: |
      npm pack
    displayName: 'Package package'

这很适合将包发布到我们的 Azure Artifacts 源,但不会更新 package.json 中的现有版本

我们的package.json 包含以下内容:

"release": {
    "plugins": [
        "@semantic-release/commit-analyzer",
        "@semantic-release/release-notes-generator",
        "@semantic-release/changelog",
        [
            "@semantic-release/npm",
            {
                "npmPublish": false
            }
        ],
        [
            "@semantic-release/git",
            {
                "assets": [
                    "dist/",
                    "package.json",
                    "CHANGELOG.md"
                ],
                "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
            }
        ]
    ]
}

问题

我们将如何更新脚本以确保package.json 中的version 值也在不触发另一个管道执行的情况下更新(这将导致触发新管道执行的无限循环)?

【问题讨论】:

    标签: npm azure-devops semantic-versioning


    【解决方案1】:

    您可以添加另一个脚本任务以推送回您的 devops git 存储库。但首先,您需要添加 checkout 步骤并将 persistCredentials 设置为 true 以验证 git 命令。见下例:

    - checkout: self
      persistCredentials: true
    
    - script: |
    
       git config --global user.email "email@example.com"
       git config --global user.name "User name"
       npm version patch -m "Bump version to %s [skip ci]" --force
    
      displayName: 'Bump release version'
    
    - script: |
    
       git push origin HEAD:$(Build.SourceBranchName)
    
      displayName: 'Update Git Repo'
      
    

    因为您在提交消息中有 [skip ci]。推回更新的 package.json 文件不会触发新的构建。见here

    【讨论】:

    • 谢谢!这非常有效。对于收到GenericContribute 错误的任何人,请参阅this question。如果您仍然无法使其工作,请复制错误中的 uid Build/<uid> 部分并将其粘贴到安全选项卡上的用户字段中,因为另一个问题中的答案建议+设置适当的权限作为答案提到。
    猜你喜欢
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 2020-04-24
    相关资源
    最近更新 更多