Skip to content

Commit

Permalink
Updating CI for repo parity
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelattwood committed Jan 25, 2023
1 parent 998b863 commit cfed02b
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 76 deletions.
22 changes: 14 additions & 8 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Exporter Release
name: Release
on:
push:
tags:
Expand Down Expand Up @@ -28,12 +28,18 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_CLI_TOKEN }}

- name: Generate goreleaser release
uses: goreleaser/goreleaser-action@v4
- name: Get Image Tags
id: tags
run: |
version=$(sed 's/^v//' <<< ${{ github.ref_name }})
echo tags="latest,${version}" >> $GITHUB_OUTPUT
- name: Build and Push
uses: docker/bake-action@v2
env:
IMAGE_REGISTRY: "natsio"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAGS: "${{ steps.tags.outputs.tags }}"
REGISTRY: "natsio"
with:
distribution: goreleaser
version: latest
args: release --rm-dist
push: true
set:
goreleaser.args.GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
68 changes: 0 additions & 68 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,71 +70,3 @@ nfpms:
license: Apache 2.0
formats:
- deb

dockers:
- use: buildx
ids:
- prometheus-nats-exporter
dockerfile: docker/linux/Dockerfile
goos: linux
goarch: amd64
image_templates:
- "{{ .Env.IMAGE_REPOSITORY }}:{{ if .IsSnapshot }}{{ .Version }}{{ else }}{{ .Tag }}{{ end }}-amd64"
build_flag_templates:
- "--platform=linux/amd64"
- "--label=org.opencontainers.image.created={{ .Date }}"
- "--label=org.opencontainers.image.title={{ .ProjectName }}"
- "--label=org.opencontainers.image.revision={{ .FullCommit }}"
- "--label=org.opencontainers.image.version={{ .Version }}"
- use: buildx
ids:
- prometheus-nats-exporter
dockerfile: docker/linux/Dockerfile
goos: linux
goarch: arm64
image_templates:
- "{{ .Env.IMAGE_REPOSITORY }}:{{ if .IsSnapshot }}{{ .Version }}{{ else }}{{ .Tag }}{{ end }}-arm64"
build_flag_templates:
- "--platform=linux/arm64"
- "--label=org.opencontainers.image.created={{ .Date }}"
- "--label=org.opencontainers.image.title={{ .ProjectName }}"
- "--label=org.opencontainers.image.revision={{ .FullCommit }}"
- "--label=org.opencontainers.image.version={{ .Version }}"
- use: buildx
ids:
- prometheus-nats-exporter
dockerfile: docker/linux/Dockerfile
goos: linux
goarch: arm
goarm: 6
image_templates:
- "{{ .Env.IMAGE_REPOSITORY }}:{{ if .IsSnapshot }}{{ .Version }}{{ else }}{{ .Tag }}{{ end }}-armv6"
build_flag_templates:
- "--platform=linux/arm/v6"
- "--label=org.opencontainers.image.created={{ .Date }}"
- "--label=org.opencontainers.image.title={{ .ProjectName }}"
- "--label=org.opencontainers.image.revision={{ .FullCommit }}"
- "--label=org.opencontainers.image.version={{ .Version }}"
- use: buildx
ids:
- prometheus-nats-exporter
dockerfile: docker/linux/Dockerfile
goos: linux
goarch: arm
goarm: 7
image_templates:
- "{{ .Env.IMAGE_REPOSITORY }}:{{ if .IsSnapshot }}{{ .Version }}{{ else }}{{ .Tag }}{{ end }}-armv7"
build_flag_templates:
- "--platform=linux/arm/v7"
- "--label=org.opencontainers.image.created={{ .Date }}"
- "--label=org.opencontainers.image.title={{ .ProjectName }}"
- "--label=org.opencontainers.image.revision={{ .FullCommit }}"
- "--label=org.opencontainers.image.version={{ .Version }}"

docker_manifests:
- name_template: "{{ .Env.IMAGE_REPOSITORY }}:{{ if .IsSnapshot }}{{ .Version }}{{ else }}{{ .Tag }}{{ end }}"
image_templates:
- "{{ .Env.IMAGE_REPOSITORY }}:{{ if .IsSnapshot }}{{ .Version }}{{ else }}{{ .Tag }}{{ end }}-amd64"
- "{{ .Env.IMAGE_REPOSITORY }}:{{ if .IsSnapshot }}{{ .Version }}{{ else }}{{ .Tag }}{{ end }}-arm64"
- "{{ .Env.IMAGE_REPOSITORY }}:{{ if .IsSnapshot }}{{ .Version }}{{ else }}{{ .Tag }}{{ end }}-armv6"
- "{{ .Env.IMAGE_REPOSITORY }}:{{ if .IsSnapshot }}{{ .Version }}{{ else }}{{ .Tag }}{{ end }}-armv7"
38 changes: 38 additions & 0 deletions cicd/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#syntax=docker/dockerfile-upstream:1.4
ARG GO_APP

FROM base as deps

ARG GO_APP

ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT

RUN mkdir -p /go/bin /go/src

COPY --from=build /go/src/dist /go/src/dist

RUN <<EOT
set -e
apk add --no-cache ca-certificates jq
if [[ ${TARGETARCH} == "arm" ]]; then VARIANT=$(echo ${TARGETVARIANT} | sed 's/^v//'); fi
BIN_PATH=$(jq -r ".[] |select(.type == \"Binary\" and \
.name == \"${GO_APP}\" and \
.goos == \"${TARGETOS}\" and \
.goarch == \"${TARGETARCH}\" and \
(.goarm == \"${VARIANT}\" or .goarm == null)) | .path" < /go/src/dist/artifacts.json)
cp ${BIN_PATH} /go/bin
EOT

FROM base

ARG GO_APP

COPY --from=deps --chmod=755 /go/bin/${GO_APP} /usr/local/bin/${GO_APP}

COPY --from=deps /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

RUN ln -s /usr/local/bin/${GO_APP} /entrypoint

ENTRYPOINT ["/entrypoint"]
19 changes: 19 additions & 0 deletions cicd/Dockerfile_goreleaser
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#syntax=docker/dockerfile-upstream:1.4
FROM --platform=$BUILDPLATFORM goreleaser

ARG CI
ARG GITHUB_TOKEN

COPY --from=src . /go/src

RUN <<EOT
set -e
cd /go/src
FLAGS="--rm-dist"
if [[ -z ${GITHUB_TOKEN} ]]; then
if [[ ${CI} != "true" ]]; then FLAGS="${FLAGS} --skip-validate --single-target"; fi
goreleaser build ${FLAGS}
else
goreleaser release ${FLAGS}
fi
EOT
83 changes: 83 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
###################
### Variables
###################

variable REGISTRY {
default = ""
}

# Comma delimited list of tags
variable TAGS {
default = "latest"
}

variable CI {
default = false
}

variable image_base {
default = "docker-image://alpine:3.17.1"
}

variable image_goreleaser {
default = "docker-image://goreleaser/goreleaser:v1.14.1"
}

###################
### Functions
###################

function "get_tags" {
params = [image]
result = [for tag in split(",", TAGS) : join("/", compact([REGISTRY, "${image}:${tag}"]))]
}

function "get_platforms_multiarch" {
params = []
result = CI ? ["linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64"] : []
}

function "get_output" {
params = []
result = CI ? ["type=registry"] : ["type=docker"]
}

###################
### Groups
###################

group "default" {
targets = [
"prometheus-nats-exporter"
]
}

###################
### Targets
###################

target "goreleaser" {
contexts = {
goreleaser = image_goreleaser
src = "."
}
args = {
CI = CI
GITHUB_TOKEN = ""
}
dockerfile = "cicd/Dockerfile_goreleaser"
}

target "prometheus-nats-exporter" {
contexts = {
base = image_base
build = "target:goreleaser"
}
args = {
GO_APP = "prometheus-nats-exporter"
}
dockerfile = "cicd/Dockerfile"
platforms = get_platforms_multiarch()
tags = get_tags("prometheus-nats-exporter")
output = get_output()
}

0 comments on commit cfed02b

Please sign in to comment.