Search code examples
azureazure-pipelines-build-taskazure-pipelines-yamlazure-pipelines-tasks

Azure pipeline - Execute one of the stages in a Container


Our Organization has started migrating to Azure. We have Azure pipeline template which runs checkout, build and deploy stages on vmImage: 'ubuntu-18.04'. Our plan is to introduce "Test" stage in the template and run tests in Container.

Question:

  • When the entire pipeline is running on vmImage, is it possible to run one of the stages (Test) in Container?
  • How to copy build artifacts from vmImage to run tests in Container?

Can someone please shed some light on this?

Thanks a lot!


Solution

  • When the entire pipeline is running on vmImage, is it possible to run one of the stages (Test) in Container?

    In Azure Devops Yaml Pipeline, you could specify the container at the job level.

    Then the job could run on the target container.

    Here is a doc about the detailed information.

    How to copy build artifacts from vmImage to run tests in Container?

    In the previous stage, you could publish the build artifacts with the Publish Build Artifacts task.

    In the stage (running job in the container), you could use the Download Build Artifacts task to download the artifacts to the container.

    Finally, you could add the steps to test it.

    Here is my sample:

    resources:
      containers:
      - container: python
        image: python:3.8
    trigger:
    - none
    
       
    stages:
    
      - stage: artifacts
        pool:
          vmimage:  ubuntu-latest
        jobs:
          - job: test123
            steps:
            - task: PublishBuildArtifacts@1
              inputs:
                PathtoPublish: '$(Build.Sourcesdirectory)'
                ArtifactName: 'drop'
                publishLocation: 'Container'
            - script: echo $(System.ArtifactsDirectory)
    
      - stage: testcontainer
        jobs:
          - job: test123
            container:  python
        
            steps:
            - task: DownloadBuildArtifacts@0
              inputs:
                buildType: 'current'
                downloadType: 'single'
                artifactName: 'drop'
                downloadPath: '$(System.ArtifactsDirectory)'
    
            - script: |
                echo $(System.ArtifactsDirectory)
              displayName: 'Run a multi-line script'
     
    

    Note: The container image is from Docker Hub, Azure Container Registry or another private container registry.