skip to Main Content

We use stages in Azure DevOps, as it is possible to select which stages, you want to run when running a pipeline (see picture). An annoying thing when running stages, however, is that if you install a program in Stage 1, it seems that you cannot use that program in Stage 2.

Is there a way to install a program just once, and then have it available in all stages?

enter image description here

Here an example of what I would like to do: 1) Install kubectl in Stage 1, 2) Use kubectl in Stage 2.

trigger:
- master

pool:
  vmImage: ubuntu-22.04

steps:
- stage: MakeK8sConfiguration
  jobs:
  - job: MakeK8sConfiguration
    steps:
      - script: |
          # Downloading kubectl
          curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
          
          # Installing kubectl
          sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

- stage: MakeK8sDeployment
  jobs:
  - job: MakeK8sDeployment
    steps:
      - script: |
        # Running kubectl
        kubectl

2

Answers


  1. From your YAML file, you are using Microsoft-Hosted agent:ubuntu-22.04.

    Refer to this doc: Microsoft-hosted agents

    Each time you run a pipeline, you get a fresh virtual machine for each job in the pipeline. The virtual machine is discarded after one job (which means any change that a job makes to the virtual machine file system, such as checking out code, will be unavailable to the next job).

    In your situation, different stages will use different and new Microsoft-hosted agents. They are independent.

    So when you install the kubectl at first stage, the second stage still not able to use the kubectl.You still need add additional steps to install kubectl in stage two.

    Is there a way to install a program just once, and then have it available in all stages?

    Based on your requirement, I suggest that you can use Self-hosted agent or VMSS agent to run the pipeline. They can keep the pipeline stages running on the same agent machine.

    Login or Signup to reply.
  2. Stages are handled from devops as different pipelines and they create a new working directory (you may notice that If you check the stage logs). This means that a different agent will be probably picked up to run your job. Although you specify the same agent pool, another VM or container may do the job in a Microsoft hosted agent pool. In order to resolve this you have two options.

    First option:
    Install the program also on the second stage
    Second option:
    Use a self hosted agent with the program installed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search