Blog Post

Using GitHub Actions to build packer AMI on AWS


GitHub Actions is a very cool tool for automating CI/CD pipeline workflows or any routine task. Once the code resides in Github, automating tasks from using Github actions is achievable.

Github Actions allows Engineers to create a very simple workflow to automate code compilation and deployment. Github Actions is very easy to use and it makes deployment to production very easy and interesting.

Actions are defined in YAML files, which allows pipeline workflow to be triggered using any GitHub events like on creation of Pull Requests, on code commits, and much more

I recently developed, deployed my first github Action and published the action to GitHub Actions Marketplace. The Action build packer images on AWS. I wanted an action to build simple packer images and i decided to pick up the challenge.

Let’s get started with the GitHub Action

I will be describing how to use GitHub Action to build packer images on AWS.

Step 01: Navigate to the repository where you wish to implement the GitHub Action.

Step 02: Create a file `packer-build.yml` in the parent directory. The file will be created in .github/workflows/packer-build.yaml. See screenshot below

How to create GitHub Actions

Step 03: Copy and paste the script below in the`packer.yaml` file.

Code Explanation: The actions work with the GitHub Event trigger which is `push to master branch`. The input parameters are the; working directory and template file. The `workingDir` is defined as the directory where the packer template and var file reside. The `templateFile` contains the packer template file for building packer AMI. The access_key and secret_key are used for authenticating to AWS will be stored in GitHub secrets.

Step 04: Add your secrets `AWS_ACCESS_KEY` and `AWS_SECRET_KEY` in the Github secrets. Under your repository name, click Settings. In the left sidebar, click Secrets. See image below

Adding secrets in GitHub for GitHub Actions

Step 05: Kindly see below a complete example of the AMI template. This template installs Jenkins and other software. Also, check the complete workflow for building the AMI.

In conclusion, I hope to build more GitHub Actions for executing routine tasks and I totally enjoyed working on my first GitHub Action. It was a very exciting experience.

Original post (opens in new tab)