GitHub Actions to Build and Push Docker Images
GitHub Actions are use to Automate, customize, and execute software development workflows right in our repository. There are list of actions are available in Github marketplace. In GitHub, We can specify actions to be triggered by certain events, such as pushes to a repository, pull requests, or the creation of a new issue.
Here we are going to see the github action to build and push docker image on the event of merge.
To set up GitHub Actions to build and push Docker images, you will need to create a workflow file in our repository. The workflow file defines the steps that should be taken when the action is triggered.
Here’s an example of a workflow file that builds and pushes a Docker image using GitHub Actions:
name: Build and push Docker image
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build and push image
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: your-username/your-repository
tag_with_sha: true
In this example, the action is triggered whenever a push is made to the repository. The action runs on an Ubuntu virtual machine and uses the docker/build-push-action
to build and push the Docker image to a registry. The username and password for the registry are stored as secrets in the repository.
To use this action, we will need to create a file named main.yml
in the .github/workflows
directory of our repository and copy the code above into the file. We will also need to replace your-username
and your-repository
with our own Docker registry username and repository name, and set the DOCKER_USERNAME
and DOCKER_PASSWORD
secrets in our repository settings.
Hope this explanation helps to understand how we can automate the docker image push workflow on the event of merge event. Thanks.