Docker #159
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Docker | |
| # This workflow uses actions that are not certified by GitHub. | |
| # They are provided by a third-party and are governed by | |
| # separate terms of service, privacy policy, and support | |
| # documentation. | |
| on: | |
| schedule: | |
| - cron: '39 11 * * *' | |
| push: | |
| branches: [ "main" ] | |
| # Publish semver tags as releases. | |
| tags: [ 'v*.*.*' ] | |
| #pull_request: | |
| # branches: [ "main" ] | |
| env: | |
| # Use docker.io for Docker Hub if empty | |
| REGISTRY: docker.io | |
| # github.repository as <account>/<repo> | |
| IMAGE_NAME: tetsuyainfra/acng | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| # This is used to complete the identity challenge | |
| # with sigstore/fulcio when running outside of PRs. | |
| id-token: write | |
| steps: | |
| - name: Set BUILD_DATE env | |
| run: echo "BUILD_DATE=$(date '+%Y-%m-%d')" >> $GITHUB_ENV | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| # Set up BuildKit Docker container builder to be able to build | |
| # multi-platform images and export cache | |
| # https://github.com/docker/setup-buildx-action | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 | |
| # Login against a Docker registry except on PR | |
| # https://github.com/docker/login-action | |
| - name: Log into registry ${{ env.REGISTRY }} | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| # Extract metadata (tags, labels) for Docker | |
| # https://github.com/docker/metadata-action | |
| - name: Extract Docker metadata | |
| id: meta | |
| uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }} | |
| type=ref,event=branch | |
| type=sha,prefix=sha- | |
| # type=ref,event=tag,pattern=v*.*.* | |
| # Build and push Docker image with Buildx (don't push on PR) | |
| # https://github.com/docker/build-push-action | |
| - name: Build Docker image | |
| id: build | |
| uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 | |
| with: | |
| context: ./acng | |
| push: false | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| # 単一プラットフォームでビルドしているのでloadオプションを使える | |
| # (マルチプラットフォームにするときは, outputオプションをつかう) | |
| load: true | |
| # GithubActionsのキャッシュ(7日間保持)に影響を受ける | |
| # cache-from: type=gha | |
| # cache-to: type=gha,mode=max | |
| # ローカルキャッシュにする | |
| cache-to: type=local,dest=./tmp/.buildx-cache | |
| - name: Get package.list in container | |
| run: | | |
| docker run --rm --entrypoint bash \ | |
| ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest \ | |
| -c 'cat /usr/share/rocks/packages.list' | tee new_package.list | |
| # Cache package.list | |
| # new_package.listのハッシュ値をキーにしてキャッシュを探す | |
| - name: Cache package.list | |
| id: package-list-cache | |
| uses: actions/cache@v4 | |
| with: | |
| path: package.list | |
| key: commit-${{ github.sha }}_package-list-${{ hashFiles('new_package.list') }} | |
| # キャッシュヒットした→ バージョンが同じ | |
| # キャッシュヒットしなかった → バージョンが異なる(おそらく新しい | |
| # キャッシュヒットしなかったら | |
| # actions/cacheにキャッシュしてもらえるようnew_package.listをpackage.listにコピーする | |
| - name: Cache hit then will not publish | |
| if: steps.package-list-cache.outputs.cache-hit != 'true' | |
| id: is_published | |
| run: | | |
| cp new_package.list package.list | |
| echo result=false >> $GITHUB_OUTPUT | |
| - name: Push Docker image | |
| id: push | |
| if: steps.is_published.outputs.result == 'false' | |
| uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 | |
| with: | |
| context: ./acng | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| # cache-from: type=gha | |
| # cache-to: type=gha,mode=max | |
| cache-from: type=local,src=./tmp/.buildx-cache |