【问题标题】:How to publish an artifact of Azure Pipelines into the main branch of the repository?如何将 Azure Pipelines 的工件发布到存储库的主分支?
【发布时间】:2023-01-11 16:31:06
【问题描述】:

我正在尝试使用当前版本的版本在我的存储库中创建一个文件,只要有推送到主版本,该文件就会自动更新。我试过使用

- task: Bash@3
        inputs:
          targetType: 'inline'
          script: |
            sudo echo "$(major).$(minor).$(patch)" > version.txt
            cat version.txt

但是,即使 cat 命令显示了正确的内容,该文件也不会在 repo 上创建。

我的一个同事建议我使用一个神器,我开发了以下代码:

steps:
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        sudo echo "$(major).$(minor).$(patch)" > version.txt
        cat version.txt
  - task: PublishPipelineArtifact@1
    inputs:
      publishLocation: filepath
      targetPath: version.txt        # path to the folder or file to publish
      artifactName: version      # name of the artifact to create

工件制作正确,我可以下载它并查看正确的版本号。有没有办法将这个工件直接推送到我的 Azure 存储库主分支的根目录中?提前致谢。

【问题讨论】:

  • sudo echo??? !!!
  • @phd 我已经尝试了 echo 和 sudo echo 以了解为什么我无法在存储库上创建文件
  • 您不能使用 sudo echo > file 来克服重定向到文件的权限问题,因为第一个 shell 在运行 sudo 之前进行了重定向;如果重定向失败,shell 甚至不会启动sudo。我的建议是在这里删除sudo

标签: bash git azure azure-devops azure-pipelines


【解决方案1】:

您需要在创建文件后执行 git add 和 commit(我假设您使用的是 Git,因为您在谈论存储库):

https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/git-commands?view=azure-devops&tabs=yaml

您正在谈论的工件实际上只是一个概念,而不是您可以或应该在任何地方提交的有形事物。实际上,当您发布管道工件时,您实际上只是将文件、文件夹结构或压缩包存储在某个地方,它可以被另一个阶段或管道下载。在您的示例中,它只是一个文件,因此您将提交该文件。当然,如果管道在运行开始时检出其他分支,则您需要检出主分支。

【讨论】:

    【解决方案2】:

    有没有办法将这个工件直接推入 我的 Azure 存储库的主要分支?

    朱卡克是对的。要将工件(在您的场景中为version.txt)直接推送到 Azure 存储库主分支的根目录中,您需要运行 git 命令以添加 version.txt 文件,然后将其提交以推送到存储库中。见Run Git commands in a script

    我正在尝试使用以下版本在我的存储库中创建一个文件 当前版本,只要有推送就会自动更新 到主要。我试过使用

    但是,根据您上面的描述,在主分支的管道中启用了 CI 触发器,因此只要向主分支推送,就会触发新管道。这意味着当 version.txt 被推送到 Azure 存储库的主分支时,它将触发一个新的管道。为避免这种情况,我们可以在作为推送一部分的任何提交的消息中包含 [skip ci],Azure Pipelines 将跳过为此推送运行 CI。见Skipping CI for individual pushes

    最终的 YAML 供您参考:

    trigger:
    - main
    
    variables:
     system.debug : true
     major: '1'
     minor: '0'
     patch: $[counter(variables['minor'], 1)]
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
      - checkout: self
        persistCredentials: true
      - task: Bash@3
        inputs:
          targetType: 'inline'
          script: |
            sudo echo "$(major).$(minor).$(patch)" > version.txt
            cat version.txt
            git config --global user.email "you@example.com"
            git config --global user.name "Your name"
            git status
            git checkout -b main
            git add --all
            git status
            git commit -m "Push version.txt to repo and [skip ci]"
            git push origin main
            git status
          workingDirectory: '$(Build.SourcesDirectory)'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 2016-09-03
      • 2017-06-28
      相关资源
      最近更新 更多