How to Check Whether a Container is in Restarting Phase or not in GitHub Actions CI/CD pipeline


Introduction to Github Actions
In today’s fast-paced world of software development, efficiency and automation are key to staying ahead of the competition. GitHub Actions, a powerful and flexible workflow automation tool provided by GitHub, empowers developers to streamline their development processes and enhance collaboration within teams. In this article, we will explore the capabilities of GitHub Actions and highlight how it can revolutionize your development workflow.
Github actions is one of the ci/cd tool provided by github.designed to streamline and enhance the software development workflow. It allows you to automate various tasks, build, test, and deploy applications, and respond to events within your GitHub repositories.
Seamless Integration with GitHub: GitHub Actions seamlessly integrates with your existing GitHub repositories, making it incredibly easy to set up and use. It is built directly into the GitHub platform, eliminating the need for external CI/CD tools and simplifying your workflow management.With GitHub Actions, you can create custom workflows using YAML files to define a series of steps and actions to be executed. These workflows can be triggered by events such as code pushes, pull requests, issue updates, or even on a scheduled basis
Benefits of GitHub Actions:
Continuous Integration and Deployment: GitHub Actions enables you to automate build, test, and deployment processes, ensuring that your code is always in a deployable state and reducing the time and effort required for manual testing and deployment.
Enhanced Collaboration: With GitHub Actions, teams can work collaboratively on automating repetitive tasks, allowing developers to focus on code and innovation instead of manual processes.
Container status checking after deployment on EC2 server with the help of github actions workflow
Prerequisites: To follow along with this tutorial, you should have basic knowledge of GitHub Actions, Docker, and Amazon EC2. Ensure that you have a GitHub repository with a Dockerized application and access to an EC2 instance
Step 1: Set up your EC2 instance
Launch an EC2 instance and make sure it is accessible via SSH.
Install Docker on the EC2 instance by running the necessary commands.
Step 2: Configure GitHub Actions Workflow
In your GitHub repository, create a
.github/workflows
directory if it doesn't exist.Inside the
.github/workflows
directory, create a new YAML file (e.g.,deploy.yml
) to define your workflow.Define the workflow using the following template:
name: cxo-ui pipeline
on:
push:
branches:
- main
- mybranch
# env:
# MY_KEY: ${{ secrets.MY_KEY }}
# This workflow install dependency and deployment on s3 bucket
jobs:
CheckBranch:
name: Test Branch
runs-on: ubuntu-latest
# Setting build timeout .
timeout-minutes: 2
steps:
- name: Take manual approval for master branch
uses: trstringer/manual-approval@v1
with:
secret: ${{ github.TOKEN }}
approvers: Hrithikwmp , Hrithik50
minimum-approvals: 1
if: github.ref == 'refs/heads/main'
Check_container_status:
needs: [CheckBranch, Build_Deploy_Dev]
runs-on: ubuntu-latest
steps:
- name: Login into EC2 using SSH
env:
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
HOSTNAME: ${{ secrets.HOSTNAME }}
USER_NAME: ${{ secrets.USERNAME }}
run: |
echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
ssh -i private_key -o 'StrictHostKeyChecking=no' ${USER_NAME}@${HOSTNAME} "
if [ \"\$(docker inspect -f '{{.State.Restarting}}' <YOUR_CONTAINER_NAME>)\" == \"true\" ]; then
echo \"Container is restarting\"
exit 1
else
echo \"Container is running\"
fi "
- name: Slack Notification
uses: act10ns/slack@v1
with:
status: ${{ job.status }}
steps: ${{ toJson(steps) }}
channel: '#cicd-Hrithik'
if: github.ref == 'refs/heads/main
4. You have to add some value in your github secrets like:- PRIVATE KEY for login into ec2 server , ec2 server USERNAME, Ec2 server HOSTNAME, and SLACK WEBHOOK URL.
5. Replace <YOUR_CONTAINER_NAME>
with the actual name of your container.
6. Step 3: Test the Workflow
Commit and push the changes to your GitHub repository.
Navigate to the “Actions” tab in your repository on GitHub.
You should see your workflow listed. Click on it to view the workflow runs.
If everything is set up correctly, you will see a green checkmark indicating a successful run.
If the container is in the restarting phase, the workflow will exit code 1 with an error, and you will see a red cross indicating a failed run also a notification will come on slack with job status.
So, that’s from my side i hope you’ll like this tutorial , this automation will more helpful because , generally after deployment of services, we have to manually check that whether our container services are running smoothly or not by logging into Ec2 server.But, if somehow we forgot to check the container status and then our dev and QA team has been blocked due to failure of that container service, so, it also saves our time in debugging and fixing the issues in QA and production stage.
Thank you, i hope you like this tutorial or blog a lot , if it is then please share this with your fellow DevOps Engineers , who also faces some kind of situations and issues.
Source from: Hrithikhiranwar
Subscribe to my newsletter
Read articles from Sudharsan Reddy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
