Azure Pipeline Part 14 – Environments Deployment Jobs
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
You can create an Environment for your application deployment In Azure Pipeline. An environment lets you manage your application deployment in a central place with optional feature like adding approval in your pipeline or monitoring your entire application deployment pipelines.
Using an environment, you can bring all your deployment pipeline with respective resources in a central place to manage your deployments. For example, suppose you application uses multiple pipelines for deployment, using environment you can track all the pipelines status in a single place or manage security and add approvals. An Environment is used in conjunction with a deployment job to manage your application deployments.
You can put your application deployment steps in a special type of job called a deployment job. A deployment job is a collection of steps that are run sequentially against an environment. Using deployment jobs, you get the deployment history across pipelines, down to a specific resource and status of the deployments for auditing and define the deployment strategy.
Note: A deployment job doesn’t automatically clone the source repo. You can checkout the source repo within your job with checkout: self.
Azure Pipeline Part 14 – Environments Deployment Jobs
Prerequisites:
- One active Azure DevOps account
- Personal Access Token (PAT)
- A self-hosted agent registered to your Azure DevOps organization
Setup:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
## ----- ## Setup ## ----- ## Define variables ORGANIZATION=" USER=$(az account show | jq -r .user.name) PROJECT=" PAT=' B64_PAT=$(printf "%s"":$PAT" | base64) ## Test if authentication working (List projects in your organization) curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 | jq . ## Create a new Azure DevOps project curl -s --request POST \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "name": "'"$PROJECT"'", "description": "Demo Project For Azure Pipeline", "capabilities": { "versioncontrol": { "sourceControlType": "Git" }, "processTemplate": { "templateTypeId": "6b724908-ef14-45cf-84f8-768b5384da45" } } }' \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 | jq . ## Get project default repository details REPOSITORY_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories?api-version=6.0 | jq -r .value[0].id) && curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories/$REPOSITORY_ID?api-version=6.0 | jq . ## Create a new Azure Pipeline using REST API curl -s --request POST \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "folder": null, "name": "mypipeline", "configuration": { "type": "yaml", "path": "/azure-pipelines.yml", "repository": { "id": "'"$REPOSITORY_ID"'", "name": "'"$PROJECT"'", "type": "azureReposGit" } } }' \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/pipelines?api-version=6.0-preview.1 | jq . ## Clone project default repository REPO_HTTPS_CLONE_URL=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories?api-version=6.0 | jq -r .value[0].webUrl ) && B64_PAT=$(printf "%s"":$PAT" | base64) && git -c http.extraHeader="Authorization: Basic ${B64_PAT}" \ clone $REPO_HTTPS_CLONE_URL cd $PROJECT |
Azure Pipelines – Environments:
An environment is a collection of resources that you can target with deployments from a pipeline. Typical examples of environment names are Dev, Test, QA, Staging, and Production.
Azure Pipelines – Environments Syntax:
1 2 3 4 5 6 7 |
## Environment Syntax: environment: name: string # Name of environment. resourceName: string # Name of resource. resourceId: string # Id of resource. resourceType: string # Type of environment resource. tags: string # List of tag filters. |
Azure Pipelines – Deployment Jobs:
A deployment job is a collection of steps to be run sequentially. A deployment job can be used to target an entire environment (group of resources) or a particular resource within the environment.
Azure Pipelines – Deployment Jobs Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
## Deployment Jobs Syntax: jobs: - deployment: string # name of the deployment job, A-Z, a-z, 0-9, and underscore. The word "deploy" is a keyword and is unsupported as the deployment name. displayName: string # friendly name to display in the UI pool: # not required for virtual machine resources name: string # Use only global level variables for defining a pool name. Stage/job level variables are not supported to define pool name. demands: string | [ string ] workspace: clean: outputs | resources | all # what to clean up before the job runs dependsOn: string condition: string continueOnError: boolean # 'true' if future jobs should run even if this job fails; defaults to 'false' container: containerReference # container to run this job inside services: { string: string | container } # container resources to run as a service container timeoutInMinutes: nonEmptyString # how long to run the job before automatically cancelling cancelTimeoutInMinutes: nonEmptyString # how much time to give 'run always even if cancelled tasks' before killing them variables: # several syntaxes, see specific section environment: string # target environment name and optionally a resource name to record the deployment history; format: strategy: runOnce: #rolling, canary are the other strategies that are supported deploy: steps: [ script | bash | pwsh | powershell | checkout | task | templateReference ] |
Azure Pipelines – Environment and Deployment Jobs Example:
Step 1: Create a new Environment in Azure Pipeline.
Login to your Azure DevOps portal and navigate to Azure Pipeline for the Project that we have created during the setup.
Click on “Environment” under Azure Pipeline and click “Create environment”.
Provide a name for your environment and select a resources type and click “Next”.
Note: Azure Environment supports Kubernetes and Virtual machines as resources type and in this demo, we are going to use a virtual machine with Linux OS.
Select the OS for your resources and copy the script that you need to execute in your Linux machine.
Log in to your Linux machine and execute the script that you previously copied. Below is the script for your reference –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
mkdir azagent cd azagent curl -fkSL -o vstsagent.tar.gz \ https://vstsagentpackage.azureedge.net/agent/2.195.2/vsts-agent-linux-x64-2.195.2.tar.gz tar -zxvf vstsagent.tar.gz if [ -x "$(command -v systemctl)" ]; then ./config.sh --environment --environmentname "MyEnv" \ --acceptteeeula \ --agent " --url https://dev.azure.com/ --work _work \ --projectname " --auth PAT --token " --runasservice sudo ./svc.sh install; sudo ./svc.sh start else ./config.sh --environment --environmentname "MyEnv" \ --acceptteeeula \ --agent " --url https://dev.azure.com/ --work _work \ --projectname " --auth PAT --token " ./run.sh; fi ./config.sh |
This script will download the Azure DevOps agent binary and install and configure in interactive mode. Optionally you can add a tag when prompted.
1 2 3 |
Enter Environment Virtual Machine resource tags? (Y/N) (press enter for N) > Y Enter Comma separated list of tags (e.g web, db) > dev Successfully added the agent |
Once you create the environment and resource it will be visible in your Azure DevOps portal.
Next, we are going to use this environment and resource in a deployment job in Azure Pipeline.
Step 2: Create an Azure Pipeline config YAML file which uses a deployment job to deploy in the environment created in previous step.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
## ------------------------------ ## Environments & Deployment Jobs ## ------------------------------ ## Create pipeline definition cat <<'EOF'> azure-pipelines.yml stages: - stage: deploy jobs: - deployment: MyDeployment displayName: MyAppDeployment environment: name: 'MyEnv' resourceName: 'CloudAffaire' resourceType: VirtualMachine tags: 'dev' strategy: runOnce: deploy: steps: - checkout: self - script: echo my first deployment EOF ## Push the changes to Azure Repo git add . git commit -m "deployment1" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Note: You might need to provide some additional access to your Azure Pipeline in order to use the new environment and resources.
Next, we will add manual approval in Azure Pipeline.
Azure Pipeline – Manual Approval:
Step 1: Login to Azure DevOps portal and navigate to the environment created previously. From the options, click “Approval and checks”.
Step 2: Click on the “Approvals”
Step 3: Add an approver and click “Create”.
Step 4: Do some update in pipeline YAML file and push the changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
## Update pipeline definition cat <<'EOF'> azure-pipelines.yml stages: - stage: deploy jobs: - deployment: MyDeployment displayName: MyAppDeployment environment: name: 'MyEnv' resourceName: 'CloudAffaire' resourceType: VirtualMachine tags: 'dev' strategy: runOnce: deploy: steps: - checkout: self - script: echo my second deployment EOF ## Push the changes to Azure Repo git add . git commit -m "deployment2" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
If you now check the Azure Pipeline UI, you will see that the pipeline is waiting for approval. The pipeline will not get executed till its approved by the approver.
Step 5: Approve the pipeline.
Once approved, you pipeline will start execution.
Clean up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## -------- ## Clean up ## -------- ## Delete the Azure DevOps project and local repository PROJECT_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 \ | jq -r '.value[] | select(.name == "'"$PROJECT"'") | .id') && curl -s --request DELETE \ -u $USER:$PAT \ --header "Content-Type: application/json" \ https://dev.azure.com/$ORGANIZATION/_apis/projects/$PROJECT_ID?api-version=6.0 cd .. rm -rf $PROJECT |
Hope you have enjoyed this article. To know more about Azure DevOps, please refer below official documentation
https://docs.microsoft.com/en-us/azure/devops/?view=azure-devops