Azure Pipeline - 在不提交存储库的情况下发布 Maven 工件

Azure Pipeline - Publish Maven Artifacts Without Comitting to Repository

一般来说,Azure 是新手,如果我的问题不够简洁,我深表歉意。

我正在尝试做的事情: 当有人推送到 master 时,我希望管道提升项目版本并将其发布为 工件 选项卡。这不应该创建任何额外的提交。

我试过的方法: 为了验证我与 Artifacts 的本地连接是否正常工作,我尝试了 mvn deploy,但效果很好不会自动处理版本冲突,所以我尝试切换到

mvn -B release:prepare release:perform

因为它确实处理了碰撞,但这会产生其他问题,因为它会额外提交两次 git 而不是将自己修改到头部,我需要修改软重置并强制推送才能获得它正确的。也不确定它是否会发布到 Artifacts,因为我在获取 git 凭据时遇到问题...

是否有一种方法可以让管道查看已经发布的工件并基于此进行调整,这样它就不必向存储库提交任何内容?

left-hand 菜单上的 Azure Artifacts 旨在作为共享模块使用,用于自定义包而不是发布构建工件。对于 Build artifacts,您可以检查特定的管道(已完成 运行)→“摘要”→“相关”,您可以看到发布了哪些管道。

不知道是不是我理解错了你的问题,这篇或许可以帮助你了解一下。

好的,经过很多天我终于搞定了。 使用组织提要发布工件而不提交代码到 repo

trigger:
  - main

pr: none

pool:
  vmImage: ubuntu-latest

variables:
  - name: artifactVersion
  - name: organization
    value: '<organization>'
  - name: feedName
    value: '<feedName>'

steps:
  - task: PowerShell@2
    displayName: Fetching latest version of artifact
    inputs:
      targetType: 'inline'
      script: |
        
        # Get list over packages
        $url = 'https://feeds.dev.azure.com/$(organization)/_apis/packaging/Feeds/$(feedName)/packages?api-version=6.0-preview.1'
        $pipeline = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

        # Find our artifact in the feed over all artifacts
        $packageName = echo '${project.groupId}:${project.artifactId}' | mvn -N -q -DforceStdout help:evaluate
        echo "Looking for artifact versions for packageName: $packageName"
        $packageId = ''
        Foreach ($package IN $pipeline.value) {
          if($package.name -eq $packageName) {
            $packageId = $package.id
          }
        }
        if($packageId -eq '') {
          echo "No artifact found, assuming this is first release"
          $artifactVersion = 1
        } else {
          $url = 'https://feeds.dev.azure.com/$(organization)/_apis/packaging/Feeds/$(feedName)/Packages/' + $packageId + '/versions?api-version=6.0-preview.1'
          $pipeline = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
        
          # Find latest version of the artifact
          Foreach ($package IN $pipeline.value) {
            if($package.isLatest) {
              $artifactVersion = [int]$package.version+1
            }
          }
        }
        Write-Host "##vso[task.setvariable variable=artifactVersion]$artifactVersion"
        echo "Preparing release for artifaction version: $artifactVersion"
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
  - task: MavenAuthenticate@0
    inputs:
      artifactsFeeds: '$(feedName)'
  - task: PowerShell@2
    displayName: Preparing artifact
    inputs:
      targetType: 'inline'
      script: |

        echo "Artifact version to be released: $(artifactVersion)"
        $userNameOfHead = git log -1 --pretty=format:'%an'
        $userEmailOfHead = git log -1 --pretty=format:'%ae'
        git config --global user.email "$userNameOfHead"
        git config --global user.name "$userEmailOfHead"

        mvn -B -DreleaseVersion="$(artifactVersion)" release:prepare release:perform

pom.xml 需要更改:

...
<plugin>
   <groupId>org.apache.maven.plugins</groupId>                
   <artifactId>maven-release-plugin</artifactId>
   <version>2.5.3</version>
   <configuration>
      <pushChanges>false</pushChanges>
      <localCheckout>true</localCheckout>
   </configuration>
</plugin>
...
<distributionManagement>
   <repository>
      <id><id></id>
      <url>https://pkgs.dev.azure.com/<organization>/_packaging/<feedName>/maven/v1</url>
   </repository>
</distributionManagement>

<scm>
   <developerConnection>
      scm:git:git@github.com:<repo>
   </developerConnection>
</scm>
...