Skip to content

Commit 341a9b3

Browse files
feat: [CI-21185]: Add Dockerfile and versioning support for plugin binary (#47)
Add support for building plugin Docker images via Harness CI pipeline, following the same pattern as lite-engine (CI-21182). Changes: - Added version/version.go with GetVersion() pattern * Returns "dev" for local builds * Can be set via ldflags during Docker build - Updated main.go to support -v and --version flags - Created docker/Dockerfile-plugin for building binary images * Multi-stage build using GAR golang:1.25-alpine3.21 * Scratch-based final image with binary at /binaries/plugin * Supports cross-platform builds (linux/darwin/windows, amd64/arm64) * Version injected via BUILD_VERSION arg - Added config/manifest.yaml for Harness CI pipeline integration This enables building harness/harness-vm-runner-plugin images that will be bundled into harness-vm-runner-binaries for VM injection. Tested: - plugin -v returns "dev" without ldflags - plugin -v returns "3.9.5-beta" with ldflags
1 parent 554f7b1 commit 341a9b3

4 files changed

Lines changed: 103 additions & 3 deletions

File tree

config/manifest.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DOCKER_FILE_PATH: /harness/plugin/docker/Dockerfile-plugin
2+
CONTEXT_PATH: /harness/plugin

docker/Dockerfile-plugin

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# ---------------------------------------------------------#
2+
# Build Plugin Binary #
3+
# ---------------------------------------------------------#
4+
FROM us-west1-docker.pkg.dev/gar-setup/docker/golang:1.25-alpine3.21 as builder
5+
6+
RUN apk add --no-cache git ca-certificates
7+
8+
WORKDIR /app
9+
10+
# Copy go module files first for better caching
11+
COPY go.mod go.sum ./
12+
RUN go mod download
13+
14+
# Copy source code
15+
COPY . .
16+
17+
# Build arguments for version information and target platform
18+
ARG BUILD_VERSION
19+
ARG BUILD_TIME
20+
ARG COMMIT_SHA
21+
ARG BRANCH_NAME
22+
ARG TARGETOS=linux
23+
ARG TARGETARCH
24+
25+
# Build the application for target platform
26+
# CGO is disabled for cross-compilation support
27+
# Version is set via BUILD_VERSION arg from Harness CI pipeline
28+
RUN --mount=type=cache,target=/root/.cache/go-build \
29+
--mount=type=cache,target=/go/pkg \
30+
set -e; \
31+
if [ "${TARGETOS}" = "windows" ]; then \
32+
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build \
33+
-buildvcs=false \
34+
-ldflags "-s -w -X github.com/drone/plugin/version.Version=${BUILD_VERSION}" \
35+
-o /app/plugin.exe; \
36+
else \
37+
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build \
38+
-ldflags "-s -w -X github.com/drone/plugin/version.Version=${BUILD_VERSION}" \
39+
-o /app/plugin; \
40+
fi
41+
42+
# ---------------------------------------------------------#
43+
# Create final image #
44+
# ---------------------------------------------------------#
45+
FROM scratch
46+
47+
WORKDIR /binaries
48+
49+
# Copy the binary (handle both Linux/macOS and Windows)
50+
ARG TARGETOS
51+
COPY --from=builder /app/plugin* /binaries/
52+
53+
# Build metadata labels
54+
ARG BUILD_VERSION
55+
ARG BUILD_TIME
56+
ARG COMMIT_SHA
57+
ARG BRANCH_NAME
58+
ARG TARGETARCH
59+
ARG TARGETOS
60+
61+
LABEL org.opencontainers.image.title="Harness Plugin"
62+
LABEL org.opencontainers.image.description="Harness CI plugin executor binary"
63+
LABEL org.opencontainers.image.vendor="Harness Inc."
64+
LABEL org.opencontainers.image.created="${BUILD_TIME}"
65+
LABEL org.opencontainers.image.revision="${COMMIT_SHA}"
66+
LABEL org.opencontainers.image.source="https://github.com/drone/plugin"
67+
LABEL org.opencontainers.image.version="${BUILD_VERSION}"
68+
LABEL BUILD_VERSION="${BUILD_VERSION}"
69+
LABEL BUILD_TIME="${BUILD_TIME}"
70+
LABEL BRANCH_NAME="${BRANCH_NAME}"
71+
LABEL COMMIT_SHA="${COMMIT_SHA}"
72+
LABEL ARCHITECTURE="${TARGETARCH}"
73+
LABEL OS="${TARGETOS}"

main.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/drone/plugin/plugin/github"
1818
"github.com/drone/plugin/plugin/harness"
1919
"github.com/drone/plugin/utils"
20+
"github.com/drone/plugin/version"
2021
)
2122

2223
var (
@@ -28,12 +29,20 @@ var (
2829
downloadOnly bool // plugin won't be executed on setting this flag. Only source will be downloaded. Used for caching the plugin dependencies
2930
disableClone bool // plugin does not clone when this flag is enabled
3031
binarySources utils.CustomStringSliceFlag // plugin uses these binary source urls in the same order to download the binaires
32+
showVersion bool // show version and exit
3133
)
3234

3335
func main() {
34-
if len(os.Args) > 1 && os.Args[1] != "" && os.Args[1] == "healthz" {
35-
fmt.Println("OK")
36-
return
36+
// Handle special commands before flag parsing
37+
if len(os.Args) > 1 && os.Args[1] != "" {
38+
if os.Args[1] == "healthz" {
39+
fmt.Println("OK")
40+
return
41+
}
42+
if os.Args[1] == "-v" || os.Args[1] == "--version" {
43+
fmt.Println(version.GetVersion())
44+
return
45+
}
3746
}
3847
ctx := context.Background()
3948

version/version.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2022 Harness Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package version
6+
7+
// Version holds the build version, set via ldflags during build
8+
var Version string
9+
10+
// GetVersion returns the current build version
11+
func GetVersion() string {
12+
if Version == "" {
13+
return "dev"
14+
}
15+
return Version
16+
}

0 commit comments

Comments
 (0)