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 workfl…


This content originally appeared on DEV Community and was authored by Ige Adetokunbo Temitayo

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.

Alt Text

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 thepacker.yaml file.

name: Run packer build on a template file

on:
  push:
    branches:
        - 'master'
jobs:
  packer_build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Packer build
        uses: ExitoLab/packer_build_action_aws@v0.2.10
        with:
          templateFile: 'ami.json'
          workingDir: 'ami'
          varFile: 'variables.json'
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: us-east-1

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.


This content originally appeared on DEV Community and was authored by Ige Adetokunbo Temitayo


Print Share Comment Cite Upload Translate Updates
APA

Ige Adetokunbo Temitayo | Sciencx (2021-07-13T21:56:22+00:00) Using GitHub Actions to build packer AMI on AWS. Retrieved from https://www.scien.cx/2021/07/13/using-github-actions-to-build-packer-ami-on-aws/

MLA
" » Using GitHub Actions to build packer AMI on AWS." Ige Adetokunbo Temitayo | Sciencx - Tuesday July 13, 2021, https://www.scien.cx/2021/07/13/using-github-actions-to-build-packer-ami-on-aws/
HARVARD
Ige Adetokunbo Temitayo | Sciencx Tuesday July 13, 2021 » Using GitHub Actions to build packer AMI on AWS., viewed ,<https://www.scien.cx/2021/07/13/using-github-actions-to-build-packer-ami-on-aws/>
VANCOUVER
Ige Adetokunbo Temitayo | Sciencx - » Using GitHub Actions to build packer AMI on AWS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/13/using-github-actions-to-build-packer-ami-on-aws/
CHICAGO
" » Using GitHub Actions to build packer AMI on AWS." Ige Adetokunbo Temitayo | Sciencx - Accessed . https://www.scien.cx/2021/07/13/using-github-actions-to-build-packer-ami-on-aws/
IEEE
" » Using GitHub Actions to build packer AMI on AWS." Ige Adetokunbo Temitayo | Sciencx [Online]. Available: https://www.scien.cx/2021/07/13/using-github-actions-to-build-packer-ami-on-aws/. [Accessed: ]
rf:citation
» Using GitHub Actions to build packer AMI on AWS | Ige Adetokunbo Temitayo | Sciencx | https://www.scien.cx/2021/07/13/using-github-actions-to-build-packer-ami-on-aws/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.