Deploy Azure Automation Runbooks via GitHub Actions

Raymond Raymond event 2023-01-01 visibility 670
more_vert

Code description

This code snippet shows how to use deploy Azure Automation PowerShell runbooks using GitHub Actions workflow.

Automation Runbooks can be used to run scripts regularly including PowerShell workflows, Python scripts, etc.

If you have multiple scripts to deploy, you can create a PowerShell script file to deploy.

The following assumptions are made:

  1. The PowerShell runbook script (myrunbook.ps1) is located in the specified folder (scripts/runbooks).

  2. The runbook is PowerShell type.

  3. GitHub Actions secrets are setup to use OIDC to authenticate with Azure.

  4. The resource will be deployed into automation account named myautomationaccount under resource group 'rg'.

Code snippet

# This workflow deploys Azure automation runbooks into Azure.

name: Deploy Kontext automation runbooks

on:
  push:
    branches: ["master"]
    paths:
      - "scripts/automation/**"
  workflow_dispatch:

permissions:
  contents: read
  id-token: write

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      # Login to Azure
      - uses: azure/login@v1
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
          enable-AzPSSession: true 

      # Deploy runbooks
      - name: Deploy Azure runbooks
        uses: azure/powershell@v1
        with:
          inlineScript: |
            pushd ./scripts/runbooks
            Import-AzAutomationRunbook -Path "myrunbook.ps1"  -Name myrunbook -Type PowerShell -AutomationAccountName 'myautomationaccount' -ResourceGroupName 'rg' -Force
             Publish-AzAutomationRunbook -Name myrunbook -AutomationAccountName 'myautomationaccount'  -ResourceGroupName 'rg'
            popd
          azPSVersion: "latest"

      # Logout
      - name: logout
        run: |
          az logout
More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts