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.

The image shows an Azure DevOps project dashboard for "SimpleWebAPI," displaying project details, a section to add a project description, and project statistics with a 50% pipeline success rate.

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.

  1. Click on Connect Your GitHub Account.

    The image shows a web page for connecting GitHub with Azure Boards, featuring a sidebar with project settings and an illustration of a person watering a plant with a dog nearby.

  2. After connecting, you will be presented with a list of available repositories. Click Save to redirect to your GitHub account for approval.

  3. Select the "SimpleWebAPI" repository and proceed to approve, install, and authorize the connection.

    The image shows a GitHub permissions screen for installing Azure Boards, with options to select repositories and grant specific read and write permissions.

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.

  1. Click on your user icon in Azure DevOps and select Personal Access Tokens from the User Settings dropdown.
  2. 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.

The image shows the Azure DevOps user settings page for managing personal access tokens, listing several tokens with their status and expiration dates.

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.

The image shows an Azure DevOps interface for creating a new pipeline, with options to connect to code repositories like Azure Repos, Bitbucket Cloud, GitHub, and GitHub Enterprise Server.

Upon repository selection, configure your pipeline. Although pre-built templates (like ASP.NET) exist, choose the Starter Pipeline option for this example.

The image shows an Azure DevOps interface for configuring a new pipeline, with options for different project types like ASP.NET, Xamarin, and existing YAML files.

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.

The image shows a screenshot of the Azure DevOps interface, specifically the "Agent pools" section under "Project Settings," displaying an agent named "KodeKloudAgent1" that is currently idle.

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'

The image shows an Azure DevOps pipeline interface with a job that is currently queued. It includes details about the repository, branch, and trigger information.

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.

  1. 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).
  2. 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:

  1. Go to Settings > Secrets and variables > Actions in your GitHub repository.
  2. Click New repository secret and add the following:
    • Name: AZURE_DEVOPS_PROJECT_URL
      Value:
      https://dev.azure.com/jeremy0665/SimpleWebAPI
      
  3. Similarly, add a new secret for AZURE_DEVOPS_TOKEN containing your personal access token.

The image shows a GitHub repository settings page for managing "Actions secrets and variables," with a section displaying a repository secret named "AZURE_DEVOPS_PROJECT_URL."

The image shows a GitHub repository settings page for adding a new secret under "Actions secrets." It includes fields for the secret's name and value, with a button to add the secret.

Once added, the secrets will appear in the repository's secrets list:

The image shows a GitHub repository settings page, specifically the "Secrets and variables" section under "Actions," displaying two repository secrets related to Azure DevOps.

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.

The image shows an Azure DevOps pipeline run summary for a project called "SimpleWebAPI," indicating a successful job execution. The pipeline was triggered by a user and completed in 14 seconds.

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.

The image shows a GitHub Actions workflow summary for a file named `main.yml`, indicating a successful build with warnings about deprecated Node.js versions and commands.

The image shows an Azure DevOps pipeline interface for a project named "SimpleWebAPI," displaying recent pipeline runs with their statuses.

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

Previous
Demo Repository Integration with Azure Boards