Candycrush Deployment On AWS EKS Using GitHub Actions
Candycrush Deployment On AWS EKS Using GitHub Actions
In today’s fast-paced world of software development, automation is the name of the game. GitHub
Actions is the ace up the sleeve of modern developers, enabling them to streamline their daily
workflows in practical and impactful ways. In this article, we’ll explore how GitHub Actions is making
a real difference in real-life scenarios.
From Continuous Integration (CI) and Continuous Deployment (CD) to code quality assurance and
security scanning, GitHub Actions brings automation to every aspect of the development process.
With custom workflows, enhanced collaboration, and release management, this tool empowers
developers to be more efficient, reliable, and productive. Discover how GitHub Actions is not just a
concept but a transformative solution in the daily lives of developers.
GitHub: https://github.com/Aj7Ay/Candycrush.git
To launch an AWS EC2 instance with Ubuntu 22.04 using the AWS Management Console, sign in to
your AWS account, access the EC2 dashboard, and click “Launch Instances.” In “Step 1,” select
“Ubuntu 22.04” as the AMI, and in “Step 2,” choose “t2.medium” as the instance type. Configure the
instance details, storage, tags, and security group settings according to your requirements. Review
the settings, create or select a key pair for secure access, and launch the instance. Once launched,
you can connect to it via SSH using the associated key pair.
Click “Roles”
Click “EC2”
Click “Next”
Click the “Search” field.
click Next
Type “Jenkins-cicd”
Select instance –> Actions –> Security –> Modify IAM role
NOTE: USE YOUR RUNNER COMMANDS (EXAMPLE CASE IAM USING MINE)
The command “mkdir actions-runner && cd actions-runner” is used to create a new directory called
“actions-runner” in the current working directory and then immediately change the current working
directory to the newly created “actions-runner” directory. This allows you to organize your files and
perform subsequent actions within the newly created directory without having to navigate to it
separately.
curl -o actions-runner-linux-x64-2.310.2.tar.gz -L
https://github.com/actions/runner/releases/download/v2.310.2/actions-runner-linux-x64-
2.310.2.tar.gz
./run.sh
Let’s close Runner for now.
Connect to your Ec2 instance using Putty, Mobaxtreme or Git bash and install docker on it.
newgrp docker
After the docker installation, we will create a Sonarqube container (Remember to add 9000 ports in
the security group).
<ec2-public-ip:9000>
Provide Login and password
login admin
password admin
Integrating SonarQube with GitHub Actions allows you to automatically analyze your code for quality
and security as part of your continuous integration pipeline.
Next, provide a name for your project and provide a Branch name and click on setup
On the next page click on With GitHub actions
This will Generate an overview of the Project and provide some instructions to integrate
Search for Secrets and variables and click on and again click on actions
It will open a page like this click on New Repository secret
Name: SONAR_TOKEN
Go back to the Sonarqube dashboard and copy the file name and content
sonar-project.properties
The content to add to the file is (copied from the above image)
sonar.projectKey=Tic-game
To do that click on Add file and then click on Create a new file
Here is the file name
name: Build,Analyze,scan
on:
push:
branches:
- main
jobs:
build-analyze-scan:
name: Build
runs-on: [self-hosted]
steps:
uses: actions/checkout@v2
with:
uses: sonarsource/sonarqube-scan-action@master
env:
cd actions-runner
./run.sh
Let’s click on Build and see what are the steps involved
Click on Run Sonarsource and you can do this after the build completion
Build complete.
Go to the Sonarqube dashboard and click on projects and you can see the analysis
3. Install Terraform.
The script automates the installation of these software tools commonly used for development and
deployment.
Script
#!/bin/bash
sudo apt update -y
/usr/bin/java --version
# Install Trivy
# Install Terraform
# Install kubectl
unzip awscliv2.zip
sudo ./aws/install
trivy --version
terraform --version
aws --version
kubectl version
node -v
java --version
EKS provision
cd Candycrush
cd Eks-terraform
terraform init
Validate the configuration and syntax of files
terraform validate
terraform plan
This step runs npm install to install Node.js dependencies. You can replace this with your specific
npm install command.
run: |
# Scanning files
This step runs Trivy to scan files. It scans the current directory (denoted by .) and redirects the output
to a file named trivyfs.txt.
If you add this to the workflow, you will get below output
Create a Personal Access token for your Dockerhub account
Go to docker hub and click on your profile –> Account settings –> security –> New access token
Search for Secrets and variables and click on and again click on actions
It will open a page like this click on New Repository secret
Let’s add our token also and click on the new repository secret again
Name
DOCKERHUB_TOKEN
Paste the token that you generated and click on Add secret.
– name: Docker build and push run: | # Run commands to build and push Docker images docker
build -t candycrush . docker tag candycrush sevenajay/candycrush:latest docker login -u ${{
secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_TOKEN }} docker push
sevenajay/candycrush:latest env: DOCKER_CLI_ACI: 1
This step builds a Docker image with specific build arguments and tags it. It also logs in to Docker
Hub using the provided credentials stored in secrets and pushes the Docker image.
If you run this job now you will get below output
Image is pushed to Dockerhub
DEPLOY
deploy:
needs: build-analyze-scan
This section defines another job named “deploy.” It specifies that this job depends on the successful
completion of the “build-analyze-scan” job. It also runs on a self-hosted runner. You should
replace self-hosted with the label of your self-hosted runner.
steps:
This step pulls the Docker image from Docker Hub, specified by sevenajay/tic-tac-toe:latest, which
was built and pushed in the previous “build-analyze-scan” job
This step runs Trivy to scan the Docker image tagged as sevenajay/tic-tac-toe:latest. You should add
the Trivy scan command here.
This step runs a Docker container named “ticgame” in detached mode (-d). It maps port 3000 on the
host to port 3000 in the container. It uses the Docker image tagged as sevenajay/tic-tac-toe:latest.
Output
output
ec2-ip:3000
Deploy to EKS
This step updates the kubeconfig to configure kubectl to work with an Amazon EKS cluster in the
region with the name of your cluster.
This step deploys Kubernetes resources defined in the deployment-service.yml file to the Amazon
EKS cluster using kubectl apply.
SLACK
Add the below code to the workflow and commit and the workflow will start.
- name: Send a Slack Notification
if: always()
uses: act10ns/slack@v1
with:
channel: '#git'
env:
This step sends a Slack notification. It uses the act10ns/slack action and is configured to run “always,”
which means it runs regardless of the job status. It sends the notification to the specified Slack
channel using the webhook URL stored in secrets.
Complete Workflow
name: Build,Analyze,scan
on:
push:
branches:
- main
jobs:
build-analyze-scan:
name: Build
runs-on: [self-hosted]
steps:
uses: actions/checkout@v2
with:
uses: sonarsource/sonarqube-scan-action@master
env:
run: |
env:
DOCKER_CLI_ACI: 1
deploy:
needs: build-analyze-scan
runs-on: [self-hosted]
steps:
if: always()
uses: act10ns/slack@v1
with:
channel: '#githubactions-eks'
env:
Deployed to EKS
Job completed.
Open the port in the security group for the Node group instance.
After that copy the external IP and paste it into the browser
output
Destruction workflow
name: Build,Analyze,scan
on:
push:
branches:
- main
jobs:
build-analyze-scan:
name: Build
runs-on: [self-hosted]
steps:
uses: actions/checkout@v2
with:
run: |
docker rm game
- name: Update kubeconfig
if: always()
uses: act10ns/slack@v1
with:
channel: '#githubactions-eks'
env:
Slack Notification
cd /home/ubuntu
cd Candycrush
cd Eks-terraform