Quickstart: Use GitHub Actions to push to Azure Artifacts

Azure DevOps Services

Get started using GitHub Actions and Azure Artifacts together. GitHub Actions help you automate your software development workflows from within GitHub. You can use GitHub Actions to deploy to an Azure Artifacts feed.

Prerequisites

Authenticate with Azure Pipelines

Use a personal access token (PAT) to connect your GitHub account to Azure DevOps. You can generate a PAT from within Azure DevOps and then store it as a GitHub secret. Within your GitHub workflow, reference the secret so that your GitHub action can authenticate with your Azure DevOps project.

  1. Open your GitHub repository and go to Settings.

  2. Select Security > Secrets and variables > Actions.

    Choose to add a secret

  3. Paste in your PAT and give it the name AZURE_DEVOPS_TOKEN.

  4. Select Add secret.

Create a GitHub workflow that builds an artifact

GitHub workflows are a series of actions (like tasks in Azure Pipelines). This workflow:

  • Sets up a .NET Core CLI environment with the setup-dotnet action.
  • Restores dependencies, builds the project and its dependencies into a set of binaries, and runs all unit tests associated with the project.
  • Packs the code into a NuGet package with the GitHub Run ID environmental variable included in the version number.
  • Publishes the NuGet package to Azure Artifacts.
  1. In your repository on GitHub, create a new YAML file in the .github/workflows directory.

  2. Copy the following contents into your YAML file. Customize the AZURE_ARTIFACTS_FEED_URL, BUILD_CONFIGURATION, and DOTNET_VERSION values.

    • Set AZURE_ARTIFACTS_FEED_URL to the registry url for your Azure Artifacts Feed.
    • Set the BUILD_CONFIGURATION.
    • Set DOTNET_VERSION to the version of your project.
    name: Push a NuGet package to Azure Artifacts or GitHub Package Registry
    
    on:
      push:
        branches:
          - main
    
    env:
      AZURE_ARTIFACTS_FEED_URL: https://pkgs.dev.azure.com/myorg/nuget-artifact/_packaging/Fabrikam_Feed/nuget/v3/index.json    
      BUILD_CONFIGURATION: 'Release'    # set this to the appropriate build configuration
      DOTNET_VERSION: '6.x' 
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          # Checkout the repo
          - uses: actions/checkout@v2
    
          # Setup .NET Core SDK
          - name: Setup .NET Core
            uses: actions/setup-dotnet@v1
            with:
              dotnet-version: ${{ env.DOTNET_VERSION }}
    
          # Run dotnet build and package
          - name: dotnet build and test
            run: |
              dotnet restore
              dotnet build --configuration '${{ env.BUILD_CONFIGURATION }}'
              dotnet test --configuration '${{ env.BUILD_CONFIGURATION }}'
    
      az-artifacts-build-and-deploy:
        needs: build
        runs-on: ubuntu-latest
        steps:
          # Checkout the repo
          - uses: actions/checkout@v2
    
          # Setup .NET Core SDK
          - name: Setup .NET Core
            uses: actions/setup-dotnet@v1
            with:
              dotnet-version: ${{ env.DOTNET_VERSION }}
              source-url: ${{ env.AZURE_ARTIFACTS_FEED_URL }}
            env:
              NUGET_AUTH_TOKEN: ${{ secrets.AZURE_DEVOPS_TOKEN }} 
    
          # Run dotnet build and package
          - name: dotnet build and publish
            run: |
              dotnet restore
              dotnet build --configuration '${{ env.BUILD_CONFIGURATION }}'
              dotnet pack -c '${{ env.BUILD_CONFIGURATION }}' --version-suffix $GITHUB_RUN_ID
    
          # Publish the package to Azure Artifacts
          - name: 'dotnet publish'
            run: dotnet nuget push --api-key AzureArtifacts bin/Release/*.nupkg 
    
  3. Go to your Azure Artifacts feed to verify that you see the package you pushed.

    Review new Azure Artifacts feed.

Clean up resources

If you're not going to continue to use your GitHub workflow, disable the workflow.

Next steps