Cancels all previous Azure Devops builds. Meant to be run from an Azure Devops pipeline
npm install azdo-cancel-previous-builds
This is a command-line script that will cancel any previous in-progress
Azure Devops builds.
By using the environment variables set within Azure Devops pipelines,
this script will query the Azure Devops API to find previous runs of this pipeline
on the same branch.
It will then cancel these builds.
From your Azure Devops pipeline, include a script task to include Node (v10 or up), and then execute the script:
``yaml`
- task: NodeTool@0
displayName: Install node
inputs:
versionSpec: 17.x
- script: npx azdo-cancel-previous-builds
displayName: Cancel previous builds
env:
System.AccessToken: $(System.AccessToken)
Since the System.AccessToken is a secret variable, you need to explitly add it to make it available.
_Note: The access token needs to have the 'read builds' and 'update builds' permission.
You can also use a custom access token that has these permissions._
The script requires these environment variables, which should be present in the context
of an Azure Devops build:
- System.TeamFoundationCollectionUri: The base URL to the Azure Devops collectionSystem.TeamProject
- : The name of the projectSystem.DefinitionId
- : The ID of the build definitionBuild.BuildId
- : The ID of the current buildBuild.SourceBranch
- : The current source code branch being builtSystem.AccessToken
- : A private access token
_This one needs to be explitly added like in the example above._
First, run npm install to install dependencies.
Second, this script depends on certain environment variables to be present. To simulate this locally, create a file called .env and fill this with:
``
SYSTEM_ACCESSTOKEN=
SYSTEM_TEAMFOUNDATIONCOLLECTIONURI=https://dev.azure.com/
SYSTEM_TEAMPROJECT=
SYSTEM_DEFINITIONID=
BUILD_BUILDID=
BUILD_SOURCEBRANCH=
To run the script once, enter:
``
npm run start
To run the script on every change, enter:
```
npm run dev