Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-build and publish container images - rather than building every run #1310

Open
sammcj opened this issue Oct 13, 2022 · 0 comments
Open

Comments

@sammcj
Copy link

sammcj commented Oct 13, 2022

Problem

At present it seems reviewdog actions have to build the container image on ever (uncached) run.

This significantly increase the time it takes to run and the compute cost for reviewdog based Actions.

Solution

One way to improve this would be to build and publish container images and use those, image hosting for open source projects is free on Github using Github.

This greatly improves performance and cost of Docker based Actions.

Example

If the Actions still want to be a "Docker" type Action, you can simply keep their Dockerfile with that only includes a FROM for the image that's already been built:

FROM ghcr.io/reviewdog/some-cool-action:latest

and rename, build and publish existing Dockerfile which will be used for the builds.

An example of such a workflow might look like:

(assuming the "real" dockerfile is now called prebuild.Dockerfile

name: Build & Publish Docker Image

on:
  workflow_dispatch:
  push:
    branches:
      - master
permissions:
  packages: write
  contents: read

env:
  IMAGE_NAME: ${{ github.repository }}
  IMAGE_TAG: ${{ github.sha }}
  REGISTRY: ghcr.io

jobs:
  build:
    timeout-minutes: 15
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Generate Docker Metadata
        id: meta
        uses: docker/metadata-action@v4
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          images: |
            ${{ env.IMAGE_NAME }}
            ghcr.io/user${{ env.IMAGE_NAME }}
          tags: |
            type=schedule
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=sha

      - name: Set up QEMU To support build amd64 and arm64 images
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to Github Container Repository
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v3
        id: docker_build
        with:
          push: ${{ github.event_name != 'pull_request' }}
          context: .
          file: ./prebuild.Dockerfile
          platforms: linux/amd64,linux/arm64
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

      - name: Output image, digest and metadata to summary
        run: |
          {
          echo imageid: "${{ steps.docker_build.outputs.imageid }}"
          echo digest: "${{ steps.docker_build.outputs.digest }}"
          echo labels: "${{ steps.meta.outputs.labels }}"
          echo tags: "${{ steps.meta.outputs.tags }}"
          echo version: "${{ steps.meta.outputs.version }}"
          } >> "$GITHUB_STEP_SUMMARY"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant