AZ-400: Designing and Implementing Microsoft DevOps Solutions
Configure Activity Traceability and Flow of Work
Demo Integrate Azure Pipelines and GitHub Actions
In this guide, we demonstrate how to integrate Azure Pipelines with GitHub Actions. Our objective is to set up a project where a GitHub Action initiates work and automatically triggers an Azure Pipeline when code is pushed to GitHub.
Let's get started!
Connecting GitHub to Azure DevOps
To connect GitHub with Azure DevOps, start by navigating to the Project Settings in your SimpleWebAPI project, then select GitHub Connections. This is where you can link GitHub with Azure Boards. For this integration, we will connect the "SimpleWebAPI" GitHub repository to Azure DevOps.
Click on Connect Your GitHub Account.
After connecting, you will be presented with a list of available repositories. Click Save to redirect to your GitHub account for approval.
Select the "SimpleWebAPI" repository and proceed to approve, install, and authorize the connection.
This process establishes the connection between your GitHub repository and Azure DevOps.
Setting Up a Personal Access Token
Next, create a Personal Access Token (PAT) in Azure DevOps to allow secure interactions.
- Click on your user icon in Azure DevOps and select Personal Access Tokens from the User Settings dropdown.
- You can either view existing tokens or generate a new one. When generating, choose the required permissions and set an appropriate expiration date.
Important
Remember: PATs are displayed only once after creation. Be sure to copy and store your token securely.
This PAT will be used later in the GitHub Actions configuration.
Creating an Azure Pipeline
Return to the SimpleWebAPI project and click on Create Pipeline. When prompted with "Where is your code?", choose GitHub. Since the connection is already established, select the "SimpleWebAPI" repository. This action installs Azure Pipelines on GitHub.
Upon repository selection, configure your pipeline. Although pre-built templates (like ASP.NET) exist, choose the Starter Pipeline option for this example.
By default, the Starter Pipeline runs on ubuntu-latest
without a specific agent pool and includes sample scripts. You can retain these scripts for now:
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- script: echo Hello, world!
displayName: 'Run a one-line script'
- script: |
echo Add other tasks to build, test, and deploy your project.
echo See https://aka.ms/yaml
displayName: 'Run a multi-line script'
Specifying an Agent Pool
If you wish to use a specific agent pool, navigate to Project Settings > Agent Pools in Azure DevOps to review available agents. For this instance, we will use the "KodeKloud Customer" pool.
Update the Starter Pipeline with your preferred pool name, then save and run the pipeline:
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
name: 'KodeKloudCustomer'
steps:
- script: echo Hello, world!
displayName: 'Run a one-line script'
- script: |
echo Add other tasks to build, test, and deploy your project.
echo See https://aka.ms/yaml
displayName: 'Run a multi-line script'
After saving, the pipeline should run successfully.
Configuring the GitHub Action
Next, configure a GitHub Action in the "SimpleWebAPI" repository that will trigger the Azure Pipeline.
- In your repository, click on the Actions tab and select Set Up a Workflow Yourself. This will create a blank YAML file (commonly named
main.yml
). - Replace the template contents with the configuration below:
name: Trigger Azure Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Trigger Azure DevOps Pipeline
uses: Azure/pipelines@v1
with:
azure-devops-project-url: ${{ secrets.AZURE_DEVOPS_PROJECT_URL }}
azure-pipeline-name: 'jeremykodekloud.SimpleWebAPI'
azure-devops-token: ${{ secrets.AZURE_DEVOPS_TOKEN }}
This workflow is designed to run on any push to the main branch. It leverages the Azure Pipelines GitHub Action (version 1) to trigger the Azure DevOps Pipeline. Sensitive values such as the project URL and PAT are handled as GitHub secrets.
Storing Secrets in GitHub
Before committing your workflow:
- Go to Settings > Secrets and variables > Actions in your GitHub repository.
- Click New repository secret and add the following:
- Name:
AZURE_DEVOPS_PROJECT_URL
Value:https://dev.azure.com/jeremy0665/SimpleWebAPI
- Name:
- Similarly, add a new secret for
AZURE_DEVOPS_TOKEN
containing your personal access token.
Once added, the secrets will appear in the repository's secrets list:
Commit the workflow file to your main branch and monitor the status under the Actions tab. If configured correctly, you will see a running job triggered by your push.
Verifying the Integration
To verify the setup, make a small change locally and push it to GitHub. For example, in Visual Studio Code, execute:
git pull
Modify files (such as YAML configurations for GitHub Actions or Azure Pipelines) as needed, then commit and push:
git add .
git commit -m "Updated pipeline configuration and workflow"
git push
Both the GitHub Action and the Azure Pipeline should be triggered by the push. For reference, here is the GitHub Actions workflow file:
name: Trigger Azure Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Trigger Azure DevOps Pipeline
uses: Azure/pipelines@v1
with:
azure-devops-project-url: ${{ secrets.AZURE_DEVOPS_PROJECT_URL }}
azure-pipeline-name: 'jeremykodekloud.SimpleWebAPI'
azure-devops-token: ${{ secrets.AZURE_DEVOPS_TOKEN }}
Similarly, if you update your azure-pipelines.yml
file with corrections (for example, specifying the project type as an ASP.NET Web API application), the process remains the same. After updating, check both the GitHub Actions and Azure Pipelines dashboards for successful execution.
This confirms that every push to the main branch now triggers an Azure Pipeline run via the GitHub Action, ensuring continuous integration for your project.
Conclusion
By following the steps outlined in this guide, you have successfully integrated GitHub Actions with Azure Pipelines. Every commit to the main branch—whether it's through pull requests, merges, or direct pushes—will automatically trigger your continuous integration process. This seamless integration harnesses the powerful features of both GitHub and Azure DevOps to build, test, and deploy your code effectively.
Happy coding!
Watch Video
Watch video content