From 60d2bb943d05f50de6d004829dc1999337037eea Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Wed, 4 Jan 2023 11:54:01 +0100 Subject: [PATCH 1/4] Add container image build --- .github/workflows/images.yml | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/workflows/images.yml diff --git a/.github/workflows/images.yml b/.github/workflows/images.yml new file mode 100644 index 0000000..e4b3b9b --- /dev/null +++ b/.github/workflows/images.yml @@ -0,0 +1,39 @@ +name: Publish container images + +on: + push: + branches: + - main + +jobs: + containers: + name: Build and push container image registries + runs-on: ubuntu-latest + + permissions: + packages: write + contents: read + + steps: + - name: Check out the repo + uses: actions/checkout@v3 + + - name: Log in to the Container registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v4 + with: + images: ghcr.io/${{ github.repository }} + + - name: Build and push Docker images + uses: docker/build-push-action@v3 + with: + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} From 35f41552e60425c2f885e07af1b9cc13218efeaa Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Wed, 4 Jan 2023 12:20:07 +0100 Subject: [PATCH 2/4] Dockerfile: Avoid working with git The containers should have labels with that information. --- Dockerfile | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index c31d021..fd4c2cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,11 @@ FROM golang:1.19.4 as builder WORKDIR /build -COPY go.mod . -COPY go.sum . -RUN go mod download +COPY . /build/ -# Build -COPY . . -RUN git rev-parse --short HEAD -RUN GIT_COMMIT=$(git rev-parse --short HEAD) && \ - CGO_ENABLED=0 go build -o app -ldflags "-X main.GitCommit=${GIT_COMMIT}" +RUN go mod download +RUN CGO_ENABLED=0 go build -o app -FROM alpine:latest +FROM alpine:latest RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/* WORKDIR /app COPY --from=builder /build/app /app From 03d012e1603be187625c047685485ecfa368c35a Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Wed, 4 Jan 2023 12:20:39 +0100 Subject: [PATCH 3/4] workflows: Only push container on default branch --- .github/workflows/images.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/images.yml b/.github/workflows/images.yml index e4b3b9b..f9f567a 100644 --- a/.github/workflows/images.yml +++ b/.github/workflows/images.yml @@ -4,6 +4,7 @@ on: push: branches: - main + pull_request: jobs: containers: @@ -24,6 +25,7 @@ jobs: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) - name: Extract metadata (tags, labels) for Docker id: meta @@ -34,6 +36,6 @@ jobs: - name: Build and push Docker images uses: docker/build-push-action@v3 with: - push: true + push: ${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} From 987d7a60e2c57751db6fabdf9ab702811ab60dc9 Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Wed, 4 Jan 2023 12:30:08 +0100 Subject: [PATCH 4/4] workflows: Also publish container on tags --- .github/workflows/images.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/images.yml b/.github/workflows/images.yml index f9f567a..019b9e2 100644 --- a/.github/workflows/images.yml +++ b/.github/workflows/images.yml @@ -4,6 +4,8 @@ on: push: branches: - main + tags: + - 'v*' pull_request: jobs: @@ -25,7 +27,7 @@ jobs: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) + if: startsWith(github.ref, 'refs/tags/v') || github.ref == format('refs/heads/{0}', github.event.repository.default_branch) - name: Extract metadata (tags, labels) for Docker id: meta @@ -36,6 +38,6 @@ jobs: - name: Build and push Docker images uses: docker/build-push-action@v3 with: - push: ${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} + push: ${{ startsWith(github.ref, 'refs/tags/v') || github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }}