Cloud  

Set Up GitHub Actions for Azure Using Terraform

Github

What is GitHub?

GitHub is a web-based platform that helps developers store, manage, and collaborate on code using Git version control. It allows multiple developers to work on a project simultaneously, Also, it elps to track changes, and maintain code history.

What are GitHub Actions?

GitHub Actions is an automation tool that allows developers to create CI/CD (Continuous Integration/Continuous Deployment) pipelines. It helps automate various development tasks like testing, building, and deploying applications.

What is a GitHub Workflow?

A GitHub Workflow is a set of automated tasks defined in a YAML file. It runs when triggered by specific events, such as pushing code to a repository, creating a pull request, or setting up a schedule. Workflows help in automating repetitive tasks like testing, code analysis, and deployment.

What is a GitHub Runner?

A GitHub Runner is a virtual machine that executes jobs specified in a GitHub Actions workflow. GitHub provides both hosted runners (free-tier available) and self-hosted runners (custom machines for private execution).

If you want to learn more about GitHub Actions, please check out the official documentation.

Why Use GitHub Actions and Workflows?

GitHub Actions and Workflows help automate software development, making the process faster and easier. Here’s why they are useful.

  • Saves Time: Runs tests, builds, and deploys applications automatically.
  • Boosts Productivity: Reduces manual work, so developers can focus on coding.
  • Improves Teamwork: Helps teams work together smoothly without conflicts.
  • Flexible & Scalable: Can be customized to work with cloud platforms like Azure, AWS, and Google Cloud.

Step-by-Step Guide to Setting Up a GitHub Action with a Runner

Step 1. Create a GitHub Repository

  • Go to GitHub and log in.
  • Click on New Repository.
  • Enter a name (e.g., github-actions-demo).
  • Select Public or Private, and click Create Repository.
    Create Repository
    Code

Step 2. You need the host to deploy and run the GitHub Runner which is needed to execute the jobs of our workflow. So, for that we gonna create a new VM for the same.

Create an Azure Virtual Machine (VM)

  • Go to Azure Portal → Virtual Machines → Click Create → Virtual Machine.
  • Choose Basic Configuration.
    Virtual machine
    Review
    Cost VM
    Public key
  • Click Review + Create → Create.

Connect to the VM

Click on the VM instance in azure and there in the side navbar you can see connect option. So, using that option to connect with your VM.

Step 3. Set Up a GitHub Self-Hosted Runner.

By default, GitHub provides free hosted runners, but here we'll set up a self-hosted runner on your local machine.

1. Navigate to Runner Settings

  • Go to GitHub Repository → Settings → Actions → Runners.
  • Click New self-hosted runner.
    Runner settings
    Window

2. Choose the Operating System: Select your OS (Windows, macOS, or Linux).

3. Download and Configure the Runner: To download and configure the GitHub Runner. First, you have to connect with the VM that we created previously, and once the connection is successful, then you can execute the commands which are visible on the runner creation page.

Follow the provided commands.

mkdir actions-runner && cd actions-runner

curl -o actions-runner-linux-x64.tar.gz -L \
  https://github.com/actions/runner/releases/download/v2.308.0/actions-runner-linux-x64-2.308.0.tar.gz

tar xzf ./actions-runner-linux-x64.tar.gz

4. Configure the Runner: Use the provided command with your repository’s token.

./config.sh --url https://github.com/your-username/github-actions-demo --token YOUR_TOKEN

5. Start the Runner

./run.sh

Github Action

Your self-hosted runner is now active and ready.

Step 4. Create the Terraform Workflow in GitHub Actions.

In your GitHub repository, create a file.

.github/workflows/terraform-azure.yml

Add the following workflow file.

Note. You might get some errors due to unavailablity of az cli and node on your VM. So, please install those things one by one and restart the runner.

name: Terraform Azure Deployment
on:
  push:
    branches:
      - main
jobs:
  terraform:
    runs-on: self-hosted  # Runs on your Azure VM runner

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Authenticate with Azure using Managed Identity
        run: az login --identity --username <MANAGED_IDENTITY_CLIENT_ID>

      - name: Install Terraform
        uses: hashicorp/setup-terraform@v3
        with:
          terraform_version: 1.6.0

      - name: Terraform Init
        run: terraform init
        working-directory: ./terraform

      - name: Terraform Plan
        run: terraform plan
        working-directory: ./terraform

      - name: Terraform Apply
        if: github.ref == 'refs/heads/main'
        run: terraform apply -auto-approve
        working-directory: ./terraform

Step 5. Create Terraform Code to Deploy an Azure Resource.

In your GitHub repository, create a folder named terraform.

Inside the terraform folder, create a file.

main.tf

Add the following Terraform code to create a Storage Account in your existing resource group.

Note. Please add your VM's managed identity inside the existing resource group with contributor rights.

provider "azurerm" {
  features {}

  use_msi         = true  # Enables Managed Identity Authentication
  subscription_id = "YOUR_AZURE_SUBSCRIPTION_ID"
}

resource "random_string" "suffix" {
  length  = 6
  special = false
  upper   = false
}

resource "azurerm_storage_account" "example" {
  name                     = "mystorage${random_string.suffix.result}"
  resource_group_name      = "jaydeep-rg"
  location                 = "East US"
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

Step 6. Push the Changes and Deploy.

Add and commit your Terraform code.

git add .
git commit -m "Added Terraform for Azure Storage Account"
git push origin main

Go to GitHub Actions → See the workflow running on your self-hosted runner.

Update

Once completed, check the Azure Portal → Resource Group to confirm the new Storage Account is created.

Azure portal

Conclusion

GitHub Actions and Workflows simplify CI/CD automation for Terraform deployments. By following the steps above, you can easily set up an automated Terraform workflow to manage Azure resources.