Build Container Images #181
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: Build Container Images | |
| on: | |
| workflow_dispatch: | |
| push: | |
| paths: | |
| - "**/Dockerfile" | |
| schedule: | |
| - cron: '0 0 */2 * *' | |
| # Avoids Github UI bugs https://github.com/orgs/community/discussions/45969 | |
| env: | |
| BUILDX_NO_DEFAULT_ATTESTATIONS: 1 | |
| jobs: | |
| ghcrbuild: | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: | |
| - ubuntu: jammy | |
| platform: linux/amd64 | |
| runner: ubuntu-latest | |
| - ubuntu: noble | |
| platform: linux/amd64 | |
| runner: ubuntu-latest | |
| - ubuntu: noble | |
| platform: linux/arm64 | |
| runner: ubuntu-latest-arm64 | |
| name: Build ${{ matrix.ubuntu }} (${{ matrix.platform }}) for GHCR | |
| runs-on: ${{ matrix.runner }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Extract metadata for container image | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ghcr.io/${{ github.repository }} | |
| tags: | | |
| type=raw,value=${{ matrix.ubuntu }} | |
| - name: Extract container name without tag | |
| id: vars | |
| run: | | |
| echo container=$(echo '${{ steps.meta.outputs.tags }}' | awk -F':' '{print $1}') >> $GITHUB_OUTPUT | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.repository_owner }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push container image to ghcr | |
| id: build | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: ${{matrix.ubuntu}} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| outputs: type=image,name=${{ steps.vars.outputs.container }},push-by-digest=true,name-canonical=true,push=true | |
| platforms: ${{matrix.platform}} | |
| provenance: false | |
| no-cache: true | |
| - name: Prepare Dockerfile to extract R version with emulator | |
| run: | | |
| mkdir -p ${{matrix.ubuntu}}-${{matrix.platform}} | |
| cat << "EOF" > ${{matrix.ubuntu}}-${{matrix.platform}}-r.Dockerfile | |
| FROM ${{ steps.vars.outputs.container }}@${{ steps.build.outputs.digest }} as extract | |
| RUN mkdir /r2utmp && R --version > /r2utmp/rver && cat /etc/os-release > /r2utmp/os-release | |
| FROM scratch as export | |
| COPY --from=extract /r2utmp/* / | |
| EOF | |
| - name: Build and push container image to ghcr | |
| uses: docker/build-push-action@v6 | |
| with: | |
| file: ${{matrix.ubuntu}}-${{matrix.platform}}-r.Dockerfile | |
| context: . | |
| push: false | |
| load: false | |
| provenance: false | |
| outputs: type=tar,dest=${{matrix.ubuntu}}-${{matrix.platform}}/r.tar | |
| tags: ${{ steps.meta.outputs.tags }}-r | |
| platforms: ${{ matrix.platform }} | |
| - name: Extract rversion | |
| id: versions | |
| run: | | |
| cd ${{matrix.ubuntu}}-${{matrix.platform}} | |
| tar -xvf r.tar | |
| echo rver=$(cat rver | awk 'NR==1{print $3}') >> $GITHUB_OUTPUT | |
| echo osver=$(awk -F= '/VERSION_ID/ {gsub("\"", "", $2); print $2}' os-release) >> $GITHUB_OUTPUT | |
| - name: Export digest by ubuntu and r versions | |
| run: | | |
| digest="${{ steps.build.outputs.digest }}" | |
| mkdir -p /tmp/digests/${{matrix.ubuntu}} | |
| touch "/tmp/digests/${{matrix.ubuntu}}/${digest#sha256:}" | |
| mkdir -p /tmp/digests/${{matrix.ubuntu}}-r-${{steps.versions.outputs.rver}} | |
| touch "/tmp/digests/${{matrix.ubuntu}}-r-${{steps.versions.outputs.rver}}/${digest#sha256:}" | |
| mkdir -p /tmp/digests/${{steps.versions.outputs.osver}} | |
| touch "/tmp/digests/${{steps.versions.outputs.osver}}/${digest#sha256:}" | |
| mkdir -p /tmp/digests/${{steps.versions.outputs.osver}}-r-${{steps.versions.outputs.rver}} | |
| touch "/tmp/digests/${{steps.versions.outputs.osver}}-r-${{steps.versions.outputs.rver}}/${digest#sha256:}" | |
| - name: Set platform name for artifact | |
| id: platform | |
| run: echo "name=$(echo '${{matrix.platform}}' | sed 's/\//-/g')" >> $GITHUB_OUTPUT | |
| - name: Upload digests | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: r2udigests-${{matrix.ubuntu}}-${{steps.platform.outputs.name}}-${{github.run_id}} | |
| path: /tmp/digests/** | |
| if-no-files-found: error | |
| retention-days: 1 | |
| merge: | |
| runs-on: ubuntu-latest | |
| needs: | |
| - ghcrbuild | |
| if: always() | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| ubuntu: [jammy, noble] | |
| steps: | |
| - name: Download amd64 digests | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: r2udigests-${{matrix.ubuntu}}-linux-amd64-${{github.run_id}} | |
| path: /tmp/digests | |
| - name: Download arm64 digests | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: r2udigests-${{matrix.ubuntu}}-linux-arm64-${{github.run_id}} | |
| path: /tmp/digests | |
| continue-on-error: true | |
| if: matrix.ubuntu == 'noble' | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.repository_owner }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata for container image | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ghcr.io/${{ github.repository }} | |
| tags: | | |
| type=raw,value=${{ matrix.ubuntu }} | |
| - name: Extract container name without tag | |
| id: vars | |
| run: | | |
| echo container=$(echo '${{ steps.meta.outputs.tags }}' | awk -F':' '{print $1}') >> $GITHUB_OUTPUT | |
| - name: Create manifest list and push | |
| run: | | |
| cd /tmp/digests | |
| ls --hide=digestdirs > digestdirs | |
| cat digestdirs | xargs -i bash -c 'docker buildx imagetools create -t ${{steps.vars.outputs.container}}:{} $(for f in {}/*; do basename "$f"; done | xargs printf "${{steps.vars.outputs.container}}@sha256:%s ")' | |
| ghcrbuild_ci: | |
| needs: merge | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: | |
| - ubuntu: noble_ci | |
| platform: linux/amd64 | |
| runner: ubuntu-latest | |
| - ubuntu: noble_ci | |
| platform: linux/arm64 | |
| runner: ubuntu-latest-arm64 | |
| name: Build ${{ matrix.ubuntu }} (${{ matrix.platform }}) for GHCR | |
| runs-on: ${{ matrix.runner }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Extract metadata for container image | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ghcr.io/${{ github.repository }} | |
| tags: | | |
| type=raw,value=${{ matrix.ubuntu }} | |
| - name: Extract container name without tag | |
| id: vars | |
| run: | | |
| echo container=$(echo '${{ steps.meta.outputs.tags }}' | awk -F':' '{print $1}') >> $GITHUB_OUTPUT | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.repository_owner }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push container image to ghcr | |
| id: build | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: ${{matrix.ubuntu}} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| outputs: type=image,name=${{ steps.vars.outputs.container }},push-by-digest=true,name-canonical=true,push=true | |
| platforms: ${{matrix.platform}} | |
| provenance: false | |
| no-cache: true | |
| - name: Export digest | |
| run: | | |
| digest="${{ steps.build.outputs.digest }}" | |
| mkdir -p /tmp/digests/${{matrix.ubuntu}} | |
| touch "/tmp/digests/${{matrix.ubuntu}}/${digest#sha256:}" | |
| - name: Set platform name for artifact | |
| id: platform | |
| run: echo "name=$(echo '${{matrix.platform}}' | sed 's/\//-/g')" >> $GITHUB_OUTPUT | |
| - name: Upload digests | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: r2udigests-${{matrix.ubuntu}}-${{steps.platform.outputs.name}}-${{github.run_id}} | |
| path: /tmp/digests/** | |
| if-no-files-found: error | |
| retention-days: 1 | |
| merge_ci: | |
| runs-on: ubuntu-latest | |
| needs: | |
| - ghcrbuild_ci | |
| if: always() | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| ubuntu: [noble_ci] | |
| steps: | |
| - name: Download amd64 digests | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: r2udigests-${{matrix.ubuntu}}-linux-amd64-${{github.run_id}} | |
| path: /tmp/digests | |
| - name: Download arm64 digests | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: r2udigests-${{matrix.ubuntu}}-linux-arm64-${{github.run_id}} | |
| path: /tmp/digests | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.repository_owner }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata for container image | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ghcr.io/${{ github.repository }} | |
| tags: | | |
| type=raw,value=${{ matrix.ubuntu }} | |
| - name: Extract container name without tag | |
| id: vars | |
| run: | | |
| echo container=$(echo '${{ steps.meta.outputs.tags }}' | awk -F':' '{print $1}') >> $GITHUB_OUTPUT | |
| - name: Create manifest list and push | |
| run: | | |
| cd /tmp/digests | |
| ls --hide=digestdirs > digestdirs | |
| cat digestdirs | xargs -i bash -c 'docker buildx imagetools create -t ${{steps.vars.outputs.container}}:{} $(for f in {}/*; do basename "$f"; done | xargs printf "${{steps.vars.outputs.container}}@sha256:%s ")' |