Search code examples
azure-devopsazure-pipelinesazure-pipelines-yamlazure-artifacts

How to trigger Azure Pipeline after successful push to Artifact Feed?


I need to trigger a Azure Pipeline after the successful push of Artifact NuGet Feed. I tried to use resources in YAML as below with the help of service connection. But while saving itself getting the error as "Only GitHub type service connections with PAT authentication scheme are supported for GitHub packages".

resources:
  packages:
    - package: MyPackage
      type: nuget
      name: MyFeed/MyPackage
      version: '*'
      trigger: true
      connection: 'NuGet-Conn'

Please let me know how to fix this issue. Otherwise please let me know any other way to trigger the pipeline.

Update:

First Pipeline:

- script: |
      echo "##vso[task.setvariable variable=myVar;isOutput=true]value123"
    name: SetVariable
    displayName: 'Set variable'

Second Pipeline:

resources:
  pipelines:
    - pipeline: sourcePipeline
      project: TestPrj
      source: FirstPipeline
      trigger:
        branches:
          include:
            - "*"

variables:
  - name: myVariable
    value: $[ resources.pipeline.sourcePipeline.outputs['Job1.SetVariable.myVar'] ]

jobs:
- job: Job2
  displayName: 'Job2'

  steps:
  - script: |
      echo "Value: $(myVar)"
      echo $(resources.pipeline.sourcePipeline.sourceBranch)

In this approach, I am not able to get the value in "myVar". Please let me know how the value.

Thanks.


Solution

  • According to Packages resource definition, package resource is designed for GitHub instead of Azure Artifact. That's why you see error Only GitHub type service connections with PAT authentication scheme are supported for GitHub packages.

    You can consume NuGet and npm GitHub packages as resources in YAML pipelines

    If your package is published from the Azure pipeline, you can trigger your target pipeline use this one. See the details from Trigger one pipeline after another.

    If your package is published somewhere else, for example from local machine, there is no out-of-the-box way to trigger the pipeline by Azure Artifact. You can submit a feature request from Developer Community to request for such feature and share the link of it here, so that others who want the same feature can follow and submit a vote to increase its priority.

    As a possible workaround, you may consider classic release pipeline and select Azure Artifact as the artifact. However, it's not perfect and has some limits.

    1. Steps:

      enter image description here

      • Select Azure Artifact as the artifact
      • Feed: Your target feed
      • Package type: NuGet
      • Package: The package you want to check updates.

      Enable CI trigger for the selected package. enter image description here

    2. Result:

      A new release is created every time a new version of the selected package is published.

      enter image description here

    3. Limits:

      • It's only supported in the classic release pipeline.
      • The trigger is based on the package instead of the feed. You may need to add multiple packages as the artifact.

    Update:

    For your question "how to pass parameters to the second pipeline based on the execution of first pipeline",

    You can run REST API Runs - Run Pipeline to trigger the target build and pass the parameters. See the example below for reference.

    First Pipeline: Ensure to replace { } with your actual values.

    pool:
      vmImage: ubuntu-latest
    
    steps:
    - script: |
        echo "##vso[task.setvariable variable=myVar;isOutput=true]value123"
      name: SetVariable
      displayName: 'Set variable'
    
    - script: |
        echo "The value of  myVar is :$(SetVariable.myVar)"
    
    - powershell: |
          $token = "{PAT}"
          $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
          $url="https://dev.azure.com/{OrgName}/{ProjecctName}/_apis/pipelines/{PipelineID}/runs?api-version=7.1"
          $body = "{
              `"templateParameters`": {
                  `"customVar1`": `"$(SetVariable.myVar)`",
                  `"customVar2`": `"test`"
    
              }
          }"
          $head = @{ Authorization ="Basic $token" }
          Invoke-RestMethod -Uri $url -Method Post -Headers $head -Body $body -ContentType application/json
    

    Second Pipeline:

    parameters:
    - name: customVar1
      type: string
      default: ''
    - name: customVar2
      type: string
      default: ''
    
    trigger:
    - none
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
    - script: |
        echo 'The value of parameter customVar1 is ' ${{parameters.customVar1}}
        echo 'The value of parameter customVar2 is ' ${{parameters.customVar2}}
      displayName: 'Echo the parameters'
    
    

    Result:

    enter image description here