GitHub ActionsでDocker RegistryにDockerイメージをpushする(latestタグ、GitHub Release連携でバージョン付け)
上の記事のWorkflowテンプレートをちょっと改良した。
- Docker Hub以外のDockerレジストリに対応
- GitHub Release作成時にリリースタグをイメージタグにしてpush
レジストリURL
- Docker Hub:
docker.io
- GitHub Container Registry:
ghcr.io
- GitLab Container Registry:
registry.gitlab.com
GitHub Secrets
- DOCKER_USERNAME
- DOCKER_TOKEN
GitHub Workflow .github/workflows/docker.yml
name: Push to Docker registryon:push:branches:- mainrelease:types:- createdenv:IMAGE_NAME: docker.io/username/imagenameIMAGE_TAG: ${{ github.event.release.tag_name != '' && github.event.release.tag_name || 'latest' }}jobs:push:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v2- name: Setup Docker Buildxid: buildxuses: docker/setup-buildx-action@v2- name: Login to Docker Registryuses: docker/login-action@v2with:registry: docker.iousername: ${{ secrets.DOCKER_USERNAME }}password: ${{ secrets.DOCKER_TOKEN }}- name: Build and Deploy Docker imageuses: docker/build-push-action@v3env:IMAGE_NAME_AND_TAG: ${{ format('{0}:{1}', env.IMAGE_NAME, env.IMAGE_TAG) }}with:context: .builder: ${{ steps.buildx.outputs.name }}file: ./Dockerfilepush: truetags: ${{ env.IMAGE_NAME_AND_TAG }}cache-from: type=registry,ref=${{ env.IMAGE_NAME_AND_TAG }}-buildcachecache-to: type=registry,ref=${{ env.IMAGE_NAME_AND_TAG }}-buildcache,mode=max