skip to Main Content

In my continuous integration pipeline I have two jobs one is for docker code build and another for code vulnerability scanning (which takes too much of time) I want that once my first job is completed it should directly trigger the release pipeline rather than waiting for the second job to complete.How can i achieve this.

I am expecting that my release pipeline should be triggered and not wait for the secops jobs to complete.

2

Answers


  1. You can trigger a pipeline when one or more stages of the triggering pipeline complete by using the stages filter.

    In your specific case, this means having separate stages for each job, for example:

    • DockerBuild stage: Builds the Docker image(s)
    • CodeAnalysis stage: Runs the code vulnerability scanning

    Trigger configuration would be something like:

    resources:
      pipelines:
      - pipeline: MyPipeline
        source: SourcePipeline
        trigger:    
          stages:         
          - DockerBuild # as defined in the source pipeline
    

    See Trigger one pipeline after another for more details.

    Login or Signup to reply.
  2. According to your tag and description, you are using the Classic release pipelines. So, the pipeline resource triggers in yaml may not be suitable for you.

    In this case, you can consider add a PowerShell task to run the Releases – Create REST API command after the build is successful in your docker code build job.

    The following is my test steps:

    1. Select the release pipeline you want to run.
    2. Select More actions > Security.
      More actions  > Security
    3. Set the build service account you are using in the build pipeline with the Create releases
      permission, and then Save your changes.
      Create releases permission
    4. The run the following build pipeline to test the issue:
    trigger:
    - none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    jobs:
      - job: A
        displayName: Docker code build
        steps:
          - task: PowerShell@2
            inputs:
              targetType: 'inline'
              script: |
                $organization="your organization name"
                $project="your project name"
                $releaseDefinitionId = "1"    #Your release Definition Id
    
                # API URL
                $url = "https://vsrm.dev.azure.com/$organization/$project/_apis/release/releases?api-version=7.0"
                
                $body = @{
                    definitionId = $releaseDefinitionId
                } | ConvertTo-Json
                
                $response = Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType "application/json" -Headers @{"Authorization" = "Bearer $(System.AccessToken)"}
                
                # out put response
                $response
      - job: B
        dependsOn: A
        displayName: code vulnerability scanning
        steps:
          - task: CmdLine@2
            inputs:
              script: |
                echo code vulnerability scanning
    

    Test result:

    The release pipeline is triggered after the PowerShell task runs in the Docker code build job.

    enter image description here

    By the way, you can also use your personal access token instead of the $(System.AccessToken) of the pipeline. Then you need to create a PAT with the release execute permission and use it in the PowerShell task.

    Sample PowerShell script with PAT:

    #Your personal access token
    $token = ""  
    $organization = ""
    $project = ""
    $releaseDefinitionId = "1"
    
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$token)))
    $headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    #API URL
    $url = "https://vsrm.dev.azure.com/$organization/$project/_apis/release/releases?api-version=7.0"
    
    
    $body = @{
        definitionId = $releaseDefinitionId
    } | ConvertTo-Json
    
    $response = Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType "application/json" -Headers $headers
    
    $response
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search