|
1 | 1 | --- |
2 | 2 |
|
| 3 | +concurrency: |
| 4 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 5 | + cancel-in-progress: true |
| 6 | + |
3 | 7 | on: # yamllint disable-line rule:truthy |
4 | 8 | workflow_dispatch: |
5 | 9 | pull_request: |
6 | 10 | branches: |
7 | 11 | - master |
8 | 12 |
|
| 13 | +env: |
| 14 | + DOCKER_NAMESPACE: wayofdev/php-base |
| 15 | + GHCR_NAMESPACE: ghcr.io/wayofdev/docker-php-base |
| 16 | + |
9 | 17 | name: 🚀 Build docker images with latest tag |
10 | 18 |
|
11 | 19 | jobs: |
12 | | - # https://docs.github.com/en/enterprise-cloud@latest/actions/learn-github-actions/expressions#example-returning-a-json-object |
13 | | - prepare: |
14 | | - runs-on: "ubuntu-latest" |
15 | | - outputs: |
16 | | - matrix: ${{ steps.matrix.outputs.matrix }} |
| 20 | + build: |
| 21 | + strategy: |
| 22 | + fail-fast: false |
| 23 | + matrix: |
| 24 | + os_name: ["alpine"] |
| 25 | + php_version: ["8.1", "8.2"] |
| 26 | + php_type: ["fpm", "cli", "supervisord"] |
| 27 | + builder: [{arch: "amd64", os: "ubuntu-latest"}, {arch: "arm64", os: "ubuntu-latest"}] |
| 28 | + runs-on: ${{ matrix.builder.os }} |
17 | 29 | steps: |
18 | | - - name: ⚙️ Generate matrix |
19 | | - id: matrix |
| 30 | + |
| 31 | + - name: 🌎 Set environment variables |
20 | 32 | run: | |
21 | | - echo 'matrix={ |
22 | | - "os_name": ["alpine"], |
23 | | - "php_version": ["8.1", "8.2"], |
24 | | - "php_type": ["fpm", "cli", "supervisord"] |
25 | | - }' | tr -d '\n' >> $GITHUB_OUTPUT |
| 33 | + php_version="${{ matrix.php_version }}" |
| 34 | + tag="${{ matrix.php_version }}-${{ matrix.php_type }}-${{ matrix.os_name }}-${{ matrix.builder.arch }}" |
| 35 | + php_version_slug="${php_version//./}" |
| 36 | + target="php-${php_version_slug}-${{ matrix.php_type }}-${{ matrix.os_name }}" |
| 37 | + echo "TARGET=${target}" >> $GITHUB_ENV |
| 38 | + echo "PLATFORM_CACHE_TAG=${tag}" >> $GITHUB_ENV |
26 | 39 |
|
27 | | - build: |
28 | | - needs: prepare |
29 | | - strategy: |
30 | | - matrix: ${{ fromJson(needs.prepare.outputs.matrix )}} |
31 | | - uses: wayofdev/gh-actions/.github/workflows/build-image.yml@master |
32 | | - with: |
33 | | - os: "ubuntu-latest" |
34 | | - push-to-hub: true |
35 | | - image-namespace: "wayofdev/php-base" |
36 | | - image-template-path: "./dist/base" |
37 | | - image-template: ${{ matrix.php_version }}-${{ matrix.php_type }}-${{ matrix.os_name }} |
38 | | - image-version: latest |
39 | | - secrets: |
40 | | - docker-username: ${{ secrets.DOCKER_USERNAME }} |
41 | | - docker-password: ${{ secrets.DOCKER_TOKEN }} |
| 40 | + - name: 📦 Check out the codebase |
| 41 | + uses: actions/checkout@v4 |
| 42 | + |
| 43 | + - name: 🤖 Generate dist files |
| 44 | + run: ansible-playbook src/playbook.yml -l ${{ matrix.php_version }}-${{ matrix.php_type }}-${{ matrix.os_name }} |
| 45 | + |
| 46 | + - name: 💻 Set up Docker for MacOS |
| 47 | + if: ${{ matrix.builder.os == 'macos-latest' }} |
| 48 | + uses: docker-practice/actions-setup-docker@master |
| 49 | + |
| 50 | + - name: 🖥️ Setup docker QEMU |
| 51 | + uses: docker/setup-qemu-action@v3 |
| 52 | + |
| 53 | + - name: 🛠️ Set up Docker Buildx |
| 54 | + uses: docker/setup-buildx-action@v3 |
| 55 | + with: |
| 56 | + buildkitd-flags: "--debug" |
| 57 | + |
| 58 | + - name: 🐳 Extract docker meta data |
| 59 | + id: meta |
| 60 | + uses: docker/metadata-action@v5 |
| 61 | + with: |
| 62 | + images: | |
| 63 | + ${{ env.DOCKER_NAMESPACE }} |
| 64 | + ${{ env.GHCR_NAMESPACE }} |
| 65 | + tags: | |
| 66 | + type=raw,event=branch,value=latest |
| 67 | + type=ref,event=pr |
| 68 | + type=semver,pattern={{version}} |
| 69 | + type=semver,pattern={{major}}.{{minor}} |
| 70 | + flavor: | |
| 71 | + latest=false |
| 72 | + prefix=${{ matrix.php_version }}-${{ matrix.php_type }}-${{ matrix.os_name }}- |
| 73 | +
|
| 74 | + - name: ⚙️ Rename meta bake definition file |
| 75 | + run: | |
| 76 | + mv "${{ steps.meta.outputs.bake-file }}" "/tmp/bake-meta-${{ env.PLATFORM_CACHE_TAG }}.json" |
| 77 | + cat "/tmp/bake-meta-${{ env.PLATFORM_CACHE_TAG }}.json" |
| 78 | +
|
| 79 | + - name: 📤 Upload meta bake definition |
| 80 | + uses: actions/upload-artifact@v4 |
| 81 | + with: |
| 82 | + name: bake-meta-${{ env.PLATFORM_CACHE_TAG }} |
| 83 | + path: /tmp/bake-meta-*.json |
| 84 | + if-no-files-found: error |
| 85 | + retention-days: 1 |
| 86 | + |
| 87 | + - name: 🔑 Login to docker-hub |
| 88 | + uses: docker/login-action@v3 |
| 89 | + with: |
| 90 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 91 | + password: ${{ secrets.DOCKER_TOKEN }} |
| 92 | + |
| 93 | + - name: 🔑 Login to GHCR |
| 94 | + uses: docker/login-action@v3 |
| 95 | + with: |
| 96 | + registry: ghcr.io |
| 97 | + username: ${{ github.repository_owner }} |
| 98 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 99 | + |
| 100 | + - name: 🚀 Bake image and push to docker-hub and GHCR |
| 101 | + id: bake |
| 102 | + uses: docker/bake-action@v4 |
| 103 | + with: |
| 104 | + targets: ${{ env.TARGET }} |
| 105 | + files: | |
| 106 | + ./docker-bake.hcl |
| 107 | + /tmp/bake-meta-${{ env.PLATFORM_CACHE_TAG }}.json |
| 108 | + set: | |
| 109 | + *.tags= |
| 110 | + *.platform=linux/${{ matrix.builder.arch }} |
| 111 | + *.cache-from=type=gha,scope=build-${{ env.PLATFORM_CACHE_TAG }} |
| 112 | + *.cache-to=type=gha,scope=build-${{ env.PLATFORM_CACHE_TAG }} |
| 113 | + *.output=type=image,"name=${{ env.DOCKER_NAMESPACE }},${{ env.GHCR_NAMESPACE }}",push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }} |
| 114 | +
|
| 115 | + - name: 🔍 Debug Bake Metadata Output |
| 116 | + run: | |
| 117 | + echo "${{ steps.bake.outputs.metadata }}" |
| 118 | +
|
| 119 | + - name: 🔍 Debug baked image |
| 120 | + run: | |
| 121 | + tag=$(jq -r '.target."docker-metadata-action".args.DOCKER_META_VERSION' /tmp/bake-meta-${{ env.PLATFORM_CACHE_TAG }}.json) |
| 122 | + if [ -z "$tag" ]; then |
| 123 | + echo "Tag is empty, skipping inspect." |
| 124 | + else |
| 125 | + echo "Tag extracted: $tag" |
| 126 | + docker buildx imagetools inspect ${{ env.DOCKER_NAMESPACE }}:${tag} |
| 127 | + fi |
| 128 | +
|
| 129 | + - name: 📥 Export digest |
| 130 | + run: | |
| 131 | + mkdir -p /tmp/digests |
| 132 | + echo "Bake Metadata: ${{ steps.bake.outputs.metadata }}" |
| 133 | + digest=$(echo '${{ steps.bake.outputs.metadata }}' | jq -r '.["${{ env.TARGET }}"]["containerimage.digest"]') |
| 134 | + if [[ -z "$digest" || "$digest" == "null" ]]; then |
| 135 | + echo "Digest not found." |
| 136 | + exit 1 |
| 137 | + fi |
| 138 | + echo "Digest: $digest" |
| 139 | + touch "/tmp/digests/${digest#sha256:}" |
| 140 | +
|
| 141 | + - name: 📤 Upload digest |
| 142 | + uses: actions/upload-artifact@v4 |
| 143 | + with: |
| 144 | + name: digests-${{ env.PLATFORM_CACHE_TAG }} |
| 145 | + path: /tmp/digests/* |
| 146 | + if-no-files-found: error |
| 147 | + retention-days: 1 |
| 148 | + |
| 149 | + merge: |
| 150 | + needs: build |
| 151 | + runs-on: ubuntu-latest |
| 152 | + steps: |
| 153 | + |
| 154 | + - name: 📥 Download meta bake definitions |
| 155 | + uses: actions/download-artifact@v4 |
| 156 | + with: |
| 157 | + pattern: bake-meta-* |
| 158 | + path: /tmp |
| 159 | + |
| 160 | + - name: 📥 Download meta bake definitions |
| 161 | + uses: actions/download-artifact@v4 |
| 162 | + with: |
| 163 | + pattern: digests-* |
| 164 | + path: /tmp/digests/* |
| 165 | + |
| 166 | + - name: Print all download artifact files |
| 167 | + run: | |
| 168 | + ls -la /tmp |
| 169 | + ls -la /tmp/digests |
| 170 | +
|
| 171 | + - name: 🔑 Login to docker-hub |
| 172 | + uses: docker/login-action@v3 |
| 173 | + with: |
| 174 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 175 | + password: ${{ secrets.DOCKER_TOKEN }} |
| 176 | + |
| 177 | + - name: 🔑 Login to GHCR |
| 178 | + uses: docker/login-action@v3 |
| 179 | + with: |
| 180 | + registry: ghcr.io |
| 181 | + username: ${{ github.repository_owner }} |
| 182 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 183 | + |
| 184 | + - name: 🖥️ Setup docker QEMU |
| 185 | + uses: docker/setup-qemu-action@v3 |
| 186 | + |
| 187 | + - name: 🛠️ Set up Docker Buildx |
| 188 | + uses: docker/setup-buildx-action@v3 |
| 189 | + with: |
| 190 | + buildkitd-flags: "--debug" |
| 191 | + |
| 192 | + - name: Create manifest list and push |
| 193 | + run: | |
| 194 | + # Aggregate all tags from the meta files |
| 195 | + TAGS=$(jq -r '.target."docker-metadata-action".tags[]' /tmp/meta/bake-meta-*.json | sort -u) |
| 196 | +
|
| 197 | + # Loop through each tag and create a manifest list |
| 198 | + for TAG in $TAGS; do |
| 199 | + FULL_TAGS=() |
| 200 | + for FILE in /tmp/meta/bake-meta-*.json; do |
| 201 | + if jq -e --arg TAG "$TAG" '.target."docker-metadata-action".tags[] | select(. == $TAG)' $FILE > /dev/null; then |
| 202 | + # Extract the repository and digest for the current platform |
| 203 | + REPO=$(jq -r --arg TAG "$TAG" '.target."docker-metadata-action".tags[] | select(. == $TAG)' $FILE | cut -d':' -f1) |
| 204 | + DIGEST=$(jq -r '.["'$REPO'"]["containerimage.digest"]' $FILE) |
| 205 | + FULL_TAGS+=("$REPO@$DIGEST") |
| 206 | + fi |
| 207 | + done |
| 208 | +
|
| 209 | + # Create and push the manifest list for the current tag |
| 210 | + docker buildx imagetools create "${FULL_TAGS[@]}" -t "$TAG" |
| 211 | + done |
42 | 212 |
|
43 | 213 | ... |
0 commit comments