Search code examples
azuremavenazure-devopsazure-artifacts

Azure Pipeline - Publish Maven Artifacts Without Comitting to Repository


New to Azure in general so sorry if my question isn't as concise as it should be.

What I'm trying to do: When someone pushes to master I want the pipeline to bump the project version and release it as an artifact that is available in the Artifacts tab. This shouldn't create any additional commits.

What I've tried: To verify that my local connection to Artifacts was working I tried mvn deploy which worked fine but it doesn't handle version bumping automatically, so instead I tried switching over to

mvn -B release:prepare release:perform

As it does take care of bumping but this creates other issues as it commits two additional times to git instead of amending itself onto the head and I need to tinker with soft resets and force push to get it correct. Also not sure if it does release to Artifacts as I had issues getting git credentials working...

Is there a way for the pipeline to look at what Artifacts has already been released and just bump based on that so it doesn't have to commit anything to the repository?


Solution

  • Okay, so after a lot of days I finally got it workig. Releasing artifacts without committing code to repo using organization feeds

    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 changes needed:

    ...
    <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>
    ...