From b60a31579e873f655c01f1d79ff648a29165538c Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 27 Jun 2024 08:27:36 +0200 Subject: [PATCH] build: build & publish with ko --- .github/workflows/build.yml | 70 +++++++++---------------------------- .ko.yaml | 10 ++++++ Dockerfile | 34 ------------------ 3 files changed, 27 insertions(+), 87 deletions(-) create mode 100644 .ko.yaml delete mode 100644 Dockerfile diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 989418b..4084187 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,65 +2,29 @@ name: build-containers on: push: branches: - - master + - main jobs: - vultr-exporter: + build-publish: name: updown-exporter runs-on: ubuntu-latest - env: - REPO: dazwilkin/updown-exporter steps: - name: Checkout uses: actions/checkout@v4 - - name: Setup - uses: docker/setup-buildx-action@v3 - - name: Login - uses: docker/login-action@v3 + + - uses: actions/setup-go@v5 with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GHCR }} - - name: Get kernel version - run: echo "VERSION=$(uname --kernel-release)" >> ${GITHUB_ENV} - - name: Exporter - uses: docker/build-push-action@v5 + go-version: stable + + - uses: ko-build/setup-ko@v0.6 + + - name: Log in to GHCR + uses: docker/login-action@v2 with: - context: . - file: ./Dockerfile - build-args: | - TOKEN=${{ secrets.GHCR }} - VERSION=${{ env.VERSION }} - COMMIT=${{ github.sha }} - tags: ghcr.io/${{ env.REPO }}:${{ github.sha }} - push: true - # - name: Cosign installer - # uses: sigstore/cosign-installer@main - # - name: Write signing key to disk (only needed for `cosign sign --key`) - # run: echo "${{ secrets.SIGNING }}" > ./cosign.key - # - name: Sign container image - # run: | - # cosign sign \ - # --key=./cosign.key \ - # --annotations="repo=${{ github.repository }}" \ - # --annotations="workflow=${{ github.workflow }}" \ - # --annotations="commit=${{ github.sha }}" \ - # --annotations="version=${{ env.VERSION }}" \ - # ghcr.io/${{ env.REPO }}:${{ github.sha }} - # env: - # COSIGN_PASSWORD: "" - - name: Revise occurrences of the image - run: | - git config --local user.email "action@github.com" - git config --local user.name "GitHub Actions" + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - for FILENAME in "./README.md" - do - echo "Replacing: ${REPO}" - sed \ - --in-place \ - "s|${{ env.REPO }}:[0-9a-f]\{40\}|${{ env.REPO }}:${{ github.sha }}|g" \ - ${FILENAME} - git add ${FILENAME} - done - git commit --message "GitHub Actions update image references" - git push origin master \ No newline at end of file + - run: | + ko build --sbom=none --bare . + env: + KO_DOCKER_REPO: ghcr.io/${GITHUB_REPOSITORY} diff --git a/.ko.yaml b/.ko.yaml new file mode 100644 index 0000000..b83b472 --- /dev/null +++ b/.ko.yaml @@ -0,0 +1,10 @@ +builds: +- id: updown-exporter + main: . + ldflags: + - -s -w + - -extldflags "-static" + +defaultPlatforms: +- linux/arm64 +- linux/amd64 diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 89c990d..0000000 --- a/Dockerfile +++ /dev/null @@ -1,34 +0,0 @@ -ARG GOLANG_VERSION=1.21 - -FROM docker.io/golang:${GOLANG_VERSION} as build - -WORKDIR /updown-exporter - -ARG COMMIT="" -ARG VERSION="" - -COPY go.mod go.mod -COPY go.sum go.sum - -RUN go mod download - -COPY main.go . -COPY collector collector -COPY updown updown - -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \ - go build \ - -ldflags "-X main.OSVersion=${VERSION} -X main.GitCommit=${COMMIT}" \ - -a -installsuffix cgo \ - -o /bin/exporter \ - . - - -FROM gcr.io/distroless/static - -LABEL org.opencontainers.image.source https://github.com/DazWilkin/updown-exporter - -COPY --from=build /bin/exporter / - -ENTRYPOINT ["/exporter"] -CMD ["--entrypoint=0.0.0.0:8080","--path=/metrics"] \ No newline at end of file