Officially, Workflow is prepared as starter-workflows from building Docker image to pushing to Github Container Registry, but since it is doing muddy without using actions, the Workflow configuration using actions is described in this article. I will describe it.
As for the file structure of the repository handled this time, it is assumed that the Dockerfile exists directly under the root.
--
|-- Dockerfile
|-- README.md
|-- .github
|-- workflows
|-- actions.yml
...
yml:.github/workflows/action.yml
name: Build and Publish Docker
on:
push:
branches:
- master
jobs:
build_and_push:
runs-on: ubuntu-latest
env:
IMAGE_NAME: sample-app
steps:
- name: checkout
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.CR_PAT }}
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: |
ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:latest
ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:1.0.0
In this workflow, a job called build_and_push
is defined.
From now on, I will briefly explain what each step in the job is doing.
checkout Check out the source code. ʻActions / checkout @ v2` We are using actions.
Set up Docker Buildx
The docker / build-push-action @ v2
action is doing the necessary setup for docker build
here.
I am using the docker / setup-buildx-action @ v1
action.
Login to GitHub Container Registry
Do a docker login
here to push the docker image to the GitHub Container Registry.
Use the docker / login-action @ v1
action.
Define the required parameters with with
.
Parameters | value |
---|---|
registry | Specify the Docker registry This time I will push to Github Container Registry ghcr.io SpecifyIf not specified, it will be Docker Hub |
username | Specify the user to push to the Docker registry This time, specify the repository owner name from the environment variable * 1 |
password | Specify the password or Personal Access Token to push to the Docker registry This time CF in secrets_Specify the Personal Access Token stored under the name PAT * 2 |
1: Github Actions provides some environment variables by default, and this time I got them from them. Reference: Environment variables --GitHub Docs
2: See here for how to set the secret. Encrypted secrets - GitHub Docs
Build and push
Do docker build
and docker push
.
I am using the docker / build-push-action @ v2
action.
Define the required parameters with with
.
Parameters | value |
---|---|
context | docker build Specify the path or URL when you doactions/checkout You can specify the path when using an actionThis time specify the subordinate of the checked out route If not specified, it will be Git context |
push | Specify true when pushing to Docker registry |
tags | Specify the tag to be attached to the image to be built Multiple tags can be specified This time latest and 1.0.Tag with 0 |
file | Specify the Dockerfile path to refer to If not specified ./Dockerfile BecomesThis parameter is not used this time, but it is used when referencing a Dockerfile other than directly under the root. |
Recommended Posts