Skip to content

Commit b3ce0ea

Browse files
authored
Merge pull request #10 from rancherlabs/v2-0-build-issues
V2 0 build issues
2 parents 422b62b + dfaf911 commit b3ce0ea

File tree

6 files changed

+172
-82
lines changed

6 files changed

+172
-82
lines changed

.github/workflows/build.yml

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,36 @@
1+
name: Build and Validate
2+
13
on:
24
push:
35
branches:
46
- master
57
- main
68
pull_request:
79

8-
name: Build
9-
jobs:
10-
build-amd64:
11-
runs-on: ubuntu-latest
12-
steps:
13-
- name: Checkout code
14-
uses: actions/checkout@v4
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.ref }}
12+
cancel-in-progress: true
1513

16-
- name: Set the TAG value
17-
id: get-TAG
18-
run: |
19-
echo "$(make -s log | grep TAG)" >> "$GITHUB_ENV"
20-
- name: Build container image
21-
uses: docker/build-push-action@v6
22-
with:
23-
context: .
24-
push: false
25-
tags: rancherlabs/swiss-army-knife:${{ env.TAG }}-amd64
26-
file: Dockerfile
27-
build-args: |
28-
TAG=${{ env.TAG }}
29-
30-
- name: Run Trivy vulnerability scanner
31-
uses: aquasecurity/[email protected]
32-
with:
33-
image-ref: rancherlabs/swiss-army-knife:${{ env.TAG }}-amd64
34-
format: 'table'
35-
exit-code: '1'
36-
ignore-unfixed: true
37-
vuln-type: 'os,library'
38-
severity: 'CRITICAL,HIGH'
39-
40-
build-arm64:
14+
jobs:
15+
build:
4116
runs-on: ubuntu-latest
4217
steps:
43-
- name: Check out code
44-
uses: actions/checkout@v4
18+
- name: Check out code
19+
uses: actions/checkout@v4
4520

46-
- name: Set up QEMU
47-
uses: docker/setup-qemu-action@v3
21+
- name: Set up QEMU
22+
uses: docker/setup-qemu-action@v3
4823

49-
- name: Set up Docker Buildx
50-
uses: docker/setup-buildx-action@v3
24+
- name: Build and validate multi-arch image
25+
run: make build-validate
5126

52-
- name: Set the TAG value
53-
id: get-TAG
54-
run: |
55-
echo "$(make -s log | grep TAG)" >> "$GITHUB_ENV"
56-
- name: Build container image
57-
uses: docker/build-push-action@v6
58-
with:
59-
context: .
60-
push: false
61-
tags: rancherlabs/swiss-army-knife:${{ env.TAG }}-arm64
62-
file: Dockerfile
63-
outputs: type=docker
64-
platforms: linux/arm64
65-
build-args: |
66-
TAG=${{ env.TAG }}
27+
- name: Display build info
28+
run: make log
6729

68-
- name: Run Trivy vulnerability scanner
69-
uses: aquasecurity/[email protected]
70-
with:
71-
image-ref: rancherlabs/swiss-army-knife:${{ env.TAG }}-arm64
72-
format: 'table'
73-
exit-code: '1'
74-
ignore-unfixed: true
75-
vuln-type: 'os,library'
76-
severity: 'CRITICAL,HIGH'
30+
- name: Upload CI files to artifacts (on failure)
31+
uses: actions/upload-artifact@v4
32+
if: failure()
33+
with:
34+
name: ci-artifacts
35+
path: 'ci'
36+
retention-days: 7

.github/workflows/release.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on:
44

55
env:
66
GITHUB_ACTION_TAG: ${{ github.ref_name }}
7+
PUBLIC_REGISTRY: docker.io
8+
REPO: rancherlabs
79

810
jobs:
911
push-multiarch:
@@ -25,15 +27,22 @@ jobs:
2527
secret/data/github/repo/${{ github.repository }}/rancher-prime-registry/credentials username | PRIME_REGISTRY_USERNAME ;
2628
secret/data/github/repo/${{ github.repository }}/rancher-prime-registry/credentials password | PRIME_REGISTRY_PASSWORD
2729
30+
- name: Set up QEMU
31+
uses: docker/setup-qemu-action@v3
32+
2833
- name: Build and push image
2934
uses: rancher/ecm-distro-tools/actions/publish-image@master
3035
with:
3136
image: swiss-army-knife
32-
tag: ${{ github.event.release.tag_name }},latest
33-
public-repo: rancherlabs
37+
tag: ${{ github.ref_name }}
38+
platforms: "linux/amd64,linux/arm64"
39+
40+
public-registry: ${{ env.PUBLIC_REGISTRY }}
41+
public-repo: ${{ env.REPO }}
3442
public-username: ${{ env.DOCKER_USERNAME }}
3543
public-password: ${{ env.DOCKER_PASSWORD }}
3644

45+
push-to-prime: true
3746
prime-repo: rancherlabs
3847
prime-registry: ${{ env.PRIME_REGISTRY }}
3948
prime-username: ${{ env.PRIME_REGISTRY_USERNAME }}

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -o ech
1313
# Final stage
1414
FROM registry.suse.com/bci/bci-base:15.7
1515

16+
# Use buildx automatic platform args
17+
ARG TARGETARCH
18+
1619
# Update all packages to latest versions to fix known vulnerabilities
1720
RUN zypper -n refresh && \
1821
zypper -n update -y && \
@@ -70,9 +73,9 @@ RUN zypper -n install --no-recommends mtr iperf3 \
7073
# Copy the compiled binary from builder stage
7174
COPY --from=builder /app/echo-server /usr/local/bin/
7275

73-
# Download the stable kubectl binary
76+
# Download the stable kubectl binary for the correct architecture
7477
RUN VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt) && \
75-
curl -L https://dl.k8s.io/release/$VERSION/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl && \
78+
curl -L https://dl.k8s.io/release/$VERSION/bin/linux/${TARGETARCH}/kubectl -o /usr/local/bin/kubectl && \
7679
chmod a+x /usr/local/bin/kubectl
7780

7881
# Set working directory

Makefile

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,65 @@
1-
# Get git commit hash
2-
GIT_COMMIT := $(shell git rev-parse --short HEAD)
3-
4-
# Try to get the tag, if it exists
5-
GIT_TAG := $(shell git describe --tags --exact-match HEAD 2>/dev/null)
6-
7-
# Set TAG based on whether a git tag exists
8-
ifdef GIT_TAG
9-
# We're on a tagged commit, use the tag
10-
TAG := $(GIT_TAG)
11-
else
12-
# Not on a tag, use build-{commit} format
13-
TAG := build-$(GIT_COMMIT)
14-
endif
1+
# Include logic that can be reused across projects.
2+
include hack/make/build.mk
3+
4+
# Define target platforms, image builder and the fully qualified image name.
5+
TARGET_PLATFORMS ?= linux/amd64,linux/arm64
6+
7+
REPO ?= rancherlabs
8+
IMAGE ?= swiss-army-knife
9+
IMAGE_NAME = $(REPO)/$(IMAGE)
10+
FULL_IMAGE_TAG = $(IMAGE_NAME):$(TAG)
11+
BUILD_ACTION = --load
1512

1613
# Default target
1714
.PHONY: all
1815
all: build
1916

20-
# Build target
17+
# Build target (for local Go binary)
2118
.PHONY: build
2219
build:
2320
go build -o echo-server main.go
2421

22+
build-image: buildx-machine ## build (and load) the container image targeting the current platform.
23+
$(IMAGE_BUILDER) build -f Dockerfile \
24+
--builder $(MACHINE) $(IMAGE_ARGS) \
25+
--build-arg VERSION=$(VERSION) --platform=$(TARGET_PLATFORMS) -t "$(FULL_IMAGE_TAG)" $(BUILD_ACTION) .
26+
@echo "Built $(FULL_IMAGE_TAG)"
27+
28+
build-validate: buildx-machine ## build (and load) the container image targeting the current platform.
29+
mkdir -p ci
30+
$(IMAGE_BUILDER) build -f Dockerfile \
31+
--builder $(MACHINE) $(IMAGE_ARGS) \
32+
--build-arg VERSION=$(VERSION) \
33+
--platform=$(TARGET_PLATFORMS) \
34+
--output type=oci,dest=ci/multiarch-image.oci \
35+
-t "$(FULL_IMAGE_TAG)" .
36+
@echo "Built $(FULL_IMAGE_TAG) multi-arch image saved to ci/multiarch-image.oci"
37+
38+
push-image: validate buildx-machine ## build the container image targeting all platforms defined by TARGET_PLATFORMS and push to a registry.
39+
$(IMAGE_BUILDER) build -f Dockerfile \
40+
--builder $(MACHINE) $(IMAGE_ARGS) $(IID_FILE_FLAG) $(BUILDX_ARGS) \
41+
--build-arg VERSION=$(VERSION) --platform=$(TARGET_PLATFORMS) -t "$(FULL_IMAGE_TAG)" --push .
42+
@echo "Pushed $(FULL_IMAGE_TAG)"
43+
44+
validate: validate-dirty ## Run validation checks.
45+
46+
validate-dirty:
47+
ifdef DIRTY
48+
@echo Git is dirty
49+
@git --no-pager status
50+
@git --no-pager diff
51+
@exit 1
52+
endif
53+
2554
# Log target - outputs variables for CI/CD
2655
.PHONY: log
2756
log:
2857
@echo "TAG=$(TAG)"
58+
@echo "VERSION=$(VERSION)"
2959
@echo "BUILD_DATE=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")"
30-
@echo "GIT_COMMIT=$(GIT_COMMIT)"
60+
@echo "GIT_COMMIT=$(shell git rev-parse --short HEAD)"
61+
62+
clean: ## clean up project.
63+
rm -rf build
64+
rm -rf ci
65+
rm -f echo-server

hack/make/build.mk

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
ifeq ($(VERSION),)
2+
VERSION := $(shell ./scripts/version --short VERSION)
3+
endif
4+
5+
ifeq ($(TAG),)
6+
TAG := $(shell ./scripts/version --short TAG)
7+
endif
8+
9+
RUNNER := docker
10+
IMAGE_BUILDER := $(RUNNER) buildx
11+
MACHINE := rancher
12+
13+
# Define the target platforms that can be used across the ecosystem.
14+
# Note that what would actually be used for a given project will be
15+
# defined in TARGET_PLATFORMS, and must be a subset of the below:
16+
DEFAULT_PLATFORMS := linux/amd64,linux/arm64
17+
18+
.PHONY: help
19+
help: ## display Makefile's help.
20+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
21+
22+
buildx-machine: ## create rancher dockerbuildx machine targeting platform defined by DEFAULT_PLATFORMS.
23+
@docker buildx ls | grep $(MACHINE) || \
24+
docker buildx create --name=$(MACHINE) --platform=$(DEFAULT_PLATFORMS)

scripts/version

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
3+
export KUBECONFIG=$KUBECONFIG
4+
5+
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
6+
DIRTY="-dirty"
7+
fi
8+
9+
COMMIT=$(git rev-parse --short HEAD)
10+
GIT_TAG=${GIT_TAG:-$(git tag -l --contains HEAD | tail -n 1)}
11+
12+
if [[ -z "$DIRTY" && -n "$GIT_TAG" ]]; then
13+
VERSION=$GIT_TAG
14+
else
15+
VERSION="${COMMIT}${DIRTY}"
16+
fi
17+
18+
ARCH=$TARGET_ARCH
19+
if [ -z "$ARCH" ]; then
20+
ARCH=$(go env GOHOSTARCH 2>/dev/null || echo "amd64")
21+
fi
22+
23+
SUFFIX="-${ARCH}"
24+
25+
TAG=${TAG:-${BRANCH_TAG:-${VERSION}}}
26+
REPO=${REPO:-rancherlabs}
27+
28+
HELM_IMAGE_TAG=${HELM_IMAGE_TAG:-${TAG}}
29+
if [ "$TAG" == "$COMMIT" ]; then
30+
HELM_CHART_VERSION="0.0.0-dev+${COMMIT}"
31+
else
32+
HELM_CHART_VERSION=${HELM_IMAGE_TAG/v/}
33+
fi
34+
35+
if echo "$TAG" | grep -q dirty; then
36+
TAG="v0.0.0-dev.1-${COMMIT}"
37+
HELM_IMAGE_TAG=$TAG
38+
HELM_CHART_VERSION=${HELM_CHART_VERSION_DEV:-${HELM_IMAGE_TAG/v/}}
39+
fi
40+
IMAGE=${IMAGE:-"swiss-army-knife"}
41+
IMAGE_NAME=${IMAGE_NAME:-"${REPO}/${IMAGE}"}
42+
FULL_IMAGE=${FULL_IMAGE:-"${IMAGE_NAME}:${TAG}"}
43+
44+
if [[ "$1" == "--short" && -n "$2" ]]; then
45+
var_name="$2"
46+
echo "${!var_name}"
47+
exit 0
48+
fi
49+
50+
function print_version_debug() {
51+
echo "DIRTY: $DIRTY"
52+
echo "SUFFIX: $SUFFIX";
53+
echo "HELM_IMAGE_TAG: $HELM_IMAGE_TAG";
54+
echo "HELM_CHART_VERSION: $HELM_CHART_VERSION";
55+
echo "REPO: $REPO; IMAGE: $IMAGE; TAG: $TAG";
56+
echo "IMAGE_NAME: $IMAGE_NAME"
57+
echo "FULL_IMAGE: $FULL_IMAGE";
58+
}
59+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then print_version_debug "$1"; fi

0 commit comments

Comments
 (0)