Automatically Build and Push Docker Image with GitHub Action
1 Introduction
In this article, we will introduce how to use GitHub Action to automatically push Docker images to a registry, greatly simplifying the tedious process of building and pushing images! We have introduced many cool features of GitHub before. To facilitate the understanding of the content of this article, we recommend reviewing the basic GitHub operation knowledge in the previous article, especially GitHub Action:
- Teaching You to Use GitHub with One Line of Code
- Git Common Commands Overview
- Create a Beautiful Online Resume with GitHub
- Overview of GitHub Action
2 Configure the Image Registry
Here we take Aliyun's image registry as an example for demonstration. The principles of other image registries are similar and can be applied by analogy. First, log in to the Aliyun image registry, and perform the following operations:
- Create a namespace as a collection of image repositories, named after the company or organization. We use
bullettech_services
. - Create an image repository as a collection of images that can store different versions of images in the repository.
3 Configure the GitHub Action
3.1 Configure the Password
Set a password in the GitHub repository for logging in to the image registry. You can find the password in the repository settings and then store the login name and password of the image registry service.
3.2 Create the Workflow
First, create a workflow in the .github/workflows
directory, such as ci.yml
, and understand the commands based on the comments, and modify them according to the project situation.
name: actions
on: [push, pull_request] # Trigger Event
jobs:
bt-product-release:
if: ${{ github.ref == 'refs/heads/main' }} # Check if the main branch is updated
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2 # pull the code to the running server
- name: Login to Aliyun Container Registry (ACR)
uses: aliyun/acr-login@v1 # Use the Aliyun Image Service Action
with:
login-server: registry.cn-shanghai.aliyuncs.com # Be sure to correctly fill in the login address of the image registry service
region-id: cn-shanghai
username: "${{ secrets.REGISTRY_USERNAME }}" # Reference the username of the image registry service set in GitHub repo
password: "${{ secrets.REGISTRY_PASSWORD }}" # Reference the password of the image registry service set in GitHub repo
- name: Build and Push Docker Image
env:
IMAGE_TAG: ${{ github.sha }} # Used to mark the container version number
run: |
docker build -t registry.cn-shanghai.aliyuncs.com/bullettech_services/app:$IMAGE_TAG .
docker push registry.cn-shanghai.aliyuncs.com/bullettech_services/app:$IMAGE_TAG
This way, every time the main branch is updated, GitHub will build the image based on the updated code, and push the image to the designated image repository (pay attention to the version):
4 Conclusion
This efficient workflow saves a lot of time and avoids many errors that are prone to occur during manual operations. GitHub Action is so awesome!
I hope this sharing will help you, and welcome to leave a message in the comments to discuss!