Azure DevOps as Code & Github Actions
Hey everyone, Github Actions is out on private preview. I was wondering how can we benefit from this service? What can it serve knowing that we have a lot of DevOps tools ?
Let’s assume that I’m using Azure DevOps. What if someday I lose the build or release pipelines ? How can I deal with it to get back the pipelines quickly and AUTOMATICALLY (knowing that I will be under pression).
This is where Github Actions comes in. Actually, we can use it as a tool to automate the automation tool ( We can use Github Actions to automatically create and configure Azure DevOps for example).
The architecture below explains how Github Actions can interact with Azure DevOps so we can automate it.
Azure DevOps As Code
Automating the configuration of a DevOps tool is not a new way to do, but many industries do not think about it. They just create the CI/CD pipelines manually thinking that surprises won’t happen.
Azure DevOps exposes REST APIs which provide a full control of the service.
- Authentication :
In order to access and consume the REST API, we need an authorization token. We need to generate a Personal Access Token (PAT).
First, go to security
Then, click on Personal Access Token and new token
Then select the scopes that you think they are relevant.
- Create a Build definition :
Let’s assume that you already have a repository containing YAML File that defines your process stages.
We will create a build which will consume this YAML File and create a build definition. I defined a JSON File which describes the build definition as below :
There are some important parameters :
- Name : A build definition name.
- Process : A sequence of build steps. Since we are using YAML Format, we will describe using the next members :
- yamlFilename : The name of the YAML File inside the repository
- Variables : Additional variables if available
- Repository : Described with the following attributes.
- Url : A Repository url
- Name : A repository name.
- Type : A repository type that described by the RepositoryTypes class.
The build will be created following this definition. The function below creates the build on Azure DevOps :
The function sends a POST Request to the build API to create the build.
- Create Release Definition :
We will use the same process as the creation of the Build Definition. We will consume a JSON File which defines the connections and the deployment staging. It’s defined as it’s shown in the link : https://github.com/charotAmine/azureDevopsRest/blob/master/release.json
Then, we run the function that creates the build
Putting all together
Now that all the scripts are ready, we can use Github Actions as a way to drive Azure Devops. In case if you lose your pipelines, You just need a run from Github Actions to recreate everything as it was before.
The workflow is as below
When we run it, firstly we must see some green colors :D
Then, on Azure DevOps we must see the Build pipeline created respecting the defined JSON.
Also the defined YML File
Finally, the release pipeline must be created
Now, you know how to drive Azure DevOps using Github Actions !
You can find the script, JSON and the workflow yaml here : https://github.com/charotAmine/azureDevopsRest
Bella ciao,