Test, Build and Deploy Images #358
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: Test, Build and Deploy Images | |
| env: | |
| PROJECT: blueos | |
| DOCKER: base | |
| DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
| IMAGE_LIMIT_SIZE_MB: 900 | |
| ARTIFACTS_PATH: /tmp/artifacts | |
| DIGESTS_PATH: /tmp/digests | |
| on: | |
| workflow_dispatch: | |
| pull_request: | |
| push: | |
| schedule: | |
| # Rebuild master every 6 days so actions/cache stays within GitHub's 7-day retention window. | |
| - cron: '0 0 */6 * *' | |
| jobs: | |
| build: | |
| runs-on: ubuntu-24.04 | |
| strategy: | |
| matrix: | |
| platform: | |
| - linux/arm/v7 | |
| - linux/arm64 | |
| - linux/amd64 | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up Python 3.11 | |
| uses: actions/setup-python@v3 | |
| with: | |
| python-version: 3.11 | |
| - name: Run pre-commit checks | |
| uses: pre-commit/action@v3.0.1 | |
| - name: Prepare | |
| id: prepare | |
| run: | | |
| echo "DOCKER_IMAGE=${DOCKER_USERNAME:-bluerobotics}/${PROJECT}-${DOCKER}" >> $GITHUB_ENV | |
| rm -rf "${ARTIFACTS_PATH}" "${DIGESTS_PATH}" | |
| mkdir -p "${ARTIFACTS_PATH}" "${DIGESTS_PATH}" | |
| platform=${{ matrix.platform }} | |
| platform_pair=${platform//\//-} | |
| echo "PLATFORM_PAIR=${platform_pair}" >> $GITHUB_ENV | |
| echo "BUILDX_CACHE_PATH=/tmp/.buildx-cache-${platform_pair}" >> $GITHUB_ENV | |
| - name: Docker meta | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.DOCKER_IMAGE }} | |
| tags: | | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| - name: Set up QEMU | |
| uses: docker/setup-qemu-action@v3 | |
| with: | |
| platforms: all | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| with: | |
| version: latest | |
| name: builder-${{ env.PLATFORM_PAIR }} | |
| keep-state: true | |
| - name: Cache Docker layers | |
| uses: actions/cache@v4 | |
| with: | |
| path: ${{ env.BUILDX_CACHE_PATH }} | |
| key: buildx-${{ env.PLATFORM_PAIR }}-${{ hashFiles('Dockerfile', 'scripts/build_gst.sh') }} | |
| restore-keys: | | |
| buildx-${{ env.PLATFORM_PAIR }}- | |
| - name: Login to DockerHub | |
| if: success() && github.event_name != 'pull_request' | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Build and push | |
| id: build | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: Dockerfile | |
| platforms: ${{ matrix.platform }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| push: ${{ github.event_name != 'pull_request' }} | |
| outputs: type=docker,dest=${{ env.ARTIFACTS_PATH }}/${{ env.PLATFORM_PAIR }}.tar | |
| tags: ${{ steps.meta.outputs.tags }} | |
| cache-from: type=local,src=${{ env.BUILDX_CACHE_PATH }} | |
| cache-to: type=local,dest=${{ env.BUILDX_CACHE_PATH }},mode=max | |
| - name: Export digest | |
| run: | | |
| digest="${{ steps.build.outputs.digest }}" | |
| touch "${{ env.DIGESTS_PATH }}/${digest#sha256:}" | |
| - name: Upload digest | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: digests-${{ env.PLATFORM_PAIR }} | |
| path: ${{ env.DIGESTS_PATH }}/* | |
| if-no-files-found: error | |
| retention-days: 1 | |
| - name: Upload artifacts | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: ${{ env.PROJECT }}-${{ env.DOCKER }}-${{ env.PLATFORM_PAIR }} | |
| path: ${{ env.ARTIFACTS_PATH }}/${{ env.PLATFORM_PAIR }}.tar | |
| if-no-files-found: error | |
| - name: Check size | |
| run: | | |
| image_name=$(docker load --input ${{ env.ARTIFACTS_PATH }}/${{ env.PLATFORM_PAIR }}.tar | awk '{print $3}') | |
| IMAGE_SIZE_BYTES=$(docker image inspect $image_name --format "{{json .Size}}") | |
| IMAGE_SIZE_MB=$((IMAGE_SIZE_BYTES / 1024 / 1024)) | |
| echo "Core size is: ${IMAGE_SIZE_MB} MB" | |
| echo "Core size limit: ${IMAGE_LIMIT_SIZE_MB} MB" | |
| if [ "$IMAGE_SIZE_MB" -gt "$IMAGE_LIMIT_SIZE_MB" ]; then | |
| echo "::error::Image size is larger than the limit" | |
| fi | |
| merge: | |
| runs-on: ubuntu-24.04 | |
| needs: | |
| - build | |
| steps: | |
| - name: Download digests | |
| uses: actions/download-artifact@v4 | |
| with: | |
| path: ${{ env.DIGESTS_PATH }} | |
| pattern: digests-* | |
| merge-multiple: true | |
| - name: Delete digests | |
| uses: geekyeggo/delete-artifact@v5 | |
| with: | |
| name: digests-* | |
| useGlob: true | |
| failOnError: false | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Prepare | |
| id: prepare | |
| run: echo "DOCKER_IMAGE=${DOCKER_USERNAME:-bluerobotics}/${PROJECT}-${DOCKER}" >> $GITHUB_ENV | |
| - name: Docker meta | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.DOCKER_IMAGE }} | |
| tags: | | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| if: success() && github.event_name != 'pull_request' | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Create manifest list and push | |
| if: success() && github.event_name != 'pull_request' | |
| working-directory: ${{ env.DIGESTS_PATH }} | |
| run: docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") $(printf '${{ env.DOCKER_IMAGE }}@sha256:%s ' *) | |
| - name: Inspect image | |
| if: always() && github.event_name != 'pull_request' | |
| run: docker buildx imagetools inspect ${{ env.DOCKER_IMAGE }}:${{ steps.meta.outputs.version }} --format "{{json .}}" | jq |