GitHub:
GitHub is a web-based platform used for version control and collaboration. It allows developers to host and review code, manage projects, and build software alongside millions of other users. GitHub offers features like issue tracking, pull requests, and wikis to facilitate collaboration among teams.
GitHub Actions:
GitHub Actions is an automation tool provided by GitHub. It allows developers to automate various tasks such as building, testing, and deploying software directly within their GitHub repositories. This automation capability helps streamline development workflows and increases productivity by reducing the manual effort required for repetitive tasks.
Components of GitHub Actions
Workflow: Workflows are configurable processes defined by YAML files within the .github/workflows directory in a repository. Each workflow can execute one or more jobs, allowing for flexible automation tailored to specific tasks like testing or deployment.
Events: Events are actions or activities within a repository that trigger workflows. These events, such as push requests or pull requests, prompt GitHub Actions to respond accordingly, enabling automation based on repository activity.
Jobs: Jobs are sets of steps within a workflow executed under the same runner. These steps can include shell scripts or actions, allowing for the execution of various tasks within a workflow.
Action: Actions are applications for GitHub Actions that perform common tasks, reducing the need for repetitive code in workflow files. They streamline automation by providing pre-built functionality for tasks like testing or deployment.
Runner: Runners are servers responsible for executing workflows when triggered. Each runner can handle one task at a time, ensuring efficient execution of workflows across repositories.
Step-by-Step Creation of GitHub Action File
First of all we have the following in our Github Repo
let's understand the components of your Node.js application and Docker configuration:
server.js:
This file contains the code for a simple Express.js server.
It listens on port 3000 and responds with "I am Batman!" when accessed.
package.json:
It defines metadata about the Node.js application.
The
"start"
script runs the server usingnode server.js
.There's also a
"test"
script that currently echoes "No tests yet".
Dockerfile:
Starts from a Node.js 16 (LTS) base image.
Specifies the working directory as
/usr/src/app
.Copies
package*.json
files and installs dependencies using npm.Copies all application files into the image.
Defines the default command to run the application using
npm start
.
Create the
.github/workflows
Directory:- If it doesn't already exist, create a directory named
.github/workflows
at the root of your repository.
- If it doesn't already exist, create a directory named
Create the Action File:
- Inside the
.github/workflows
directory, create a file namedmain.yml
.
- Inside the
Copy YAML Content:
- Open the
main.yml
file and copy the following YAML content into it:
- Open the
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Change directory to project root
run: cd Docker
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Install dependencies
run: npm install
- name: Build Docker image
run: |
docker build -t bruno74t/simple-node .
docker tag bruno74t/simple-node:latest bruno74t/simple-node:v1
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD_SYMBOLS_ALLOWED }}
- name: Push Docker image with v1 tag
run: docker push bruno74t/simple-node:v1
Let's break down the GitHub Actions workflow in simpler terms:
Name of the Workflow:
- This line just gives a name to your workflow, in this case, "CI/CD Pipeline".
Trigger:
- This workflow is triggered whenever you push changes to the main branch of your repository.
Jobs:
- In this workflow, there's only one job named "build" that runs on an Ubuntu environment.
Steps within the Job:
Checkout code: This step checks out your repository code to be used in the workflow.
Change directory to project root: This step changes the current directory to the "Docker" folder within your project.
Set up Node.js: This step sets up Node.js environment with version 16.
Install dependencies: This step installs the dependencies required for your Node.js application using npm.
Build Docker image: This step builds a Docker image for your application. It creates an image with the tag "bruno74t/simple-node" and also tags it as "bruno74t/simple-node:v1".
Log in to Docker Hub: This step logs in to Docker Hub using provided credentials stored in GitHub secrets.
Push Docker image with v1 tag: This step pushes the Docker image with the "v1" tag to Docker Hub.
This how our directory looks like !
After making changes in your GitHub repository, such as updating code or modifying files, GitHub Actions automatically detects these changes when you push the code to the repository. Once the push is completed, GitHub Actions triggers workflows defined in your repository based on specified events, such as pushes to specific branches.
An in depth look at what is perfomed by Github-Actions
After initiating the GitHub Actions workflow, it progresses through various steps to execute tasks such as building Docker images and pushing them to DockerHub, which serves as our container registry. Once these tasks are completed successfully, you can verify the results by checking DockerHub.
Conclusion
GitHub Actions simplifies software development by automating tasks directly within GitHub repositories, we explored setting up a CI/CD pipeline for a Node.js application deployed with Docker containers.
Key Points:
GitHub Actions automates tasks like building, testing, and deploying software.
Workflows are defined using YAML files triggered by specific events.
Our workflow included steps to check out code, set up Node.js, install dependencies, build Docker images, and push images to Docker Hub.
GitHub Actions streamlines development, allowing developers to focus on writing code while automation handles the rest.
By embracing GitHub Actions, developers can accelerate their development cycles, improve productivity, and deliver software more efficiently.
Cheers ๐ป !