Docker Build GitHub Actions
GitHub Actions はビルド、テスト、デプロイの連携作業を自動化する、一般的な CI/CD プラットフォームです。 Docker では、開発ワークフロー内で利用可能な GitHub Actions を公式に提供しています。 この Actions は、イメージのビルド、アノテーション、プッシュを行うための、再利用可能で使い方も簡単なコンポーネントです。
以下のような GitHub Actions が利用できます。
- Build and push Docker images: BuildKit を利用して Docker イメージのビルドとプッシュを行います。
- Docker Buildx Bake: Bake を使った高度なビルドを可能にします。
- Docker Login: Docker レジストリへのサインインを行います。
- Docker Setup Buildx: BuildKit ビルダーの生成と起動を行います。
- Docker Metadata action: Git リファレンスや GitHub イベントからメタデータを抽出し、タグ、ラベル、アノテーションを生成します。
- Docker Setup Compose: installs and sets up Compose.
- Docker Setup Docker: installs Docker Engine.
- Docker Setup QEMU: マルチプラットフォームビルド向けに QEMU のスタティックバイナリをインストールします。
- Docker Scout: Docker イメージのセキュリティぜい弱性を分析します。
Docker のアクションでは簡単なインターフェースを提供します。 それでもビルドパラメーターをカスタマイズできる機能は十分に維持しています。
メモFor an alternative to configuring individual actions, see Docker GitHub Builder. It provides Docker-maintained reusable workflows for building images and artifacts with BuildKit.
Get started
Build an image from your GitHub repository and push it to Docker Hub with the following workflow.
Prerequisites
- A GitHub repository with your application and a
Dockerfileat its root. - A Docker Hub account and a personal access token with permission to push images.
Configure your GitHub repository
Add your Docker Hub credentials to your GitHub repository:
- Open your repository's Settings.
- Under Security, go to Secrets and variables > Actions.
- Under Secrets, create a repository secret named
DOCKERHUB_TOKENcontaining your Docker access token. - Under Variables, create a repository variable named
DOCKERHUB_USERNAMEcontaining your Docker Hub username.
Set up your GitHub Actions workflow
Create .github/workflows/docker-ci.yml in your repository with the following
contents. Replace my-image with the name of your Docker Hub repository, and
change main if your default branch has a different name.
name: Build and push Docker image
on:
push:
branches:
- main
pull_request:
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Extract Docker image metadata
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ vars.DOCKERHUB_USERNAME }}/my-image
- name: Sign in to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v4
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Build and push Docker image
uses: docker/build-push-action@v7
with:
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
annotations: ${{ steps.meta.outputs.annotations }}Commit the workflow file and push it to your default branch to run it. The workflow also runs on pull requests. Pull-request builds skip signing in to Docker Hub and pushing the image, so they don't require registry secrets, including for pull requests from forks.
The build action uses Git context by default: BuildKit fetches your repository directly at the Git reference for the event that triggered the workflow, so no checkout step is needed. Changes to files made by earlier workflow steps are ignored because the build uses files from Git.
The metadata action generates tags, labels, and
annotations from the GitHub event and passes them to the build step. For
example, a push to main publishes DOCKERHUB_USERNAME/my-image:main.
See Manage tags and labels
and Annotations to customize
this metadata.
This workflow builds images but doesn't run application tests. To add tests, see Test before push. For additional build options, see Multi-platform images and SBOM and provenance attestations, which covers default provenance and how to add SBOM attestations when pushing to a registry.
利用例
Docker GitHub Actions を使った利用例を確認したい場合は、以下の節を参照してください。