|
| 1 | +name: Build Docker Images |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + target_branch_or_tag: |
| 7 | + required: true |
| 8 | + pull_request: |
| 9 | + |
| 10 | +jobs: |
| 11 | + build-images: |
| 12 | + runs-on: ubuntu-24.04-arm |
| 13 | + permissions: |
| 14 | + contents: write |
| 15 | + packages: write |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout repository when push |
| 19 | + if: github.event_name == 'push' |
| 20 | + uses: actions/checkout@v6 |
| 21 | + with: |
| 22 | + submodules: recursive |
| 23 | + fetch-depth: 1 |
| 24 | + |
| 25 | + - name: Checkout repository when input |
| 26 | + if: github.event_name == 'workflow_dispatch' |
| 27 | + uses: actions/checkout@v6 |
| 28 | + with: |
| 29 | + ref: ${{ inputs.target_branch_or_tag }} |
| 30 | + submodules: recursive |
| 31 | + fetch-depth: 1 |
| 32 | + |
| 33 | + - name: Log in to Github Container Registry |
| 34 | + uses: docker/login-action@v3 |
| 35 | + with: |
| 36 | + registry: ghcr.io |
| 37 | + username: ${{ github.actor }} |
| 38 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 39 | + |
| 40 | + - parallel: |
| 41 | + - name: Store backend tag |
| 42 | + id: backend_tag |
| 43 | + run: | |
| 44 | + cd backend |
| 45 | + echo "tag=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT |
| 46 | +
|
| 47 | + - name: Store frontend tag |
| 48 | + id: frontend_tag |
| 49 | + run: | |
| 50 | + cd frontend |
| 51 | + echo "tag=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT |
| 52 | +
|
| 53 | + - parallel: |
| 54 | + - name: Check if backend image exists |
| 55 | + id: check_tag_backend |
| 56 | + env: |
| 57 | + IMAGE_NAME: "ghcr.io/${{ github.repository_owner }}/csa-certification-tool-backend" |
| 58 | + run: | |
| 59 | + if docker manifest inspect $IMAGE_NAME:${{ steps.backend_tag.outputs.tag }} > /dev/null 2>&1; then |
| 60 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 61 | + else |
| 62 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 63 | + fi |
| 64 | +
|
| 65 | + - name: Check if frontend image exists |
| 66 | + id: check_tag_frontend |
| 67 | + env: |
| 68 | + IMAGE_NAME: "ghcr.io/${{ github.repository_owner }}/csa-certification-tool-frontend" |
| 69 | + run: | |
| 70 | + if docker manifest inspect $IMAGE_NAME:${{ steps.frontend_tag.outputs.tag }} > /dev/null 2>&1; then |
| 71 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 72 | + else |
| 73 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 74 | + fi |
| 75 | +
|
| 76 | + - name: Set up QEMU |
| 77 | + if: steps.check_tag_backend.outputs.exists == 'false' || steps.check_tag_frontend.outputs.exists == 'false' |
| 78 | + uses: docker/setup-qemu-action@v3 |
| 79 | + |
| 80 | + - name: Set up Docker Buildx |
| 81 | + if: steps.check_tag_backend.outputs.exists == 'false' || steps.check_tag_frontend.outputs.exists == 'false' |
| 82 | + uses: docker/setup-buildx-action@v3 |
| 83 | + with: |
| 84 | + platforms: linux/amd64, linux/arm64 |
| 85 | + |
| 86 | + - name: Install docker squash |
| 87 | + if: steps.check_tag_backend.outputs.exists == 'false' || steps.check_tag_frontend.outputs.exists == 'false' |
| 88 | + run: |
| 89 | + pip install docker-squash |
| 90 | + |
| 91 | + - parallel: |
| 92 | + - name: Build and push backend image |
| 93 | + if: steps.check_tag_backend.outputs.exists == 'false' |
| 94 | + run: | |
| 95 | + ./backend/scripts/build-docker-image.sh --squash --push --multiarch |
| 96 | +
|
| 97 | + - name: Build and push frontend image |
| 98 | + if: steps.check_tag_frontend.outputs.exists == 'false' |
| 99 | + run: | |
| 100 | + ./frontend/scripts/build-docker-image.sh --squash --push |
| 101 | +
|
| 102 | + - name: Ensure backend sha is used in docker-compose |
| 103 | + env: |
| 104 | + SHORT_HASH: steps.backend_tag.outputs.tag |
| 105 | + run: |
| 106 | + sed -i "s/backend:[a-z0-9]\{7\}/backend:${SHORT_HASH}/" docker-compose.yml |
| 107 | + |
| 108 | + - name: Ensure frontend sha is used in docker-compose |
| 109 | + env: |
| 110 | + SHORT_HASH: steps.frontend.outputs.tag |
| 111 | + run: |
| 112 | + sed -i "s/frontend:[a-z0-9]\{7\}/frontend:${SHORT_HASH}/" docker-compose.yml |
| 113 | + |
| 114 | + - name: Add changes |
| 115 | + run: | |
| 116 | + git add docker-compose.yml |
| 117 | +
|
| 118 | + - name: Check for changes |
| 119 | + id: check |
| 120 | + run: | |
| 121 | + if git diff --staged --exit-code; then |
| 122 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 123 | + else |
| 124 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 125 | + fi |
| 126 | +
|
| 127 | + - name: Commit and push |
| 128 | + if: steps.check.outputs.has_changes == 'true' |
| 129 | + run: | |
| 130 | + git config user.name "GitHub Actions" |
| 131 | + git config user.email "actions@github.com" |
| 132 | + git commit -m "Updating image labels in docker-compose" |
| 133 | + git push |
0 commit comments