After setting up the git repository for my backend infrastructure (read more), it was time to automate the stack deployment as a CI/CD
pipeline for my website. I don't claim any expertise in this matter, however this article guided me.
GitHub Actions makes it easy to automate all your software workflows and deploy your code right from GitHub.
After a lot of search I finally found a git action (Refer) that worked for me! It integrated with my
template.yml and deployed the stack after a few configurations to the files. So I went ahead and created my own action after learning.
- I setup my
.git/actions/sam/packagefolder as below. This particular folder includes three files action.yaml, a dockerfile and a entry-point script for the dockerfile.
┃ ┣ 📂actions
┃ ┃ ┗ 📂sam
┃ ┃ ┃ ┗ 📂package
┃ ┃ ┃ ┃ ┣ 📜action.yml
┃ ┃ ┃ ┃ ┣ 📜Dockerfile
┃ ┃ ┃ ┃ ┗ 📜entrypoint.sh
┃ ┗ 📂workflows
┃ ┃ ┗ 📜ci.yml
┃ ┣ 📜app.py
┃ ┗ 📜init.py
┃ ┣ 📜test_handler.py
┃ ┗ 📜init.py
Let’s take a look at
- The action has two jobs
test- checks the integrity of my lambda function.
deploy- deploys the entire SAM stack in AWS and needs
testto be successful as a dependency. The workflow is triggered on every push to the repository.
jobs: test: delpoy: needs:test
Test job :
The test job uses
ubuntu-latestimage. Installs python 3. Uses two standard actions in here - actions/checkout@v1 and actions/setup-python@v1. Installs dependencies such as
pipand installs the dependencies from
requirements.txtfile we generated during our local testing (read more). Then it runs the test using
Deploy job :
testjob is successful, only then
jobsdetermine the actions that take place during the workflow, and in what order. The job uses
ubuntu-latestimage. The uses statement defines a particular action that we want to use. There is a standard action in here - actions/checkout@v1 and uses
./.github/actions/sam/packageas the path to my action files.
The workflow uses files present in
The action.yaml file details metadata about the particular action. It uses the image defined in
I configure my dockerfile to build a docker image
alpine:latest and run the
GLIBC_VER=2.31-r0' environment on it and setup
awscliv2 on it. I had to adds glibc and then removes some stuff.
Copy the code file from my action repository to the filesystem path
/ of the container
COPY entrypoint.sh /entrypoint.sh(Refer). However, I was facing continuous
Permission denied exception so had to add the below command:
RUN ["chmod", "+x", "/entrypoint.sh"]. (Refer).
Most of the work is done by this file in the docker image.
It is the same code as used in this action (Refer - falnyr/aws-sam-deploy-action). The script basically configure an AWS profile with my credentials, and
deploys the SAM template I developed previously in
template.yml file (read more).
The below parameters have
been passed as environment variables for security purposes.
C:\Users\USER_NAME\git-komlalebu-sam>git add . C:\Users\USER_NAME\git-komlalebu-sam>git commit -m master C:\Users\USER_NAME\git-komlalebu-sam>git push origin master
Completion status of the two jobs.
The stack primarily consists of an API, a lambda function and a DynamoDB table.