preface
This article explains how to use GitHub Actions with Docker for continuous integration workflow during development deployment.
You will need to have some basic knowledge of Dockerfile before you follow this article. You can learn from my previous article: The Docker Rapid Deployment Node Express project
As for why GitHub Actions were used, here’s the official quote:
Automate, customize, and execute software development workflows in the GitHub Actions repository. You can discover, create, and share actions to perform any job you like (including CI/CD) and merge the actions into a fully customized workflow.
Create Actions file
Of course, GitHub Actions are based on git repositories. In your project’s Git repository, select the Actions menu and click Set Up a Workflow Yourself -> Automatically create yML files.
You can also manually create the.github/workflows directory under the project root in the IDE and create a name.yml file.
Select workflow Templates
Our current job is to update code + build image + submit image + server pull image + start container. GitHub Actions is not recommended for handling server content, so let Actions automate the first few steps.
Related to Docker, looking at Docker Actions in Git Marketplace, I found a workflow template Build and Push Docker images that fits very well with us
Go into the template details and copy the code into the name.yml file and modify it.
When branch triggers push (push, merge Pull Request) and tag is image.xxx, a workflow is triggered. Jobs: # Job 1 name build: # Specifies the runtime environment runs-on: Ubuntu-latest # Job contains a sequence of tasks called steps steps Actions /checkout@v2 # setup-qemu Action is useful if you want to use qemu to add emulation support so that you can build for more platforms - name: Set up qemu uses: Docker /setup-qemu-action@v1 # setup-buildxAction will create and boot the builder using the Docker-Container builder driver by default. Optional - name: Login to Ali Docker uses: Docker /setup-buildx-action@v1 # Login to Ali Docker uses: Github Settings -> secrets set with: registry: ${{ secrets.ALI_DOCKER_HUB_REGISTRY }} username: ${{ secrets.ALI_DOCKER_HUB_USN }} password: ${{secretsecrets.ALI_DOCKER_HUB_PWD}} ${{secretsecrets.ALI_DOCKER_HUB_PWD}} docker/build-push-action@v2 with: context: . push: true tags: registry.cn-shenzhen.aliyuncs.com/xxxx/image-name:v1Copy the code
Viewing workflow Logs
Once the workflow is triggered, the logs can be viewed in GitHub Actions.
Green √ indicates completion and success.
Deploying to the server
Enter the server terminal and start the Docker container
Stop docker-name docker rm docker-name # pull the latest image docker pull Registry.cn-shenzhen.aliyuncs.com/xxxx/image-name:v1 # start the container docker run - d - p 3000:3000 - name = docker - name registry.cn-shenzhen.aliyuncs.com/xxxx/image-name:v1Copy the code
Thank you
Making Actions document: docs.github.com/cn/actions/…
Benny tech blog full of dry goods: www.benny.wiki
Reprint statement: please indicate the author, mark the original link, if you have questions, send to [email protected]