Minimal Workflow to push Docker image to Github Container Registry with Github Actions

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.

Premise

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
...

Solution

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

Workflow explanation

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 Registryghcr.ioSpecify
If 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

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 buildSpecify the path or URL when you do
actions/checkoutYou can specify the path when using an action
This 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./DockerfileBecomes
This parameter is not used this time, but it is used when referencing a Dockerfile other than directly under the root.

reference

Recommended Posts

Minimal Workflow to push Docker image to Github Container Registry with Github Actions
Steps to push Docker image to GitHub Container Registry (ghcr.io)
Push Docker images from GitHub Actions to GitHub Container Registry
Test, build, and push your Docker image to GitHub Container Registry using GitHub Actions
[GCP] Until you push the local Docker image to the Container Registry
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
[Personal notes] How to push a Docker image to GitHub Packages
Getting Started with GitHub Container Registry instead of Docker Hub
Publish Docker Image on GitHub Package Registry
Migrate Dockerhub images to Github Container Registry
Push the Docker Image distributed by Docker Hub to Google Container Registry and start the VM based on that Image
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
Setting to exit from Docker container with VScode
Update container image with KUSANAGI Runs on Docker
Push the image to docker hub using Jib
How to push an app developed with Rails to Github
I want to push an app made with Rails 6 to GitHub
Wait for the container service to start with docker healthcheck
Check all container image licenses in private registry with tern
Until you push to Github
GitHub Actions Introduction to self-made actions
Introduction to Linux Container / Docker (Part 1)
How to push from Tarminal to GitHub
Migrate Docker image to another server
Introduction to Linux Container / Docker (Part 2)
2. Create Docker image and register Registry
Restart apache with docker php-apache image
Pass environment variables to docker container
[Docker] Operation up to container creation # 2
[Linux] Start Apache container with Docker
Update MySQL from 5.7 to 8.0 with Docker
How to start Camunda with Docker
Build WebRTC Janus with Docker container
How to start a Docker container with a volume mounted in a batch file
Create a container image for arm64 of Kibana and register it in GitHub Container Registry. Start Elastic Stack with Docker Compose on Raspberry Pi 4 (64bit)