DEV Community

Cover image for Automating Azure infrastructure With GitHub Actions and Terraform.
Christiana
Christiana

Posted on

Automating Azure infrastructure With GitHub Actions and Terraform.

Introduction to GitHub Actions

Git actions is a continuous integration and continuous delivery platform that allows us to build, test, deploy our tasks. It is an automation tool.

Practical Section

Step 1: Create a private GitHub repository

  • Go to your GitHub and create a private repository

Step 2: Generate a workflow token in GitHub

  • Go to your GitHub and click on your profile
  • Scroll down and click on settings
  • Scroll down and click on developers by your left
  • Click on personal access token drop down and select tokens (classic)

  • Click on generate tokens

  • Name your token
  • Scroll down and enable workflow

  • Scroll down and click on generate tokens
  • Before you exist that page, make sure you copy your token

Step 3: Create a file

  • Create a file on your laptop
  • Right click on it and open it with VS code
  • Create a backend.tf file
  • Create a HCP terraform account, create an organization and workspace. Then copy the backend code

  • Go back to your VS code and paste it in the backend.tf file and do the necessary editing

Step 4: Let's push this to our GitHub repository

  • After doing the above, type git init
  • Type git add .
  • Type git commit -m "azure backend file added"
  • Type git remote add origin https://your GitHub username:your workflow token@github.com
  • Type git push -u origin main

Step 5: GitHub Action

Firstly, we will install the code pipeline in our repository

  • Go to your GitHub repository, click on Action at the top
  • Click on terraform configure (because we want to use terraform)
  • Go back to your VS code and create a folder. Let's name it .github, workflows
  • Click the workflows folder and create a file under it. Let's name it cicd.yml
  • Go back to your GitHub terraform configure and copy from name: terraform to the end
  • Click on the cicd.yml on your VS code and paste those things you copied
  • Start editing it
  • Change the name to terraform infrastructure deployment to Azure cloud (or anything)
  • Remove pull_request under branch ["main"]
  • Remove permission: and contents:read
  • Name: under jobs, put terraform azure cloud
  • Go to your GitHub repository
  • Click settings on top of it
  • Click on secret and variables
  • Click actions
  • Click on secret, and click on create new repository
  • Let's name it TF_API_TOKEN
  • Go to your HCP terraform, create a new token in your HCP terraform, name it git actions pipeline token
  • Copy the token
  • Go to the git actions secret you were creating in GitHub and paste the token you created in HCP terraform
  • Click add
  • go to your VS code, remove terraform format and terraform fmt-check
  • Scroll down and remove if github.ref==
  • type git add.
  • type git commit -m "pipeline code added"
  • type git push

  • This is what it should look like

Top comments (0)