Test, build, and push your Docker image to GitHub Container Registry using GitHub Actions

What to prepare

--Github repository --Dockerfile in the repository

basic configuration

Creating a Token for Github Actions

Create PAT from Github settings, location:

Github -> Settings -> Developer settings -> Personal access tokens

Privileges: Check write: packages`` read: packages

Save Token

Click New Secrets in Settings-> Secrets in the repository:

Creating Github Actions

Creating Workflow

docker-publish.yml


name: Docker

on:
  push:
    # `master`Branch`latest`Build as
    branches:
      - master

    #version`v1.2.3`Build with tag released
    tags:
      - v*

  #Test if you have a PR
  pull_request:

env:
  # TODO:The name of the Docker image
  IMAGE_NAME: author/xxxx

jobs:
  #test
  #Commentary: https://docs.docker.com/docker-hub/builds/automated-testing/
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Run tests
        run: |
          if [ -f docker-compose.test.yml ]; then
            docker-compose --file docker-compose.test.yml build
            docker-compose --file docker-compose.test.yml run sut
          else
            docker build . --file Dockerfile
          fi
  #Push the Docker image to GitHub Packages
  #Commentary: https://docs.docker.com/docker-hub/builds/
  push:
    #Check the test
    needs: test

    runs-on: ubuntu-latest
    #Run when not PR
    if: github.event_name == 'push'

    steps:
      - uses: actions/checkout@v2

      - name: Build image
        run: docker build . --file Dockerfile --tag $IMAGE_NAME

      - name: Log into GitHub Container Registry
      #I set it to Secrets here`CR_PAT`Using ghcr.Log in to io
        run: echo "${{ secrets.CR_PAT }}" | docker login https://ghcr.io -u ${{ github.actor }} --password-stdin

      - name: Push image to GitHub Container Registry
        run: |
          IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
          #Make the name of the set Docker image lowercase
          IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
          #Processing version characters
          VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
          #From the tag"v"Remove
          [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
          #If it's the master branch`latest`Tag
          [ "$VERSION" == "master" ] && VERSION=latest
          echo IMAGE_ID=$IMAGE_ID
          echo VERSION=$VERSION
          docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
          docker push $IMAGE_ID:$VERSION

If you commit this file, it should automatically run once, the log will be visible under Actions in the repository. This Actions also supports docker-compose.

The actual application is Please refer to the OVaaS Front-End repository currently under development: https://github.com/OVaaS/ovaas-front

the term

Recommended Posts

Test, build, and push your Docker image to GitHub Container Registry using GitHub Actions
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
Docker push to GitHub Container Registry (ghcr.io)
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
[GCP] Until you push the local Docker image to the Container Registry
Push the image to docker hub using Jib
Push the Docker Image distributed by Docker Hub to Google Container Registry and start the VM based on that Image
Push Docker images from GitHub Actions to GitHub Container Registry
[Personal notes] How to push a Docker image to GitHub Packages
2. Create Docker image and register Registry
How to build CloudStack using Docker
Use Jenkins to build inside Docker and then create a Docker image.
Publish Docker Image on GitHub Package Registry
Migrate Dockerhub images to Github Container Registry
Enable Docker build cache on GitHub Action and deploy to Amazon ECS
Quick build maven project using maven docker container
Docker container build fails to install php-radis
How to give your image to someone with docker
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
[Road _node.js_1-1] Road to build Node.js Express MySQL environment using Docker
Easily build Redmine on Windows using WSL2 and Docker
[Docker] Build an Apache container on EC2 using dockerfile
Try to build a Java development environment using Docker
I tried to build an environment using Docker (beginner)
How to test including images when using ActiveStorage and Faker
[Docker] Build a site on Hugo and publish it on GitHub
Register your own Docker image with ECR using AWS CLI
[Memo] Commands used when using Docker (stop container, delete, delete image)
Build Docker Image lightweight and fast with CodeBuild with Santa Banner
I tried to build the environment little by little using docker
Build a Node-RED environment with Docker to move and understand
Getting Started with GitHub Container Registry instead of Docker Hub
Easily convert Java application to Docker container with Jib ~ Build with gradle and register in local repository