From 087ae4faca0793833405ee0b4a19a0d0d6d0f32f Mon Sep 17 00:00:00 2001 From: Wouter Wijsman Date: Mon, 9 Oct 2023 14:22:52 +0200 Subject: [PATCH] docker: Only build once and push to repo registry --- .github/workflows/build_docker_image.yml | 89 +++++++++++++++++------- .github/workflows/build_samples.yml | 31 --------- 2 files changed, 62 insertions(+), 58 deletions(-) diff --git a/.github/workflows/build_docker_image.yml b/.github/workflows/build_docker_image.yml index ef3c966c3..b1dc6263a 100644 --- a/.github/workflows/build_docker_image.yml +++ b/.github/workflows/build_docker_image.yml @@ -1,64 +1,99 @@ name: Build Docker image -on: - push: - branches: master +on: [push, pull_request] jobs: main: name: Docker - if: github.repository_owner == 'XboxDev' runs-on: ubuntu-latest timeout-minutes: 20 strategy: matrix: + fail-fast: false image-name: [nxdk, nxdk-debug, nxdk-lto] include: - image-name: nxdk build-args: buildparams=CFLAGS=-O2 CXXFLAGS=-O2 + tag: latest - image-name: nxdk-debug build-args: buildparams=DEBUG=y + tag: lto - image-name: nxdk-lto build-args: buildparams=LTO=y CFLAGS=-O2 CXXFLAGS=-O2 + tag: debug steps: - name: Clone Tree uses: actions/checkout@v3 with: submodules: recursive - - name: Generate timestamp - id: timestamp - run: echo ::set-output name=timestamp::$(date --rfc-3339=seconds --utc) - - name: Generate tags - id: tags + - name: Set variables run: | - DOCKER_IMAGE=xboxdev/${{ matrix.image-name }} - TAGS="${DOCKER_IMAGE}:latest" - TAGS="$TAGS,${DOCKER_IMAGE}:git-${GITHUB_SHA::8}" - TAGS="$TAGS,ghcr.io/${DOCKER_IMAGE}:latest" - TAGS="$TAGS,ghcr.io/${DOCKER_IMAGE}:git-${GITHUB_SHA::8}" - echo ::set-output name=tags::${TAGS} + echo "DOCKER_TAG=$(echo ghcr.io/${{ github.repository }}:${{ matrix.tag }}|tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV + echo "CREATED=$(date --rfc-3339=seconds --utc)" >> $GITHUB_ENV + echo "SHORT_SHA=${GITHUB_SHA::8}" >> $GITHUB_ENV - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - - name: Login to DockerHub - uses: docker/login-action@v2 + - name: Build Docker image + uses: docker/build-push-action@v3 with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Login to GitHub Container Registry + push: false + tags: ${{ env.DOCKER_TAG}} + build-args: ${{ matrix.build-args }} + - name: Test Docker image + run: | + cd samples + for dir in */ + do + cd "$dir" + docker run --rm -v `pwd`:/usr/src/app -t "${DOCKER_TAG}" make -j`nproc` + cd .. + done + + - name: Login to Docker Registry uses: docker/login-action@v2 + if: github.ref == 'refs/heads/master' with: registry: ghcr.io - username: ${{ github.actor }} + username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Build and push + - name: Build Docker Image uses: docker/build-push-action@v3 with: - context: . - file: ./Dockerfile + push: true build-args: ${{ matrix.build-args }} - platforms: linux/amd64,linux/386 + labels: | + org.opencontainers.image.revision=${{ github.sha }} + org.opencontainers.image.created=${{ env.CREATED }} + tags: ${DOCKER_TAG} + + - name: Push to XboxDev Group + uses: docker/build-push-action@v3 + if: github.repository_owner == 'XboxDev' && github.ref == 'refs/heads/master' + with: push: true - tags: ${{ steps.tags.outputs.tags }} + build-args: ${{ matrix.build-args }} + labels: | + org.opencontainers.image.revision=${{ github.sha }} + org.opencontainers.image.created=${{ env.CREATED }} + tags: | + ghcr.io/xboxdev/${{ matrix.image-name }}:latest + ghcr.io/xboxdev/${{ matrix.image-name }}:git-${{ env.SHORT_SHA }} + + - name: Login to Docker Hub + uses: docker/login-action@v2 + if: github.repository_owner == 'XboxDev' && github.ref == 'refs/heads/master' + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Push to Docker Hub + uses: docker/build-push-action@v3 + if: github.repository_owner == 'XboxDev' && github.ref == 'refs/heads/master' + with: + push: true + build-args: ${{ matrix.build-args }} labels: | org.opencontainers.image.revision=${{ github.sha }} - org.opencontainers.image.created=${{ steps.timestamp.outputs.timestamp }} + org.opencontainers.image.created=${{ env.CREATED }} + tags: | + xboxdev/${{ matrix.image-name }}:latest + xboxdev/${{ matrix.image-name }}:git-${{ env.SHORT_SHA }} diff --git a/.github/workflows/build_samples.yml b/.github/workflows/build_samples.yml index b6f2aac45..38e079b9f 100644 --- a/.github/workflows/build_samples.yml +++ b/.github/workflows/build_samples.yml @@ -3,37 +3,6 @@ name: Build Samples on: [push, pull_request] jobs: - docker: - name: Docker - runs-on: ubuntu-latest - timeout-minutes: 20 - strategy: - matrix: - image-name: [nxdk, nxdk-debug, nxdk-lto] - include: - - image-name: nxdk - build-args: buildparams=CFLAGS=-O2 CXXFLAGS=-O2 - - image-name: nxdk-debug - build-args: buildparams=DEBUG=y - - image-name: nxdk-lto - build-args: buildparams=LTO=y CFLAGS=-O2 CXXFLAGS=-O2 - steps: - - name: Clone Tree - uses: actions/checkout@v3 - with: - submodules: recursive - - name: Build Docker image - run: | - docker build --build-arg buildparams="${{ matrix.build-args }}" -t ${{ matrix.image-name }} ./ - - name: Test Docker image - run: | - cd samples - for dir in */ - do - cd "$dir" - docker run --rm -v `pwd`:/usr/src/app -t ${{ matrix.image-name }} make -j`nproc` - cd .. - done windows: name: Windows runs-on: windows-latest