|
1 | 1 | --- |
2 | | -name: Package Docker Image |
| 2 | +name: Package Docker Images |
3 | 3 |
|
4 | 4 | permissions: |
5 | 5 | packages: write |
| 6 | + contents: write |
6 | 7 |
|
7 | 8 | on: |
8 | 9 | push: |
9 | 10 | branches: |
10 | 11 | - main |
11 | | - workflow_dispatch: |
| 12 | + paths: |
| 13 | + - 'app/**/*.py' |
| 14 | + - '.devcontainer/**' |
| 15 | + - '.github/workflows/package-docker-images.yml' |
12 | 16 |
|
13 | 17 | jobs: |
14 | | - docker: |
| 18 | + build-docker-images: |
| 19 | + name: Build and push ${{ matrix.name }} image |
15 | 20 | runs-on: ubuntu-latest |
| 21 | + continue-on-error: true |
| 22 | + strategy: |
| 23 | + fail-fast: false |
| 24 | + matrix: |
| 25 | + include: |
| 26 | + - name: api |
| 27 | + context: ./ |
| 28 | + dockerfile: Dockerfile |
| 29 | + path_filter: api/ |
| 30 | + needs_api_url: true |
| 31 | + - name: api-devcontainer |
| 32 | + context: ./.devcontainer |
| 33 | + dockerfile: .devcontainer/Dockerfile |
| 34 | + path_filter: .devcontainer/ |
| 35 | + needs_api_url: true |
16 | 36 |
|
17 | 37 | steps: |
18 | 38 | - uses: actions/checkout@v4 |
| 39 | + with: |
| 40 | + fetch-depth: 0 |
| 41 | + |
| 42 | + - name: Check if build is needed |
| 43 | + id: check_changes |
| 44 | + run: | |
| 45 | + # Check if files in the component's directory were changed |
| 46 | + if git diff --name-only HEAD~1 HEAD | grep -q "^${{ matrix.path_filter }}" || \ |
| 47 | + git diff --name-only HEAD~1 HEAD | grep -q "^\.github/workflows/package-docker-images\.yml"; then |
| 48 | + echo "should_build=true" >> "$GITHUB_OUTPUT" |
| 49 | + else |
| 50 | + echo "should_build=false" >> "$GITHUB_OUTPUT" |
| 51 | + fi |
19 | 52 |
|
20 | 53 | - name: Extract version |
| 54 | + if: steps.check_changes.outputs.should_build == 'true' |
21 | 55 | id: version |
22 | 56 | run: | |
23 | | - VERSION=$(grep -oP '__version__\s*=\s*"\K[0-9]+\.[0-9]+\.[0-9]+' app/version.py) |
| 57 | + VERSION=$(date +%y.%m.%d.%H.%M) |
24 | 58 | echo "tag=$VERSION" >> "$GITHUB_OUTPUT" |
25 | 59 |
|
26 | | - - uses: docker/login-action@v3 |
| 60 | + - name: Normalize repository owner |
| 61 | + if: steps.check_changes.outputs.should_build == 'true' |
| 62 | + id: repo_owner |
| 63 | + run: echo "name=$(echo '${{ github.repository_owner }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT" |
| 64 | + |
| 65 | + - name: Validate Dockerfile exists |
| 66 | + if: steps.check_changes.outputs.should_build == 'true' |
| 67 | + run: | |
| 68 | + test -f "${{ matrix.dockerfile }}" || (echo "Dockerfile not found at ${{ matrix.dockerfile }}" && exit 1) |
| 69 | +
|
| 70 | + - name: Set up QEMU |
| 71 | + if: steps.check_changes.outputs.should_build == 'true' |
| 72 | + uses: docker/setup-qemu-action@v3 |
| 73 | + |
| 74 | + - name: Set up Docker Buildx |
| 75 | + if: steps.check_changes.outputs.should_build == 'true' |
| 76 | + uses: docker/setup-buildx-action@v3 |
| 77 | + |
| 78 | + - name: Login to GHCR |
| 79 | + if: steps.check_changes.outputs.should_build == 'true' |
| 80 | + uses: docker/login-action@v3 |
27 | 81 | with: |
28 | 82 | registry: ghcr.io |
29 | 83 | username: ${{ github.actor }} |
30 | 84 | password: ${{ secrets.GITHUB_TOKEN }} |
31 | 85 |
|
32 | | - - name: Normalize image name |
33 | | - run: | |
34 | | - echo "REPO_LOWER=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_ENV" |
| 86 | + - name: Build and push ${{ matrix.name }} image |
| 87 | + if: steps.check_changes.outputs.should_build == 'true' |
| 88 | + uses: docker/build-push-action@v5 |
| 89 | + with: |
| 90 | + context: ${{ matrix.context }} |
| 91 | + platforms: linux/amd64,linux/arm64 |
| 92 | + push: true |
| 93 | + build-args: ${{ matrix.needs_api_url == true && format('PUBLIC_API_URL={0}', secrets.PUBLIC_API_URL) || '' }} |
| 94 | + tags: | |
| 95 | + ghcr.io/${{ steps.repo_owner.outputs.name }}/${{ matrix.name }}:${{ steps.version.outputs.tag }} |
| 96 | + ghcr.io/${{ steps.repo_owner.outputs.name }}/${{ matrix.name }}:latest |
| 97 | + cache-from: type=gha,scope=${{ matrix.name }} |
| 98 | + cache-to: type=gha,mode=max,scope=${{ matrix.name }} |
35 | 99 |
|
36 | | - - name: Build and push Docker image |
37 | | - run: | |
38 | | - IMAGE="ghcr.io/${REPO_LOWER}-image:${{ steps.version.outputs.tag }}" |
39 | | - echo "🔨 Building image: $IMAGE" |
40 | | - docker build -t "$IMAGE" . |
41 | | - docker push "$IMAGE" |
| 100 | + - name: Generate SBOM |
| 101 | + if: steps.check_changes.outputs.should_build == 'true' |
| 102 | + uses: anchore/sbom-action@v0 |
| 103 | + with: |
| 104 | + image: ghcr.io/${{ steps.repo_owner.outputs.name }}/${{ matrix.name }}:${{ steps.version.outputs.tag }} |
| 105 | + format: spdx-json |
| 106 | + upload-release-assets: false |
| 107 | + artifact-name: sbom-${{ matrix.name }}-${{ steps.version.outputs.tag }} |
0 commit comments