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?
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
From your YAML file, you are using Microsoft-Hosted agent:ubuntu-22.04.
Refer to this doc: Microsoft-hosted agents
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.
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.
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.