Docker Publish Dev Multiarch #590
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Docker Publish Dev Multiarch | |
| on: | |
| workflow_dispatch: | |
| push: | |
| branches: | |
| - master | |
| permissions: | |
| contents: read | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version-file: go.mod | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to Docker Hub | |
| run: echo "${{ secrets.DOCKER_PW }}" | docker login -u "${{ secrets.DOCKER_USER }}" --password-stdin | |
| - name: Cross-compile binaries | |
| run: | | |
| LDFLAGS="-s -w \ | |
| -X 'github.com/forceu/gokapi/internal/environment.IsDocker=true' \ | |
| -X 'github.com/forceu/gokapi/internal/environment.Builder=Project Docker File' \ | |
| -X 'github.com/forceu/gokapi/internal/environment.BuildTime=$(date)'" | |
| go generate ./... | |
| declare -A TARGETS=( | |
| ["linux/amd64"]="" | |
| ["linux/386"]="" | |
| ["linux/arm64"]="" | |
| ["linux/arm/v7"]="" | |
| ["linux/arm/v6"]="" | |
| ["linux/riscv64"]="" | |
| ) | |
| for platform in "${!TARGETS[@]}"; do | |
| OS="${platform%%/*}" | |
| REST="${platform#*/}" | |
| ARCH="${REST%%/*}" | |
| VARIANT="${REST##*/}" | |
| [ "$VARIANT" = "$ARCH" ] && VARIANT="" | |
| GOARM="" | |
| [ -n "$VARIANT" ] && GOARM="${VARIANT//v/}" | |
| OUT="bin/gokapi-${ARCH}${VARIANT:+-$VARIANT}" | |
| echo "Building $OUT (GOARCH=$ARCH GOARM=$GOARM)..." | |
| CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH GOARM=$GOARM \ | |
| go build -ldflags "$LDFLAGS" -o "$OUT" github.com/forceu/gokapi/cmd/gokapi & | |
| done | |
| wait | |
| echo "All binaries built." | |
| - name: Build and push multi-arch image | |
| run: | | |
| docker buildx create --use --name multiarch-builder | |
| declare -A PLATFORMS=( | |
| ["linux/amd64"]="gokapi-amd64" | |
| ["linux/386"]="gokapi-386" | |
| ["linux/arm64"]="gokapi-arm64" | |
| ["linux/arm/v7"]="gokapi-arm-v7" | |
| ["linux/arm/v6"]="gokapi-arm-v6" | |
| ["linux/riscv64"]="gokapi-riscv64" | |
| ) | |
| for platform in "${!PLATFORMS[@]}"; do | |
| binary="${PLATFORMS[$platform]}" | |
| echo "Building $platform with binary $binary..." | |
| docker buildx build \ | |
| --push \ | |
| --platform "$platform" \ | |
| --build-arg BINARY_NAME="$binary" \ | |
| --tag "f0rc3/gokapi:tmp-dev-${binary}" \ | |
| --file Dockerfile.multiarch \ | |
| . | |
| done | |
| docker buildx imagetools create \ | |
| --tag f0rc3/gokapi:latest-dev \ | |
| $(for binary in "${PLATFORMS[@]}"; do | |
| echo "f0rc3/gokapi:tmp-dev-${binary}" | |
| done) | |
| # Delete intermediate tags | |
| TOKEN=$(curl -s -X POST \ | |
| -H "Content-Type: application/json" \ | |
| -d "{\"username\": \"${{ secrets.DOCKER_USER }}\", \"password\": \"${{ secrets.DOCKER_PW }}\"}" \ | |
| https://hub.docker.com/v2/users/login/ | jq -r .token) | |
| for binary in "${PLATFORMS[@]}"; do | |
| curl -s -X DELETE \ | |
| -H "Authorization: JWT ${TOKEN}" \ | |
| "https://hub.docker.com/v2/repositories/f0rc3/gokapi/tags/tmp-dev-${binary}/" | |
| done |