3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[脳死したい人向け]Github ActionでDockerImageのBuild&Pushの自動化の実装、解説してみた

Last updated at Posted at 2023-03-18

自己紹介

初めまして。私は都内でwebエンジニアをしています。

今の会社にインターンとして経て、入社しました。

業務では、Nuxtjs, Golang, Kubernetesを用いて運用保守開発や

最近はPandasを用いてデータ分析など日々苦戦している若輩者です。

背景

Github ActionによるDockerImageのBuildを実装した際に、document通りに実装した時のメモです。

対象としている人

  • Github、DockerHubのアカウントがある人
  • Github ActionによるDokcer ImageのBuild&Pushの実装をしたい人

結論

1.以下のファイルを導入したいGithubレポジトリの.github/workflowsディレクトリ配下に設置してください。

name: ci
# mainブランチに変更があった場合に走ります。
on:
  push:
    branches:
      - "main"
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      -
        name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
                    # Dockerfileが存在するPathを指定する
          file: ./Dockerfile
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME }}/${リポジトリ名}:latest
      -
        name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          # Intel系AppleSilicon系に対応するため
          platforms: linux/amd64,linux/arm64
          # Dockerfileが存在するPathを指定する
          file: ./Dockerfile
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME ${リポジトリ名}:${{ github.sha }}

2.DockerHubにログインするための情報を把握する。
Github上でpushをするために、DockerのLoginを行う必要があるため以下の情報が必要です。

  • Dockerhubのユーザー名
  • DockerhubのAccess Token
    こちらからAccessTokenを取得してください。
    3.SecretにDockerHubにログインする必要な設定をする。
    対象レポジトリのSettngs/Security/Actions/Secretsに先ほど取得したDockerHubにログインするために必要な設定を行います。
    |key|value|
    |---|-----|
    |DOCKERHUB_USERNAME|Dockerhubのユーザー名|
    |DOCKERHUB_TOKEN|DockerhubのAccess Token|
    image.png

完成

以上で実装は完成です。
mainブランチのコードに変更があった場合、自動的にDockerImageがPushされます!

学んだ箇所

actions/checkout@v3の動作について
引用ですが、checkoutを追加するとpushされたコードをチェックアウト,プルリク時には,そのプルリクされたコードをチェックアウトするという動作になるそうです。

    -
      name: Checkout
      uses: actions/checkout@v3

これがないと以下のエラーが発生します。

buildx failed with: ERROR: failed to solve: failed to read dockerfile: open /tmp/buildkit-mount3050335498/Dockerfile: no such file or directory

参考

https://teratail-com.translate.goog/questions/06z9r8qi6eoq0x?_x_tr_sl=ja&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=sc
https://docs.docker.com/build/ci/github-actions/

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?