What is GitHub Actions and GitHub Workflows?
GitHub Actions
GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.
GitHub Actions goes beyond just DevOps and lets you run workflows when other events happen in your repository. For example, you can run a workflow to automatically add the appropriate labels whenever someone creates a new issue in your repository.
GitHub provides Linux, Windows, and macOS virtual machines to run your workflows, or you can host your own self-hosted runners in your own data center or cloud infrastructure.
GitHub Workflows
A workflow is a configurable automated process that will run one or more jobs. Workflows are defined by a YAML file checked in to your repository and will run when triggered by an event in your repository, or they can be triggered manually, or at a defined schedule.
Workflows are defined in the .github/workflows directory in a repository, and a repository can have multiple workflows, each of which can perform a different set of tasks. For example, you can have one workflow to build and test pull requests, another workflow to deploy your application every time a release is created, and still another workflow that adds a label every time someone opens a new issue.
Let’s Start
1. Create Dockerfile
To create Dockerfile and add it to GitHub repository
- Create a DockerFile with
ARG
variable. - Create two branchs -
latest
.
Example:
1
2
ARG BUSYBOX_TAG
FROM busybox:${BUSYBOX_TAG}
2. Create Workflow
To create workflow file
- Create
.github/workflow
folder inside the GitHub repository. - Create
docker-image.yml
workflow file. - Copy and paste following code into
docker-image.yml
file.
Example:
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
name: Docker Image CI
on:
push:
branches: [ "latest" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
# Checkout a Git repository at a particular version
- name: Checkput
uses: actions/checkout@v3
# Set up Docker Buildx
- name: Docker Setup Buildx
uses: docker/setup-buildx-action@v3.0.0
# Login against a Docker registry
- name: Docker Login
if: github.event_name != 'pull_request'
uses: docker/login-action@v3.0.0
with:
username: ${{ secrets.HUB_USERNAME }}
password: ${{ secrets.HUB_ACCESS_TOKEN }}
# Extract metadata (tags, labels) for Docker
- name: Docker Metadata action
uses: docker/metadata-action@v5.0.0
with:
images: ${{ vars.IMAGE_NAME }}
# Build and push Docker images
- name: Build and push Docker images
uses: docker/build-push-action@v5.0.0
with:
build-args: BUSYBOX_TAG=${{ vars.BUSYBOX_TAG }}
context: .
file: ./Dockerfile
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64,linux/arm/v6,linux/arm/v7
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ vars.IMAGE_NAME }}:${{ vars.BUSYBOX_TAG }}
# Update a Docker Hub repository description from README.md
- name: Docker Hub Description
uses: peter-evans/dockerhub-description@v3.4.2
with:
username: ${{ secrets.HUB_USERNAME }}
password: ${{ secrets.HUB_ACCESS_TOKEN }}
repository: ${{ vars.IMAGE_NAME }}
short-description: Docker Image - My BusyBox
readme-filepath: ./README.md
3. Create Secrets and Variables
Secrets in GitHub workflow file
Name | Description |
---|---|
secrets.HUB_USERNAME | Username used to log against the Docker registry |
secrets.HUB_ACCESS_TOKEN | Password or personal access token used to log against the Docker registry |
Variables in GitHub workflow file
Name | Description |
---|---|
vars.IMAGE_NAME | Base name of Docker images |
vars.BUSYBOX_TAG | Tag of Docker images |
Create Repository Secrets and Variables
- Navigate to the main apge of repository.
- Under your repository name, click
Settings
. If you cannot see theSettings
tab, select the dropdown menu, then clickSettings
. - In the
Security
section of the sidebar, selectSecrets and variables
, then clickActions
.
Create Secrets
- Click the
Secrets
tab. - Click
New repository secret
. - In the
Name
field, type a name for your secret. - In the
Secret
field, enter the value for your secret. - Click
Add secret
.
Create Variables
- Click the
Variables
tab. - Click
New repository variable
. - In the
Name
field, type a name for your variable. - In the
Value
field, enter the value for your variable. - Click
Add variable
.
Create Environment Secrets and Variables
- Navigate to the main page of the repository.
- Under your repository name, click
Settings
. If you cannot see theSettings
tab, select the dropdown menu, then clickSettings
. - In the left sidebar, click
Environments
. - Click on the environment that you want to add a secret to.
Create Secrets
- Under
Environment secrets
, clickAdd secret
. - Type a name for your secret in the
Name
input box. - Enter the
value
for your secret. - Click
Add secret
.
Create Variables
- Under
Environment variables
, clickAdd variable
. - Type a name for your secret in the
Name
input box. - Enter the
value
for your secret. - Click
Add variable
.
4. Test Workflow
- Since workflow’s trigger is a push event on
latest
branch. - Push a commit on
latest
branch. - Under your repository name, click
Actions
. If you cannot see theActions
tab, select the dropdown menu, then clickActions
. - The commit message for the push that triggered the workflow will be visible.
- Once the workflow completed, click the commit message to check the status of workflow.
5. Check Docker Image on DockerHub
Now, the Docker image already push to your registry.
References
About Myself
Please reach out to connect with me via Linkedin.