
Azure DevOps Script Step is a powerful tool that allows you to automate repetitive tasks in your pipeline. This guide will walk you through the process of setting up and using Azure DevOps Script Step to achieve pipeline success.
To start, you'll need to create a new pipeline in Azure DevOps. You can do this by clicking on the "New pipeline" button in the Pipelines tab. From there, you can select the repository and branch you want to use for your pipeline.
The Script Step is a flexible and customizable task that allows you to run any script or command in your pipeline. You can use it to automate tasks such as building, testing, and deploying your code. For example, you can use the Script Step to run a PowerShell script to automate a build process.
In the next section, we'll dive deeper into the details of setting up and using the Script Step in your pipeline.
Setting Up the Pipeline
Setting up a pipeline in Azure DevOps involves breaking down the process into stages, each with its own set of jobs and tasks. Stages can run sequentially or in parallel, depending on how you set dependencies.
A pipeline is a collection of stages, and stages can have multiple jobs that run in parallel. Jobs in a stage all run in parallel, and tasks within a job run sequentially. This allows for a more efficient and faster deployment process.
In Azure DevOps, jobs can be named according to their purpose, such as "deployment" which allows for additional options like deployment history and deployment strategies. This is in contrast to standard job types.
The environment property in a deployment stage is set to 'Staging' or 'Production', depending on the environment being targeted. This allows for easy switching between environments during the deployment process.
Here are the key components of a pipeline in Azure DevOps:
By understanding these components and how they work together, you can create a pipeline that meets your specific needs and automates your deployment process.
Cross-Platform Tools and Scripts
Cross-platform tools can be a challenge in Azure DevOps, but there are ways to make it easier. The script keyword is a shortcut for the command line task and can run Bash on Linux and macOS, and cmd.exe on Windows.
Using script can be useful when your task just passes arguments to a cross-platform tool, like calling npm with a set of arguments. You can easily accomplish this with a script step.
To run cross-platform tools with a script step, add a Command Line task to your pipeline and replace the body of the script with the tool and arguments you want to use, like this: npm install npm test.
Here are the steps to set up the pipeline:
- Add a Command Line task to your pipeline.
- Replace the body of the script with the tool and arguments you want to use.
If you need to detect the platform you're on, you can use a condition to detect what platform you're on. This can be useful when you need to use different commands on different platforms.
Here's an example of how to use a condition to detect the platform:
- Add a Bash task to your pipeline and set the Type to Inline.
- Replace the body of the script with the command you want to use, like this: export IPADDR=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
- Change the value of Run this task to "Custom conditions".
- In the Custom condition field that appears, enter "eq( variables['Agent.OS'], 'Linux' )".
This will run the script only on Linux. You can do the same for other platforms by changing the condition to match the platform you're on.
Here are the conditions for different platforms:
Configure Environments
Configure Environments is a powerful feature in Azure DevOps that allows you to manage different environments for your pipeline. You can define environments in the Azure DevOps UI under Pipelines in the navigation. After clicking on this, you will see that there are already some environments listed, which were automatically created when the environment property was added to the pipeline script.
To set approval gates, you can click on the three-dot menu in the top right of a specific environment and select Approvals and checks. You can add specific users and/or groups to the list of Approvers, and fill out the approvers to create the approval.
Multiple types of checks can be set for an environment, but for this example, we are only adding an approval for the pipeline. You can select Approvals on the form, add specific users and/or groups to the list of Approvers, and click 'Create'.
Here are the steps to set up an approval gate:
- Add specific users and/or groups to the list of Approvers.
- Fill out the approvers to create the approval.
Once an approval gate is set up, you can run the pipeline and it will wait for the approver to review and approve the deployment. The approver will receive an email and can log in to review the pipeline and approve or reject the deployment.
Pipeline Steps and YAML
A pipeline is comprised of Stages, Jobs, and Steps, which are defined in a YAML file. This file follows a specific syntax, with required indents and dashes that must be carefully maintained.
To create a pipeline, you need to define the structure of your pipeline, including the stages, jobs, and steps. This can be done using a YAML file, which provides a clear and concise way to define the pipeline.
The YAML syntax for a pipeline is quite straightforward, with each stage containing multiple jobs and each job containing multiple steps. For example, a simple pipeline might look like this:
1. Stage 1
* Job 1
+ Step 1
+ Step 2
* Job 2
+ Step 3
+ Step 4
This structure can be defined in a YAML file using the following syntax:
```
stages:
- stage1:
jobs:
- job1:
steps:
- step1
- step2
- job2:
steps:
- step3
- step4
```
The key components of a pipeline are Jobs, Stages, Tasks, and Steps, which are laid out in a logical order. By defining these components in a YAML file, you can create a repeatable and controlled pipeline that automates your workflow tasks.
Here's a breakdown of the key components:
- Jobs: A job is a collection of steps that are executed together.
- Stages: A stage is a collection of jobs that are executed together.
- Tasks: A task is a single unit of work that is executed as part of a job.
- Steps: A step is a single action that is executed as part of a task.
By understanding these components and how they fit together, you can create a pipeline that meets your needs and automates your workflow tasks.
Frequently Asked Questions
What is the difference between script and task in Azure pipeline?
A script is custom code that runs as a step in your pipeline, while a task is a pre-built, reusable action. Understanding the difference between scripts and tasks is key to building efficient and scalable Azure pipelines
Sources
- https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/cross-platform-scripting
- https://mercuryworks.com/blog/creating-a-multi-stage-pipeline-in-azure-devops
- https://medium.com/@muppedaanvesh/%EF%B8%8F-azure-devops-pipeline-essentials-jobs-stages-tasks-steps-%EF%B8%8F-1757d6366201
- https://spacelift.io/blog/terraform-azure-devops
- https://josh-ops.com/posts/extends-template/
Featured Images: pexels.com