|
| 1 | +name: Build multi-arch image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + ### |
| 6 | + ### Variables |
| 7 | + ### |
| 8 | + inputs: |
| 9 | + name: |
| 10 | + description: 'Name of the service to build (used for job names only).' |
| 11 | + required: true |
| 12 | + type: string |
| 13 | + enabled: |
| 14 | + description: 'Determines wheather this workflow is enabled at all (will run or skip).' |
| 15 | + required: true |
| 16 | + type: boolean |
| 17 | + can_deploy: |
| 18 | + description: 'Determines wheather this workflow will also deploy (login and push).' |
| 19 | + required: true |
| 20 | + type: boolean |
| 21 | + matrix_version: |
| 22 | + description: 'The version build matrix as JSON string (list of objects [{NAME,VERSION}]).' |
| 23 | + required: true |
| 24 | + type: string |
| 25 | + matrix_arch: |
| 26 | + description: 'The architecture build matrix as JSON string (list of architectures).' |
| 27 | + required: true |
| 28 | + type: string |
| 29 | + matrix_refs: |
| 30 | + description: 'The ref build matrix as JSON string (list of git refs to build/deploy).' |
| 31 | + required: false |
| 32 | + type: string |
| 33 | + ### |
| 34 | + ### Secrets |
| 35 | + ### |
| 36 | + secrets: |
| 37 | + dockerhub_username: |
| 38 | + description: 'The username for Dockerhub.' |
| 39 | + required: false |
| 40 | + dockerhub_password: |
| 41 | + description: 'The password for Dockerhub.' |
| 42 | + required: false |
| 43 | + |
| 44 | +jobs: |
| 45 | + |
| 46 | + # ----------------------------------------------------------------------------------------------- |
| 47 | + # JOB (1/3): CONFIGURE |
| 48 | + # ----------------------------------------------------------------------------------------------- |
| 49 | + configure: |
| 50 | + name: Configure |
| 51 | + runs-on: ubuntu-latest |
| 52 | + outputs: |
| 53 | + can_login: ${{ steps.set-login.outputs.can_login }} |
| 54 | + has_refs: ${{ steps.set-matrix.outputs.has_refs }} |
| 55 | + matrix_build: ${{ steps.set-matrix.outputs.matrix_build }} |
| 56 | + matrix_deploy: ${{ steps.set-matrix.outputs.matrix_deploy }} |
| 57 | + manifest: ${{ steps.set-manifest-arch.outputs.manifest }} |
| 58 | + if: inputs.enabled |
| 59 | + steps: |
| 60 | + - name: "[Set-Output] Set Docker login capabilities" |
| 61 | + id: set-login |
| 62 | + shell: bash |
| 63 | + run: | |
| 64 | + if [ "${{ env.ENV_USER }}" = '' ] || [ "${{ env.ENV_PASS }}" = '' ]; then |
| 65 | + echo "::set-output name=can_login::0" |
| 66 | + else |
| 67 | + echo "::set-output name=can_login::1" |
| 68 | + fi |
| 69 | + env: |
| 70 | + ENV_USER: ${{ secrets.dockerhub_username }} |
| 71 | + ENV_PASS: ${{ secrets.dockerhub_password }} |
| 72 | + |
| 73 | + - name: "[Set-Output] Set Build & Deploy Matrix" |
| 74 | + id: set-matrix |
| 75 | + shell: bash |
| 76 | + run: | |
| 77 | + if [ "${{ inputs.refs }}" != "" ]; then |
| 78 | + MATRIX_BUILD="$( \ |
| 79 | + jq -M -c \ |
| 80 | + --argjson refs '${{ inputs.matrix_refs }}' \ |
| 81 | + --argjson arches '${{ inputs.matrix_arch }}' \ |
| 82 | + 'map({name:.NAME, version:.VERSION, arch:$arches[], refs:$refs[]})' <<<'${{ inputs.matrix_version }}' \ |
| 83 | + )" |
| 84 | + MATRIX_DEPLOY="$( \ |
| 85 | + jq -M -c \ |
| 86 | + --argjson refs '${{ inputs.matrix_refs }}' \ |
| 87 | + 'map({name:.NAME, version:.VERSION, refs:$refs[]})' <<<'${{ inputs.matrix_version }}' \ |
| 88 | + )" |
| 89 | + echo "::set-output name=matrix_build::${MATRIX_BUILD}" |
| 90 | + echo "::set-output name=matrix_deploy::${MATRIX_DEPLOY}" |
| 91 | + echo "::set-output name=has_refs::1" |
| 92 | + else |
| 93 | + MATRIX_BUILD="$( \ |
| 94 | + jq -M -c \ |
| 95 | + --argjson arches '${{ inputs.matrix_arch }}' \ |
| 96 | + 'map({name:.NAME, version:.VERSION, arch:$arches[]})' <<<'${{ inputs.matrix_version }}' \ |
| 97 | + )" |
| 98 | + MATRIX_DEPLOY="$( \ |
| 99 | + jq -M -c \ |
| 100 | + 'map({name:.NAME, version:.VERSION})' <<<'${{ inputs.matrix_version }}' \ |
| 101 | + )" |
| 102 | + echo "::set-output name=matrix_build::${MATRIX_BUILD}" |
| 103 | + echo "::set-output name=matrix_deploy::${MATRIX_DEPLOY}" |
| 104 | + echo "::set-output name=has_refs::1" |
| 105 | + fi |
| 106 | +
|
| 107 | + - name: "[Set-Output] Set Manifest 'Arch'" |
| 108 | + id: set-manifest-arch |
| 109 | + run: | |
| 110 | + ARCH="$( echo ${{ inputs.arch }} | sed 's/"//g' | sed 's/\[//g' | sed 's/\]//g' | sed 's/ //g' )" |
| 111 | + echo "::set-output name=manifest::${ARCH}" |
| 112 | +
|
| 113 | + - name: "[DEBUG] Workflow Inputs" |
| 114 | + shell: bash |
| 115 | + run: | |
| 116 | + echo 'name: ${{ inputs.name }} ' |
| 117 | + echo 'enabled: ${{ inputs.enabled }} ' |
| 118 | + echo 'can_deploy: ${{ inputs.can_deploy }} ' |
| 119 | + echo 'matrix_version: ${{ inputs.matrix_version }} ' |
| 120 | + echo 'matrix_refs: ${{ inputs.matrix_refs }} ' |
| 121 | + echo 'matrix_arch: ${{ inputs.matrix_arch }} ' |
| 122 | +
|
| 123 | + - name: "[DEBUG] Determined Settings" |
| 124 | + shell: bash |
| 125 | + run: | |
| 126 | + echo 'can_login=${{ steps.set-login.outputs.can_login }}' |
| 127 | + echo 'has_refs=${{ steps.set-matrix.outputs.has_refs }}' |
| 128 | + echo 'matrix_build=${{ steps.set-matrix.outputs.matrix_build }}' |
| 129 | + echo 'matrix_deploy=${{ steps.set-matrix.outputs.matrix_deploy }}' |
| 130 | + echo 'manifest=${{ steps.set-manifest-arch.outputs.manifest }}' |
| 131 | +
|
| 132 | + # ----------------------------------------------------------------------------------------------- |
| 133 | + # JOB (2/3): BUILD |
| 134 | + # ----------------------------------------------------------------------------------------------- |
| 135 | + build: |
| 136 | + needs: [configure] |
| 137 | + name: Build ${{ inputs.name }}-${{ matrix.version }} (${{ matrix.arch }}) ${{ matrix.refs }} |
| 138 | + runs-on: ubuntu-latest |
| 139 | + strategy: |
| 140 | + fail-fast: false |
| 141 | + matrix: |
| 142 | + include: ${{ fromJson(needs.configure.outputs.matrix_build) }} |
| 143 | + if: inputs.enabled |
| 144 | + steps: |
| 145 | + # ------------------------------------------------------------ |
| 146 | + # Setup repository |
| 147 | + # ------------------------------------------------------------ |
| 148 | + - name: "[SETUP] Checkout repository (current)" |
| 149 | + uses: actions/checkout@v2 |
| 150 | + with: |
| 151 | + fetch-depth: 0 |
| 152 | + if: needs.configure.outputs.has_refs == 0 |
| 153 | + |
| 154 | + - name: "[SETUP] Checkout repository (ref: ${{ matrix.refs }})" |
| 155 | + uses: actions/checkout@v2 |
| 156 | + with: |
| 157 | + fetch-depth: 0 |
| 158 | + ref: ${{ matrix.refs }} |
| 159 | + if: needs.configure.outputs.has_refs != 0 |
| 160 | + |
| 161 | + - name: "[SETUP] Setup QEMU environment" |
| 162 | + uses: docker/setup-qemu-action@v1 |
| 163 | + with: |
| 164 | + image: tonistiigi/binfmt:latest |
| 165 | + platforms: all |
| 166 | + |
| 167 | + - name: "[SETUP] Determine Docker tag" |
| 168 | + id: tag |
| 169 | + uses: cytopia/docker-tag-action@v0.4.7 |
| 170 | + |
| 171 | + - name: "[SETUP] Determine Docker login" |
| 172 | + run: | |
| 173 | + echo ${{ needs.configure.outputs.can_login }} |
| 174 | +
|
| 175 | + # ------------------------------------------------------------ |
| 176 | + # Build |
| 177 | + # ------------------------------------------------------------ |
| 178 | + - name: Build |
| 179 | + uses: cytopia/shell-command-retry-action@v0.1.2 |
| 180 | + with: |
| 181 | + command: | |
| 182 | + make build ARCH=${{ matrix.arch }} |
| 183 | +
|
| 184 | + # ------------------------------------------------------------ |
| 185 | + # Test |
| 186 | + # ------------------------------------------------------------ |
| 187 | + - name: Test |
| 188 | + uses: cytopia/shell-command-retry-action@v0.1.2 |
| 189 | + with: |
| 190 | + command: | |
| 191 | + make test ARCH=${{ matrix.arch }} |
| 192 | +
|
| 193 | + # ------------------------------------------------------------ |
| 194 | + # Deploy |
| 195 | + # ------------------------------------------------------------ |
| 196 | + - name: Docker login |
| 197 | + uses: docker/login-action@v1 |
| 198 | + with: |
| 199 | + username: ${{ secrets.dockerhub_username }} |
| 200 | + password: ${{ secrets.dockerhub_password }} |
| 201 | + if: needs.configure.outputs.can_login == 1 && inputs.can_deploy |
| 202 | + |
| 203 | + - name: Docker push architecture image |
| 204 | + uses: cytopia/shell-command-retry-action@v0.1.2 |
| 205 | + with: |
| 206 | + command: | |
| 207 | + make push-arch TAG=${{ steps.tag.outputs.docker-tag }} ARCH=${{ matrix.arch }} |
| 208 | + if: needs.configure.outputs.can_login == 1 && inputs.can_deploy |
| 209 | + |
| 210 | + # ----------------------------------------------------------------------------------------------- |
| 211 | + # JOB (3/3): DEPLOY |
| 212 | + # ----------------------------------------------------------------------------------------------- |
| 213 | + deploy: |
| 214 | + needs: [configure, build] |
| 215 | + name: Deploy ${{ inputs.name }}-${{ matrix.version }} ${{ matrix.refs }} |
| 216 | + runs-on: ubuntu-latest |
| 217 | + strategy: |
| 218 | + fail-fast: false |
| 219 | + matrix: |
| 220 | + include: ${{ fromJson(needs.configure.outputs.matrix_deploy) }} |
| 221 | + if: inputs.enabled && needs.configure.outputs.can_login == 1 && inputs.can_deploy |
| 222 | + steps: |
| 223 | + # ------------------------------------------------------------ |
| 224 | + # Setup repository |
| 225 | + # ------------------------------------------------------------ |
| 226 | + - name: "[SETUP] Checkout repository (current)" |
| 227 | + uses: actions/checkout@v2 |
| 228 | + with: |
| 229 | + fetch-depth: 0 |
| 230 | + if: needs.configure.outputs.has_refs == 0 |
| 231 | + |
| 232 | + - name: "[SETUP] Checkout repository (ref: ${{ matrix.refs }})" |
| 233 | + uses: actions/checkout@v2 |
| 234 | + with: |
| 235 | + fetch-depth: 0 |
| 236 | + ref: ${{ matrix.refs }} |
| 237 | + if: needs.configure.outputs.has_refs != 0 |
| 238 | + |
| 239 | + - name: "[SETUP] Determine Docker tag" |
| 240 | + id: tag |
| 241 | + uses: cytopia/docker-tag-action@v0.4.7 |
| 242 | + |
| 243 | + # ------------------------------------------------------------ |
| 244 | + # Deploy |
| 245 | + # ------------------------------------------------------------ |
| 246 | + - name: "[DEPLOY] Login" |
| 247 | + uses: docker/login-action@v1 |
| 248 | + with: |
| 249 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 250 | + password: ${{ secrets.DOCKERHUB_PASSWORD }} |
| 251 | + |
| 252 | + - name: "[DEPLOY] Create Docker manifest" |
| 253 | + uses: cytopia/shell-command-retry-action@v0.1.2 |
| 254 | + with: |
| 255 | + command: | |
| 256 | + make manifest-create TAG=${{ steps.tag.outputs.docker-tag }} ARCH="${{ needs.configure.outputs.manifest }}" |
| 257 | +
|
| 258 | + - name: "[DEPLOY] Publish Docker manifest: ${{ steps.tag.outputs.docker-tag }}" |
| 259 | + uses: cytopia/shell-command-retry-action@v0.1.2 |
| 260 | + with: |
| 261 | + command: | |
| 262 | + make manifest-push TAG=${{ steps.tag.outputs.docker-tag }} |
0 commit comments