Setup CI/CD Workflow with Github Actions to AWS
Introduction
After a lot of time not publishing new content to my personal blog which I intent to store some of the work that I’ve done and learned throught the years, now finally it is time to get back to it and post some more content. If some how you are wondering why I stopped publishing more content it is very simple, I became father for the second time and I’ve been very busy with my new born :P .
Now for this article lets try to address the following:
First lets talk about Github Actions, this is an Integration Delivery tool very similar to bitbucket pipelines, jenkins, aws codedeploy, etc. It is very useful if you have your code stored in Github and you don’t want to use other services to deploy your code to AWS for instance.
Get to the point
Github Actions uses workflow files in YML format to handle all the steps that you want to do but lets sat you need to setup a Github Actions to handle CI/CD to more than 1 branch and you don’t want to use more than 1 file in order to have a simple file that handles both scenarios.
We are going to use branches stage and main for this excercise.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
name: Autodeploy to AWS
on:
workflow_dispatch:
inputs:
releaseType:
description: "Where to release (stage or prod)?"
required: true
default: "stage"
push:
branches:
- stage
release:
types: [published]
env:
AWS_REGION: $
AWS_ACCESS_KEY_ID: $
AWS_SECRET_ACCESS_KEY: $
ECR_REPOSITORY: $
DOCKER_IMAGE_NAME: $
ECS_SERVICE: $
ECS_CLUSTER: $
ECS_TASK_DEFINITION: $
CONTAINER_NAME: $
ENVIRONMENT: $
jobs:
set-env-variable:
runs-on: ubuntu-latest
outputs:
environment: $
steps:
- name: Set Environment Variable
id: set-env
run: |
if [ "$" == "stage" ]; then
echo "ENVIRONMENT=stage" >> $GITHUB_OUTPUT
elif [ "$" == "prod" ]; then
echo "ENVIRONMENT=prod" >> $GITHUB_OUTPUT
elif [ "$" == "push" ]; then
echo "ENVIRONMENT=stage" >> $GITHUB_OUTPUT
elif [ "$" == "release" ]; then
echo "ENVIRONMENT=prod" >> $GITHUB_OUTPUT
else
echo "ENVIRONMENT=stage" >> $GITHUB_OUTPUT
fi
- name: Use Variable
run: |
echo "The environment is: $"
build-and-publish:
needs: set-env-variable
runs-on: ubuntu-latest
environment: $
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: "Create env file"
run: |
touch .env
cat <<EOF > .env
$
EOF
cp .env public/.env
- name: Setup Docker
uses: docker/setup-buildx-action@v1
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: $
aws-access-key-id: $
aws-secret-access-key: $
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REPOSITORY: $
IMAGE_TAG: $
run: |
docker build -t $ECR_REPOSITORY:$IMAGE_TAG .
docker tag $ECR_REPOSITORY:$IMAGE_TAG $ECR_REPOSITORY:latest
docker push $ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REPOSITORY:latest
echo "image=$ECR_REPOSITORY:latest" >> $GITHUB_OUTPUT
- name: Deploy to ECS
run: |
aws ecs update-service --region $ --cluster $ --service $ --force-new-deployment
:wink:
I Hope you have enjoyed this post and if it is useful to you please invite me a coffee to keep posting more…