From 19f76b0617c286198dacf448908a3f41108b833c Mon Sep 17 00:00:00 2001 From: Dmitry Sergeev Date: Wed, 15 May 2024 18:03:39 +0400 Subject: [PATCH] WIP --- .github/workflows/build.yaml | 132 +++++++++++++++++------------------ Dockerfile | 19 +++++ 2 files changed, 84 insertions(+), 67 deletions(-) create mode 100644 Dockerfile diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 69d3a14..cdf5658 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -4,13 +4,34 @@ on: tags: - 'v*' jobs: - build_and_release: - name: build_and_release + test_build: + name: test_build + runs-on: ubuntu-22.04 + steps: + - name: clone + uses: actions/checkout@main + - name: install depends + run: | + go mod download + mkdir artifacts + - name: Setup Go + uses: actions/setup-go@main + with: + go-version: 1.22 + - name: build + run: | + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o artifacts/custom-exporter + + create_release: + name: create_release runs-on: ubuntu-22.04 + needs: [test_build] + outputs: + upload_url: ${{ steps.create_release.outputs.upload_url }} steps: - name: Create Release id: create_release - uses: actions/create-release@v1 + uses: actions/create-release@main env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: @@ -18,83 +39,60 @@ jobs: release_name: Release ${{ github.ref }} draft: false prerelease: false + + upload_release: + needs: [create_release] + name: build_and_release + runs-on: ubuntu-22.04 + strategy: + matrix: + arch: [amd64, arm64, arm7] + steps: - name: clone uses: actions/checkout@main - name: install depends run: | - go mod tidy + go mod download mkdir artifacts - name: Setup Go - uses: actions/setup-go@v2 + uses: actions/setup-go@main with: go-version: 1.22 - - name: build run: | - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o artifacts/custom-exporter-amd64 . - CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags="-s -w" -o artifacts/custom-exporter-386 . - CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o artifacts/custom-exporter-arm64 . - CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 go build -ldflags="-s -w" -o artifacts/custom-exporter-arm7 . - CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 go build -ldflags="-s -w" -o artifacts/custom-exporter-arm6 . - CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=5 go build -ldflags="-s -w" -o artifacts/custom-exporter-arm5 . - - - name: Upload relerase amd64 - id: upload-release-asset-amd64 - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./artifacts/custom-exporter-amd64 - asset_name: custom-exporter-amd64 - asset_content_type: application/x-elf - - name: Upload relerase 386 - id: upload-release-asset-386 - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./artifacts/custom-exporter-386 - asset_name: custom-exporter-386 - asset_content_type: application/x-elf - - name: Upload relerase arm64 - id: upload-release-asset-arm64 - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./artifacts/custom-exporter-arm64 - asset_name: custom-exporter-arm64 - asset_content_type: application/x-elf - - name: Upload relerase arm7 - id: upload-release-asset-arm7 - uses: actions/upload-release-asset@v1 + CGO_ENABLED=0 GOOS=linux GOARCH=${{ matrix.arch }} go build -ldflags="-s -w" -o artifacts/custom-exporter-${{ matrix.arch }} . + - name: Upload release + id: upload-release-asset + uses: actions/upload-release-asset@main env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./artifacts/custom-exporter-arm7 - asset_name: custom-exporter-arm7 + upload_url: ${{ needs.create_release.outputs.upload_url }} + asset_path: ./artifacts/custom-exporter-${{ matrix.arch }} + asset_name: custom-exporter-${{ matrix.arch }} asset_content_type: application/x-elf - - name: Upload relerase arm6 - id: upload-release-asset-arm6 - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + container_image: + name: container_image + runs-on: ubuntu-22.04 + needs: [upload_release] + steps: + - name: Checkout code + uses: actions/checkout@main + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Login to GitHub Container Registry + uses: docker/login-action@v1 with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./artifacts/custom-exporter-arm6 - asset_name: custom-exporter-arm6 - asset_content_type: application/x-elf - - name: Upload relerase arm5 - id: upload-release-asset-arm5 - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build and push Docker image + uses: docker/build-push-action@v2 with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./artifacts/custom-exporter-arm5 - asset_name: custom-exporter-arm5 - asset_content_type: application/x-elf \ No newline at end of file + context: . + push: true + platforms: linux/amd64,linux/arm64,linux/arm/v7 + tags: ghcr.io/${{ github.repository_owner }}/custom-exporter:latest \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1939926 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM --platform=$BUILDPLATFORM golang:1.22 AS build + +WORKDIR /app + +COPY go.mod go.sum ./ +RUN go mod download + +COPY ./main.go ./ +COPY ./pkg ./pkg + +ARG TARGETPLATFORM +RUN echo "Building BUILDPLATFORM: '${BUILDPLATFORM}', TARGETPLATFORM: '$TARGETPLATFORM'"; +RUN GOOS=$(echo $TARGETPLATFORM | cut -d / -f1) \ + GOARCH=$(echo $TARGETPLATFORM | cut -d / -f2) \ + CGO_ENABLED=0 go build -ldflags="-s -w" -o ./custom-exporter . + +FROM scratch +COPY --from=build /app/custom-exporter /custom-exporter +ENTRYPOINT ["/custom-exporter"] \ No newline at end of file