|
| 1 | +name: Docker Hub |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - "master" |
| 6 | + - "dev" |
| 7 | + - "dev/*" |
| 8 | + tags: |
| 9 | + - "[0-9]+.[0-9]+.[0-9]+" |
| 10 | + |
| 11 | +env: |
| 12 | + IMAGE_NAME: infrastructureascode/hello-world |
| 13 | + |
| 14 | +jobs: |
| 15 | + check: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Check out the repo |
| 19 | + uses: actions/checkout@v3 |
| 20 | + |
| 21 | + - name: Set up Go |
| 22 | + uses: actions/setup-go@v4 |
| 23 | + with: |
| 24 | + go-version: '^1.20.0' |
| 25 | + |
| 26 | + - name: gofmt |
| 27 | + run: | |
| 28 | + test -z `gofmt -s -l .` |
| 29 | +
|
| 30 | + - name: go vet |
| 31 | + run: | |
| 32 | + go vet ./... |
| 33 | +
|
| 34 | + - name: golangci-lint |
| 35 | + uses: golangci/golangci-lint-action@v3 |
| 36 | + |
| 37 | + - uses: dominikh/[email protected] |
| 38 | + with: |
| 39 | + version: "2022.1.3" |
| 40 | + |
| 41 | + - name: Run tests |
| 42 | + run: | |
| 43 | + GIN_MODE=debug go test |
| 44 | +
|
| 45 | + build: |
| 46 | + runs-on: ubuntu-latest |
| 47 | + needs: |
| 48 | + - check |
| 49 | + permissions: |
| 50 | + contents: read |
| 51 | + packages: write |
| 52 | + strategy: |
| 53 | + fail-fast: false |
| 54 | + matrix: |
| 55 | + include: |
| 56 | + - platform: linux/amd64 |
| 57 | + goarch: amd64 |
| 58 | + dockerenv: |
| 59 | + - platform: linux/arm/v6 |
| 60 | + goarch: arm |
| 61 | + dockerenv: -e QEMU_CPU=arm1176 |
| 62 | + - platform: linux/arm/v8 |
| 63 | + goarch: arm64 |
| 64 | + dockerenv: -e QEMU_CPU=cortex-a53 |
| 65 | + |
| 66 | + steps: |
| 67 | + - name: Check out the repo |
| 68 | + uses: actions/checkout@v3 |
| 69 | + |
| 70 | + # Cross-compile the golang app first and generate |
| 71 | + # the sha1sum to help with the provenance of the |
| 72 | + # binary in the images. |
| 73 | + - uses: actions/setup-go@v4 |
| 74 | + with: |
| 75 | + go-version: '^1.20.0' |
| 76 | + |
| 77 | + - name: Compile the app |
| 78 | + run: | |
| 79 | + export GOOS=linux GOARCH=${{ matrix.goarch }} |
| 80 | + CGO_ENABLED=0 go build -a -o hello_world |
| 81 | + - name: Show binary info |
| 82 | + run: | |
| 83 | + file hello_world |
| 84 | + sha1sum hello_world |
| 85 | +
|
| 86 | + - name: Log in to the container registry |
| 87 | + uses: docker/login-action@v2 |
| 88 | + with: |
| 89 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 90 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 91 | + |
| 92 | + - name: Set up QEMU |
| 93 | + uses: docker/setup-qemu-action@v2 |
| 94 | + |
| 95 | + # do a local build to make sure the image runs |
| 96 | + # before it is pushed to the registry |
| 97 | + - name: Build and run image locally as cross-compile test |
| 98 | + run: | |
| 99 | + docker build --rm \ |
| 100 | + --platform ${{ matrix.platform }} \ |
| 101 | + --tag test-image \ |
| 102 | + . |
| 103 | + docker run --rm \ |
| 104 | + --tty \ |
| 105 | + ${{ matrix.dockerenv }} \ |
| 106 | + test-image \ |
| 107 | + --version |
| 108 | +
|
| 109 | + # Build and push to registry since we have validated |
| 110 | + # that the image can be run |
| 111 | + - name: Set up Docker Buildx |
| 112 | + uses: docker/setup-buildx-action@v2 |
| 113 | + |
| 114 | + - name: Extract metadata (tags, labels) for Docker |
| 115 | + id: meta |
| 116 | + uses: docker/metadata-action@v4 |
| 117 | + with: |
| 118 | + images: ${{ env.IMAGE_NAME }} |
| 119 | + |
| 120 | + - name: Build and push Docker image |
| 121 | + id: build |
| 122 | + uses: docker/build-push-action@v4 |
| 123 | + with: |
| 124 | + context: . |
| 125 | + platforms: ${{ matrix.platform }} |
| 126 | + push: ${{ github.event_name != 'pull_request' }} |
| 127 | + labels: ${{ steps.meta.outputs.labels }} |
| 128 | + outputs: type=image,name=${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true |
| 129 | + |
| 130 | + - name: Export digest |
| 131 | + run: | |
| 132 | + mkdir -p /tmp/digests |
| 133 | + digest="${{ steps.build.outputs.digest }}" |
| 134 | + touch "/tmp/digests/${digest#sha256:}" |
| 135 | +
|
| 136 | + - name: Upload digest |
| 137 | + uses: actions/upload-artifact@v3 |
| 138 | + with: |
| 139 | + name: digests |
| 140 | + path: /tmp/digests/* |
| 141 | + if-no-files-found: error |
| 142 | + retention-days: 1 |
| 143 | + |
| 144 | + merge: |
| 145 | + runs-on: ubuntu-latest |
| 146 | + needs: |
| 147 | + - build |
| 148 | + permissions: |
| 149 | + contents: read |
| 150 | + packages: write |
| 151 | + |
| 152 | + steps: |
| 153 | + - name: Download digests |
| 154 | + uses: actions/download-artifact@v3 |
| 155 | + with: |
| 156 | + name: digests |
| 157 | + path: /tmp/digests |
| 158 | + |
| 159 | + - name: Set up Docker Buildx |
| 160 | + uses: docker/setup-buildx-action@v2 |
| 161 | + |
| 162 | + - name: Docker meta |
| 163 | + id: meta |
| 164 | + uses: docker/metadata-action@v4 |
| 165 | + with: |
| 166 | + images: ${{ env.IMAGE_NAME }} |
| 167 | + tags: | |
| 168 | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} |
| 169 | + type=raw,value=${{ github.ref_name }} |
| 170 | +
|
| 171 | + - name: Log in to the container registry |
| 172 | + uses: docker/login-action@v2 |
| 173 | + with: |
| 174 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 175 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 176 | + |
| 177 | + - run: | |
| 178 | + ls -l |
| 179 | + working-directory: /tmp/digests |
| 180 | +
|
| 181 | + - name: Create manifest list and push |
| 182 | + working-directory: /tmp/digests |
| 183 | + run: | |
| 184 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 185 | + $(printf '${{ env.IMAGE_NAME }}@sha256:%s ' *) |
| 186 | + - |
| 187 | + name: Inspect image |
| 188 | + run: | |
| 189 | + docker buildx imagetools inspect ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} |
| 190 | +
|
| 191 | + # run the Buildx-built images as a sanity check |
| 192 | + test-run: |
| 193 | + runs-on: ubuntu-latest |
| 194 | + needs: |
| 195 | + - merge |
| 196 | + permissions: |
| 197 | + contents: read |
| 198 | + packages: read |
| 199 | + strategy: |
| 200 | + fail-fast: false |
| 201 | + matrix: |
| 202 | + include: |
| 203 | + - platform: linux/amd64 |
| 204 | + dockerenv: |
| 205 | + - platform: linux/arm/v6 |
| 206 | + dockerenv: -e QEMU_CPU=arm1176 |
| 207 | + - platform: linux/arm/v8 |
| 208 | + dockerenv: -e QEMU_CPU=cortex-a53 |
| 209 | + |
| 210 | + steps: |
| 211 | + - name: Set up QEMU |
| 212 | + uses: docker/setup-qemu-action@v2 |
| 213 | + |
| 214 | + - name: Docker meta |
| 215 | + id: meta |
| 216 | + uses: docker/metadata-action@v4 |
| 217 | + with: |
| 218 | + images: ${{ env.IMAGE_NAME }} |
| 219 | + tags: | |
| 220 | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} |
| 221 | + type=raw,value=${{ github.ref_name }} |
| 222 | +
|
| 223 | + - name: Docker run |
| 224 | + run: | |
| 225 | + docker run --rm \ |
| 226 | + --tty \ |
| 227 | + --platform ${{ matrix.platform }} \ |
| 228 | + ${{ matrix.dockerenv }} \ |
| 229 | + ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} \ |
| 230 | + --version |
0 commit comments