Voiced by Amazon Polly |
1. Introduction
GitHub Actions is a continuous integration and continuous delivery (CI/CD) tool offered by GitHub. It is open-source and one of the fastest-growing tools that allow you to automate your build, test, and releases. It is a fast deployment tool with well-founded security, less overhead, and makes you write workflows easily.
Let us assume that we want to create a workflow that gets triggered when an issue is raised. The workflow will contain multiple jobs in its virtual machine runner and performs one or more steps that you define or run an action. So what are these workflow, events, jobs ??
2. The Components of GitHub Actions
- Workflows: The workflow is an automated process that runs one or more jobs. You can define a workflow in a YAML file in your repository, and it will run when triggered by a specific event. We can have multiple workflows created in a repository to perform different roles.
- Events: Events are an activity that occurs in your repository that triggers the workflow, for example, pushing a commit, creating pull requests.
- Jobs: A job is a set of instructions executed on the runner. An instruction can be a script or an action that will run. Jobs will be executed in sequence or parallelly. We can also share data from one step to another since they are dependent on each other. The job dependencies can be handled with other jobs; by default, jobs are executed in parallel with no dependencies.
- Actions: It is a custom application that can be used to reduce the repetitive code in the workflow files. We can use the actions available in the GitHub marketplace and can also write our own actions.
- Runners: It is a backend server on which the workflow runs. Each workflow will be executed on a newly provisioned runner.
You can deep dive into the GitHub actions here
Customized Cloud Solutions to Drive your Business Success
- Cloud Migration
- Devops
- AIML & IoT
3. Example for Setting up a Workflow
Consider setting up a workflow that gets triggered when the code is pushed into the main branch. And the workflow jobs which automate the terraform to deploy Azure resources.
4. Prerequisites
- You need to have a GitHub account and Actions should be enabled
- Azure credentials
- Azure Tenant ID
- Azure subscription ID
- We need an Azure Service Principle with a Contributor role
- An azure Service principle’s ClientID and Secrets. Terraform will use this service principle to authenticate with Azure for creating resources
- I will be using an Azure storage account as my terraform backend in this setup, so I have created a resource group with a storage account and container.
- Terraform configuration files to create resources. Here, I am creating the azure resources, but you can deploy resources in any cloud of your choice.
5. Introduction to Terraform
Terraform is an Infrastructure as a Code tool that allows you to build and manage both cloud and on-premises resources in human-readable configuration files which you can share, reuse, and version. The configuration files are written in terraform HCL ( Hashicorp Configuration Language)
So Let’s begin the setup by creating a repository for our pipeline
- Login in to your GitHub account and create a repository
- Push your terraform code into the repository. Here is my terraform code, I have modules to create a Resource group, Azure Virtual Network, subnet, and a security group for the dev environment
- Next, add the service principle’s secrets to your repository. Go to Settings on your repository. Under Secrets, click on Actions, and add a New repository secret. Provide a name to your secret, add its value, and click Add secret. Repeat this process to add all the required credentials
- Now, we need to enable the GitHub Actions for our account, for that navigate to your repository, click Actions and search for terraform and click on the Configure. There are other third-party actions are also available here I’m going with Hashicorp.
- Once you click the Configure, you will be taken to the terraform.yml file, the workflow configuration file
- We need to make changes to the yaml file for our pipeline. Then, we need to set up the jobs to run the terraform code to deploy the Azure resources.
- In the yaml file, the below code snippet shows that the workflow will trigger when a push is made on the main branch.
12345name: 'Terraform'on:push:branches:- main
Next, I have defined the job and given the steps that a job has to perform.
Here terraform is the job that runs on ubuntu runner, and steps are the instructions which a job will perform. We can define multiple steps under a job. The secrets which are stored are referenced using the environment env variable, and the values are fetched from the Action’s secret during the execution.
12345678910111213jobs:terraform:name: 'Terraform'runs-on: ubuntu-latestenvironment: devsteps:# Checkout the repository to the GitHub Actions runner- name: Checkoutuses: actions/checkout@main# Install the latest version of Terraform CLI- name: Setup Terraformuses: hashicorp/setup-terraform@v112345678- name: Terraform Initrun: terraform initworking-directory: "./environments/dev"env:ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID}}ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID}}ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID}}ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET}} - Below is our final workflow configuration for our pipeline
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152name: 'Terraform'on:push:branches:- mainjobs:terraform:name: 'Terraform'runs-on: ubuntu-latestenvironment: devsteps:# Checkout the repository to the GitHub Actions runner- name: Checkoutuses: actions/checkout@main# Install the latest version of Terraform CLI- name: Setup Terraformuses: hashicorp/setup-terraform@v1- name: Terraform Initrun: terraform initworking-directory: "./environments/dev"env:ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID}}ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID}}ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID}}ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET}}- name: Terraform Formatrun: terraform fmt -check- name: Terraform Planrun:terraform plan -out terraform.plan -var-file=terraform.tfvars -lock=falseworking-directory: "./environments/dev"env:ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID}}ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID}}ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID}}ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET}}- name: Terraform Applyid: applyrun:terraform apply -var-file=terraform.tfvars -auto-approve -lock=falseworking-directory: "./environments/dev"env:ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID}}ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID}}ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID}}ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET}}
We can check the pipeline execution logs here
- Once the pipeline is succeeded, we can verify the azure resources created
6. Conclusion
With that, this is how we can integrate terraform with GitHub actions and provision our infrastructure with a single GitHub action workflow in multiple environments. GitHub actions let you run your workflows when an event occurs in your repository. To run your workflows, GitHub offers Linux, Windows, and Mac OS virtual machines, and you can also run your workflows in your data centers or on the cloud.
Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.
- Cloud Training
- Customized Training
- Experiential Learning
About CloudThat
CloudThat is the official Microsoft Gold Partner, AWS Advanced Consulting Partner, and Training partner helping people develop knowledge on the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
FAQs
1. Other than GitHub, what other version controls are supported?
ANS: – Bitbucket Cloud, Bitbucket Server, Github Enterprise, GitLab.com, GitLab CE, and EE are other supported version controls.
2. Can you bulk import the state of the current cloud subscription into Terraform state?
ANS: – terraform_import command can be used to import individual resources into your Terraform state, but there is not currently a bulk import tool. Here is a Step-By-Step Guide to Manage AWS Infrastructure on AWS using Terraform Cloud for free.
WRITTEN BY Rekha S
Comments