From 8014312c3a416bc3d069e2570df7f83fc807bd25 Mon Sep 17 00:00:00 2001 From: Neil Twigg Date: Wed, 1 Oct 2025 14:42:38 +0100 Subject: [PATCH] Refactor Docker nightly builds [skip ci] Signed-off-by: Neil Twigg --- .github/actions/nightly-release/action.yaml | 32 +++++++++++++---- .github/workflows/nightly.yaml | 1 + .goreleaser-nightly.yml | 40 --------------------- docker/Dockerfile.nightly | 36 +++++++++++++------ 4 files changed, 52 insertions(+), 57 deletions(-) delete mode 100644 .goreleaser-nightly.yml diff --git a/.github/actions/nightly-release/action.yaml b/.github/actions/nightly-release/action.yaml index 26cafcd870b..6c7a8bef2e3 100644 --- a/.github/actions/nightly-release/action.yaml +++ b/.github/actions/nightly-release/action.yaml @@ -10,6 +10,11 @@ inputs: description: Docker hub password required: true + name: + description: The name of the build + default: nightly + required: false + workdir: description: The working directory for actions requiring it required: true @@ -21,16 +26,31 @@ runs: shell: bash run: docker login -u "${{ inputs.hub_username }}" -p "${{ inputs.hub_password }}" + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Set up Go uses: actions/setup-go@v6 with: go-version: stable + - name: Generate build metadata + shell: sh + run: | + echo "BUILD_DATE=$(date +%Y%m%d)" >> $GITHUB_ENV + echo "GIT_COMMIT=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + echo "BUILD_NAME=$(echo ${{ inputs.name }} | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9.-]/-/g')" >> $GITHUB_ENV + - name: Build and push Docker images - # Use commit hash here to avoid a re-tagging attack, as this is a third-party action - # Commit 9ed2f89a662bf1735a48bc8557fd212fa902bebf = tag v6.1.0 - uses: goreleaser/goreleaser-action@9ed2f89a662bf1735a48bc8557fd212fa902bebf + uses: docker/build-push-action@v6 with: - workdir: "${{ inputs.workdir }}" - version: ~> v2 - args: release --skip=announce,validate --config .goreleaser-nightly.yml + context: "${{ inputs.workdir }}" + file: "${{ inputs.workdir }}/docker/Dockerfile.nightly" + build-args: | + VERSION=nightly-${{ env.BUILD_DATE }} + GIT_COMMIT=${{ env.GIT_COMMIT }} + platforms: linux/amd64,linux/arm64 + push: true + tags: | + synadia/nats-server:${{ env.BUILD_NAME }} + synadia/nats-server:${{ env.BUILD_NAME }}-${{ env.BUILD_DATE }} diff --git a/.github/workflows/nightly.yaml b/.github/workflows/nightly.yaml index fb22b5e84e0..d1b9f3cb823 100644 --- a/.github/workflows/nightly.yaml +++ b/.github/workflows/nightly.yaml @@ -29,6 +29,7 @@ jobs: - uses: ./src/github.com/nats-io/nats-server/.github/actions/nightly-release with: + name: ${{ inputs.target || 'nightly' }} workdir: src/github.com/nats-io/nats-server hub_username: "${{ secrets.DOCKER_USERNAME }}" hub_password: "${{ secrets.DOCKER_PASSWORD }}" diff --git a/.goreleaser-nightly.yml b/.goreleaser-nightly.yml deleted file mode 100644 index ead4d5e2795..00000000000 --- a/.goreleaser-nightly.yml +++ /dev/null @@ -1,40 +0,0 @@ -project_name: nats-server -version: 2 - -builds: - - main: . - id: nats-server - binary: nats-server - ldflags: - - -w -X github.com/nats-io/nats-server/v2/server.gitCommit={{.ShortCommit}} - env: - - GO111MODULE=on - - CGO_ENABLED=0 - goos: - - linux - goarch: - - amd64 - mod_timestamp: "{{ .CommitTimestamp }}" - -release: - disable: true - -dockers: - - goos: linux - goarch: amd64 - dockerfile: docker/Dockerfile.nightly - skip_push: false - build_flag_templates: - - '--build-arg=VERSION={{ if ne .Branch "main" }}{{ replace .Branch "/" "-" }}{{ else }}nightly{{ end }}-{{ time "20060102" }}' - image_templates: - - synadia/nats-server:{{ if ne .Branch "main" }}{{ replace .Branch "/" "-" }}{{ else }}nightly{{ end }} - - synadia/nats-server:{{ if ne .Branch "main" }}{{ replace .Branch "/" "-" }}{{ else }}nightly{{ end }}-{{ time "20060102" }} - extra_files: - - docker/nats-server.conf - -checksum: - name_template: "SHA256SUMS" - algorithm: sha256 - -snapshot: - version_template: '{{ if ne .Branch "main" }}{{ replace .Branch "/" "-" }}{{ else }}nightly{{ end }}-{{ time "20060102" }}' diff --git a/docker/Dockerfile.nightly b/docker/Dockerfile.nightly index 3c9d22f4b50..17f8e4272e6 100644 --- a/docker/Dockerfile.nightly +++ b/docker/Dockerfile.nightly @@ -1,24 +1,38 @@ FROM golang:alpine AS builder ARG VERSION="nightly" +ARG GIT_COMMIT +ARG TARGETOS +ARG TARGETARCH -RUN apk add --update git -RUN mkdir -p src/github.com/nats-io && \ - cd src/github.com/nats-io/ && \ - git clone https://github.com/nats-io/natscli.git && \ - cd natscli/nats && \ - go build -ldflags "-w -X main.version=${VERSION}" -o /nats +ENV GOOS=$TARGETOS \ + GOARCH=$TARGETARCH \ + GO111MODULE=on \ + CGO_ENABLED=0 -RUN go install github.com/nats-io/nsc/v2@latest +RUN apk add --no-cache git ca-certificates +RUN mkdir -p /src/nats-server /src/natscli /src/nsc -FROM alpine:latest +COPY . /src/nats-server +RUN git clone --depth 1 https://github.com/nats-io/natscli /src/natscli +RUN git clone --depth 1 https://github.com/nats-io/nsc /src/nsc + +WORKDIR /src/nats-server +RUN go install -v -trimpath -ldflags "-w -X server.serverVersion=${VERSION},server.gitCommit=${GIT_COMMIT}" . + +WORKDIR /src/natscli +RUN go install -v -trimpath -ldflags "-w -X main.version=${VERSION}" ./nats -RUN apk add --update ca-certificates && mkdir -p /nats/bin && mkdir /nats/conf +WORKDIR /src/nsc +RUN go install -v -trimpath . + +FROM alpine:latest COPY docker/nats-server.conf /nats/conf/nats-server.conf -COPY nats-server /bin/nats-server -COPY --from=builder /nats /bin/nats +COPY --from=builder /go/bin/nats-server /bin/nats-server +COPY --from=builder /go/bin/nats /bin/nats COPY --from=builder /go/bin/nsc /bin/nsc +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ EXPOSE 4222 8222 6222 5222