Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(version): correctly set falcoctl version at build time #237

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions .github/workflows/docker-image.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
name: docker-image

on:
push:
branches: [main]
workflow_call:

inputs:
release:
required: true
type: string
commit:
required: true
type: string
build_date:
required: true
type: string
jobs:
docker-image:
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -47,3 +54,7 @@ jobs:
push: true
tags: ${{ steps.meta_falcoctl.outputs.tags }}
file: ./build/Dockerfile
build-args: |
RELEASE=${{ inputs.release }}
COMMIT=${{ inputs.commit }}
BUILD_DATE=${{ inputs.build_date }}
25 changes: 25 additions & 0 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,31 @@ jobs:
path: ./falcoctl-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz
retention-days: 1

docker-configure:
if: ${{ github.event_name == 'push' }}
runs-on: ubuntu-22.04
outputs:
release: ${{ steps.vars.outputs.release }}
commit: ${{ steps.vars.outputs.commit }}
build_date: ${{ steps.vars.outputs.build_date }}
steps:
- name: Set version fields
id: vars
run: |
echo "release=${{ github.sha }}" >> $GITHUB_OUTPUT
echo "commit=${{ github.sha }}" >> $GITHUB_OUTPUT
echo "build_date=$(date -u '+%Y-%m-%d_%H:%M:%S')" >> $GITHUB_OUTPUT

docker-image:
if: ${{ github.event_name == 'push' }}
needs: docker-configure
uses: ./.github/workflows/docker-image.yaml
secrets: inherit
with:
release: ${{ needs.docker-configure.outputs.release }}
commit: ${{ needs.docker-configure.outputs.commit }}
build_date: ${{ needs.docker-configure.outputs.build_date }}

test:
needs: build
runs-on: ubuntu-latest
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ jobs:
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

docker-configure:
runs-on: ubuntu-22.04
outputs:
release: ${{ steps.vars.outputs.release }}
commit: ${{ steps.vars.outputs.commit }}
build_date: ${{ steps.vars.outputs.build_date }}
steps:
- name: Set version fields
id: vars
run: |
echo "release=$(echo $GITHUB_REF | cut -d / -f 3 | sed 's/^v//')" >> $GITHUB_OUTPUT
echo "commit=${{ github.sha }}" >> $GITHUB_OUTPUT
echo "build_date=$(date -u '+%Y-%m-%d_%H:%M:%S')" >> $GITHUB_OUTPUT

docker-image:
needs: docker-configure
uses: ./.github/workflows/docker-image.yaml
secrets: inherit
with:
release: ${{ needs.docker-configure.outputs.release }}
commit: ${{ needs.docker-configure.outputs.commit }}
build_date: ${{ needs.docker-configure.outputs.build_date }}
6 changes: 3 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ builds:
goarch: 386

ldflags: |
-X github.com/falcosecurity/falcoctl/pkg/version.buildDate={{ .Date }}
-X github.com/falcosecurity/falcoctl/pkg/version.gitCommit={{ .Commit }}
-X github.com/falcosecurity/falcoctl/pkg/version.semVersion={{ if .IsSnapshot }}{{ .Commit }}{{ else }}{{ .Version }}{{ end }}
-X github.com/falcosecurity/falcoctl/cmd/version.buildDate={{ .Date }}
-X github.com/falcosecurity/falcoctl/cmd/version.gitCommit={{ .Commit }}
-X github.com/falcosecurity/falcoctl/cmd/version.semVersion={{ if .IsSnapshot }}{{ .Commit }}{{ else }}{{ .Version }}{{ end }}
-s
-w
main: .
Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ GO ?= go
DOCKER ?= docker

# version settings
RELEASE?=$(shell git rev-parse --short HEAD)
COMMIT?=$(shell git rev-parse --short HEAD)
RELEASE?=$(shell git rev-parse HEAD)
COMMIT?=$(shell git rev-parse HEAD)
BUILD_DATE?=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
PROJECT?=github.com/falcosecurity/falcoctl

Expand All @@ -21,9 +21,9 @@ TEST_FLAGS ?= -v -cover# -race
.PHONY: falcoctl
falcoctl:
$(GO) build -ldflags \
"-X ${PROJECT}/pkg/version.semVersion=${RELEASE} \
-X ${PROJECT}/pkg/version.gitCommit=${COMMIT} \
-X ${PROJECT}/pkg/version.buildDate=${BUILD_DATE}" \
"-X '${PROJECT}/cmd/version.semVersion=${RELEASE}' \
-X '${PROJECT}/cmd/version.gitCommit=${COMMIT}' \
-X '${PROJECT}/cmd/version.buildDate=${BUILD_DATE}'" \
-o falcoctl .

.PHONY: test
Expand Down Expand Up @@ -71,4 +71,4 @@ lint: golangci-lint
$(GOLANGCILINT) run --new-from-rev main

docker:
$(DOCKER) build -f ./build/Dockerfile .
$(DOCKER) build -f ./build/Dockerfile . --build-arg RELEASE=${RELEASE} --build-arg COMMIT=${COMMIT} --build-arg BUILD_DATE=${BUILD_DATE}
20 changes: 19 additions & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
FROM golang:1.19 as builder
WORKDIR /tmp/builder

ARG RELEASE
ARG COMMIT
ARG BUILD_DATE
ARG PROJECT=github.com/falcosecurity/falcoctl

RUN test -n "$RELEASE" || ( echo "The RELEASE argument is unset. Aborting" && false )
RUN test -n "$COMMIT" || ( echo "The COMMIT argument is unset. Aborting" && false )
RUN test -n "$BUILD_DATE" || ( echo "The BUILD_DATE argument is unset. Aborting" && false )

COPY go.mod ./go.mod
COPY go.sum ./go.sum
RUN go mod download

COPY . ./
RUN CGO_ENABLED=0 GOOS=$(go env GOOS) GOARCH=$(go env GOARCH) go build -ldflags="-s -w" ./

RUN CGO_ENABLED=0 \
GOOS=$(go env GOOS) \
GOARCH=$(go env GOARCH) \
go build -ldflags \
"-s \
-w \
-X '${PROJECT}/cmd/version.semVersion=${RELEASE}' \
-X '${PROJECT}/cmd/version.gitCommit=${COMMIT}' \
-X '${PROJECT}/cmd/version.buildDate=${BUILD_DATE}'" \
./

RUN echo ${RELEASE}

FROM alpine:3.16.3

Expand Down