azure-devops

Adding tag for specific Docker image


I am using a pipeline (MainPipeline) which triggers the next one (DependantPipeline).

During the DependantPipeline execution on Azure DevOps I see the error on Terraform Plan step during the fetching Docker image where it can not find the image with proper tag.

When I added that tag manually pipeline passes.

deploying.yml

parameters:
  - name: pipelineOptions
    type: object
  - name: environment
    type: string
  - name: env
    type: string
  - name: region
    type: string
    default: $(region)

jobs:
  - template: cicd/deployment.yml@Pipelines
    parameters:
      pipelineOptions: ${{parameters.pipelineOptions}}
      environment: ${{parameters.environment}}

  - template: ./terraform/deploy.yml
    parameters:
      pipelineOptions: ${{parameters.pipelineOptions}}
      environment: ${{parameters.environment}}
      env: ${{parameters.env}}
      region: ${{parameters.region}}

/cicd/terraform/deploy.yml

parameters:
  - name: pipelineOptions
    type: object
  - name: environment
    type: string
  - name: region
    type: string
    default: $(region)
    [...]

jobs:
  - deployment: terraform_plan
      displayName: "Teraform Plan"
    environment: ${{parameters.environment}}
    workspace:
      clean: all
    strategy:
      runOnce:
        deploy:
          steps:
            [...]
            - task: TerraformCLI@0
              displayName: 'Terraform Plan'
              name: plan
              inputs:
                command: plan
                publishPlanResults: 'Terraform-Plan-${{parameters.env}}-$(System.StageAttempt)'
                workingDirectory: '$(Pipeline.Workspace)/${{parameters.artifactName}}/terraform'
                commandOptions: '-out=$(Build.ArtifactStagingDirectory)/terraform/terraform.tfplan -detailed-exitcode'

Output from Terraform Plan step:

The pipelineID of resources pipeline is 379701 The runID of resources pipeline is 379701 The runName of resources pipeline is 379701

Terraform Plan task:

379702 from registry: Got bad response from registry: 404 Not Found

EDIT 1.

I updated that part in deploy.yml like that:

- task: TerraformCLI@0
              displayName: 'Terraform Plan'
              name: plan
              inputs:
                command: plan
                publishPlanResults: 'Terraform-Plan-${{parameters.env}}-$(System.StageAttempt)'
                workingDirectory: '$(Pipeline.Workspace)/${{parameters.artifactName}}/terraform'
                commandOptions: '-out=$(Build.ArtifactStagingDirectory)/terraform/terraform.tfplan -compact-warnings -detailed-exitcode -input=false -var-file=./config/$(projectId).tfvars -var="source_pipeline_runid=$(resources.pipeline.SourcePipeline.runID)"'

And added new variable in variables.tf, added reference to that variable in other .tf files.

variable "source_pipeline_runid" {
  description = "runID"
  type        = string
}

Solution

  • In DependantPipeline, if you set MainPipeline as a pipeline resource, you can use the predefined variable resources.pipeline.<Alias>.runID to get he buildId (runId) of MainPipeline.

    For example, the YAML of DependantPipeline.

    resources:
      pipelines:
      - pipeline: mainPipe
        source: MainPipeline
        trigger:
        . . .
    
    steps:
    - script: |
        echo "Print the buildId/runId of pipeline resource."
        echo "resources.pipeline.mainPipe.runID = $(resources.pipeline.mainPipe.runID)"
    

    For more available predefined variables, you can see "Pipeline resource variables".


    EDIT:

    From the YAML of DependantPipeline you provided, I can see you set the pipeline resource as below.

    resources:
      . . .
      pipelines:
        - pipeline: MainPipeline
          project: DEV
          source: 'MainPipeline'
          trigger:
            branches:
              include:
                - neworder/pipeline
    

    You define the alias of the pipeline resource as 'MainPipeline'. But, when you reference the predefined variable 'resources.pipeline.<Alias>.runID', you are using a wrong alias 'SourcePipeline' in the expression "$(resources.pipeline.SourcePipeline.runID)".

    The correct expression should be "$(resources.pipeline.MainPipeline.runID)" that uses the right alias which is consistent with the one you defined on the pipeline resource.


    EDIT_2:

    Below is a sample to show to pass the runId as parameter into the nested templates.

    1. The pipelines:

      • PipelineA: the main pipeline.
      • PipelineB: the dependant pipeline that will be triggered by PipelineA.
    2. The main YAML of PipelineB.

    # azure-pipelines.yml
    
    resources:
      repositories:
      - repository: Pipelines
        type: git
        name: templatesYAML
        ref: main
      pipelines:
      - pipeline: mainPipe
        source: PipelineA
        trigger:
          branches:
            include:
            - main
    
    extends:
      template: /cicd/pipeline.yml@Pipelines
      parameters:
        SourcePipeInfor:
          pipelineID: $(resources.pipeline.mainPipe.pipelineID)
          runID: $(resources.pipeline.mainPipe.runID)
          runName: $(resources.pipeline.mainPipe.runName)
    
    1. The template YAML files.

      enter image description here

      • The cicd/pipeline.yml.
    # cicd/pipeline.yml
    
    parameters:
    - name: SourcePipeInfor
      type: object
      default: []
    
    stages:
    - template: /cicd/deploy_environments.yml@Pipelines
      parameters:
        SourcePipeInfor: ${{parameters.SourcePipeInfor}}
    
    # cicd/deploy_environments.yml
    
    parameters:
    - name: SourcePipeInfor
      type: object
      default: []
    
    stages:
    - stage: deploy
      jobs:
      - job: showInfor
        steps:
        - bash: |
            echo "The pipelineID of resources pipeline is ${{ parameters.SourcePipeInfor.pipelineID }}"
            echo "The runID of resources pipeline is ${{ parameters.SourcePipeInfor.runID }}"
            echo "The runName of resources pipeline is ${{ parameters.SourcePipeInfor.runName }}"
          displayName: 'Print resources pipeline information'
    
    1. The result.

      enter image description here