Skip to content

Commit 6f2d762

Browse files
authored
(ci): automated releasing with goreleaser (#5)
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
1 parent 1d7b885 commit 6f2d762

File tree

13 files changed

+392
-258
lines changed

13 files changed

+392
-258
lines changed

.github/workflows/release.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: release
2+
3+
on:
4+
workflow_dispatch:
5+
merge_group:
6+
push:
7+
branches:
8+
- 'main'
9+
tags:
10+
- 'v*'
11+
pull_request:
12+
branches:
13+
- main
14+
15+
jobs:
16+
goreleaser:
17+
name: goreleaser
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Check out code into the Go module directory
21+
uses: actions/checkout@v3
22+
with:
23+
fetch-depth: 0
24+
25+
- name: Install Go
26+
uses: actions/setup-go@v4
27+
with:
28+
go-version-file: "go.mod"
29+
30+
- name: Docker Login
31+
if: ${{ github.event_name != 'pull_request' }}
32+
uses: docker/login-action@v2
33+
with:
34+
registry: quay.io
35+
username: ${{ secrets.QUAY_USERNAME }}
36+
password: ${{ secrets.QUAY_PASSWORD }}
37+
38+
- name: Set up QEMU
39+
uses: docker/setup-qemu-action@v2
40+
41+
- name: Set the release related variables
42+
run: |
43+
if [[ "${GITHUB_REF}" == "refs/tags/*" ]]; then
44+
# Release tags.
45+
echo IMAGE_TAG="${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
46+
echo GORELEASER_ARGS="--clean --timeout=60m" >> $GITHUB_ENV
47+
echo ENABLE_RELEASE_PIPELINE=true >> $GITHUB_ENV
48+
elif [[ "${GITHUB_REF}" == "refs/heads/*" ]]; then
49+
# Branch build.
50+
echo IMAGE_TAG="$(echo "${GITHUB_REF#refs/heads/}" | sed -r 's|/+|-|g')" >> $GITHUB_ENV
51+
echo GORELEASER_ARGS="--clean --skip-validate --timeout=60m" >> $GITHUB_ENV
52+
elif [[ "${GITHUB_REF}" == "refs/pull/*" ]]; then
53+
# PR build.
54+
echo IMAGE_TAG="pr-$(echo "${GITHUB_REF}" | sed -E 's|refs/pull/([^/]+)/?.*|\1|')" >> $GITHUB_ENV
55+
else
56+
echo IMAGE_TAG="$(git describe --tags --always)" >> $GITHUB_ENV
57+
fi
58+
59+
- name: Run goreleaser
60+
run: make release
61+
env:
62+
GITHUB_TOKEN: ${{ github.token }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/build
66
/dist
77
**/bin/
8+
ansible-operator
89

910
# Test artifacts
1011
**/testbin/

.goreleaser.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
before:
2+
hooks:
3+
- go mod tidy
4+
- go mod download
5+
builds:
6+
- id: binary
7+
main: ./cmd/ansible-operator/
8+
binary: ansible-operator
9+
asmflags: "{{ .Env.GO_BUILD_ASMFLAGS }}"
10+
gcflags: "{{ .Env.GO_BUILD_GCFLAGS }}"
11+
ldflags: "{{ .Env.GO_BUILD_LDFLAGS }}"
12+
mod_timestamp: "{{ .CommitTimestamp }}"
13+
goos:
14+
- linux
15+
goarch:
16+
- amd64
17+
- arm64
18+
- ppc64le
19+
- s390x
20+
dockers:
21+
- image_templates:
22+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-amd64"
23+
dockerfile: images/ansible-operator/Dockerfile
24+
goos: linux
25+
goarch: amd64
26+
use: buildx
27+
build_flag_templates:
28+
- "--platform=linux/amd64"
29+
extra_files:
30+
- "images/ansible-operator/Pipfile"
31+
- "images/ansible-operator/Pipfile.lock"
32+
- image_templates:
33+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-arm64"
34+
dockerfile: images/ansible-operator/Dockerfile
35+
goos: linux
36+
goarch: arm64
37+
use: buildx
38+
build_flag_templates:
39+
- "--platform=linux/arm64"
40+
extra_files:
41+
- "images/ansible-operator/Pipfile"
42+
- "images/ansible-operator/Pipfile.lock"
43+
- image_templates:
44+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-ppc64le"
45+
dockerfile: images/ansible-operator/Dockerfile
46+
goos: linux
47+
goarch: ppc64le
48+
use: buildx
49+
build_flag_templates:
50+
- "--platform=linux/ppc64le"
51+
extra_files:
52+
- "images/ansible-operator/Pipfile"
53+
- "images/ansible-operator/Pipfile.lock"
54+
- image_templates:
55+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-s390x"
56+
dockerfile: images/ansible-operator/Dockerfile
57+
goos: linux
58+
goarch: s390x
59+
use: buildx
60+
build_flag_templates:
61+
- "--platform=linux/s390x"
62+
extra_files:
63+
- "images/ansible-operator/Pipfile"
64+
- "images/ansible-operator/Pipfile.lock"
65+
docker_manifests:
66+
- name_template: "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}"
67+
image_templates:
68+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-amd64"
69+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-arm64"
70+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-ppc64le"
71+
- "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-s390x"
72+
release:
73+
disable: '{{ ne .Env.ENABLE_RELEASE_PIPELINE "true" }}'

Makefile

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,38 @@ SHELL = /bin/bash
44
# This value must be updated to the release tag of the most recent release, a change that must
55
# occur in the release commit. IMAGE_VERSION will be removed once each subproject that uses this
66
# version is moved to a separate repo and release process.
7-
export IMAGE_VERSION = v1.31.0
7+
export IMAGE_VERSION = v0.0.0
88
# Build-time variables to inject into binaries
9-
# export SIMPLE_VERSION = $(shell (test "$(shell git describe --tags)" = "$(shell git describe --tags --abbrev=0)" && echo $(shell git describe --tags)) || echo $(shell git describe --tags --abbrev=0)+git)
10-
# export GIT_VERSION = $(shell git describe --dirty --tags --always)
11-
# export GIT_COMMIT = $(shell git rev-parse HEAD)
9+
export SIMPLE_VERSION = $(shell (test "$(shell git describe --tags)" = "$(shell git describe --tags --abbrev=0)" && echo $(shell git describe --tags)) || echo $(shell git describe --tags --abbrev=0)+git)
10+
export GIT_VERSION = $(shell git describe --dirty --tags --always)
11+
export GIT_COMMIT = $(shell git rev-parse HEAD)
1212
export K8S_VERSION = 1.26.0
1313

1414
# Build settings
1515
export TOOLS_DIR = tools/bin
1616
export SCRIPTS_DIR = tools/scripts
1717
REPO = $(shell go list -m)
18-
BUILD_DIR = build
19-
GO_ASMFLAGS = -asmflags "all=-trimpath=$(shell dirname $(PWD))"
20-
GO_GCFLAGS = -gcflags "all=-trimpath=$(shell dirname $(PWD))"
21-
GO_BUILD_ARGS = \
22-
$(GO_GCFLAGS) $(GO_ASMFLAGS) \
23-
-ldflags " \
18+
BUILD_DIR = .
19+
export GO_BUILD_ASMFLAGS = all=-trimpath=$(shell dirname $(PWD))
20+
export GO_BUILD_GCFLAGS = all=-trimpath=$(shell dirname $(PWD))
21+
export GO_BUILD_LDFLAGS = \
2422
-X '$(REPO)/internal/version.Version=$(SIMPLE_VERSION)' \
2523
-X '$(REPO)/internal/version.GitVersion=$(GIT_VERSION)' \
2624
-X '$(REPO)/internal/version.GitCommit=$(GIT_COMMIT)' \
2725
-X '$(REPO)/internal/version.KubernetesVersion=v$(K8S_VERSION)' \
2826
-X '$(REPO)/internal/version.ImageVersion=$(IMAGE_VERSION)' \
29-
" \
27+
\
28+
29+
GO_BUILD_ARGS = \
30+
-gcflags "$(GO_BUILD_GCFLAGS)" -asmflags "$(GO_BUILD_ASMFLAGS)" -ldflags "$(GO_BUILD_LDFLAGS)"
3031

3132
export GO111MODULE = on
3233
export CGO_ENABLED = 0
3334
export PATH := $(PWD)/$(BUILD_DIR):$(PWD)/$(TOOLS_DIR):$(PATH)
3435

36+
export IMAGE_REPO = quay.io/operator-framework/ansible-operator-plugins
37+
export IMAGE_TAG = dev
38+
3539
##@ Development
3640

3741
.PHONY: generate
@@ -79,12 +83,7 @@ build/ansible-operator:
7983
# Convenience wrapper for building all remotely hosted images.
8084
.PHONY: image-build
8185
IMAGE_TARGET_LIST = ansible-operator
82-
image-build: $(foreach i,$(IMAGE_TARGET_LIST),image/$(i)) ## Build all images.
83-
84-
# Convenience wrapper for building dependency base images.
85-
.PHONY: image-build-base
86-
IMAGE_BASE_TARGET_LIST = ansible-operator
87-
image-build-base: $(foreach i,$(IMAGE_BASE_TARGET_LIST),image-base/$(i)) ## Build all images.
86+
image-build: build $(foreach i,$(IMAGE_TARGET_LIST),image/$(i)) ## Build all images.
8887

8988
# Build an image.
9089
BUILD_IMAGE_REPO = quay.io/operator-framework
@@ -94,11 +93,7 @@ DOCKER_PROGRESS = --progress plain
9493
endif
9594
image/%: export DOCKER_CLI_EXPERIMENTAL = enabled
9695
image/%:
97-
docker buildx build $(DOCKER_PROGRESS) -t $(BUILD_IMAGE_REPO)/$*:dev -f ./images/$*/Dockerfile --load .
98-
99-
image-base/%: export DOCKER_CLI_EXPERIMENTAL = enabled
100-
image-base/%:
101-
docker buildx build $(DOCKER_PROGRESS) -t $(BUILD_IMAGE_REPO)/$*-base:dev -f ./images/$*/base.Dockerfile --load images/$*
96+
docker buildx build $(DOCKER_PROGRESS) -t $(BUILD_IMAGE_REPO)/$*-plugins:dev -f ./images/$*/Dockerfile --load . --no-cache
10297
##@ Release
10398

10499
## TODO: Add release targets here
@@ -169,6 +164,22 @@ test-e2e-ansible-molecule:: install dev-install image/ansible-operator ## Run mo
169164
go run ./hack/generate/samples/molecule/generate.go
170165
./hack/tests/e2e-ansible-molecule.sh
171166

167+
## Location to install dependencies to
168+
LOCALBIN ?= $(shell pwd)/bin
169+
$(LOCALBIN):
170+
mkdir -p $(LOCALBIN)
171+
172+
GORELEASER := $(abspath $(LOCALBIN)/goreleaser)
173+
GORELEASER_VERSION ?= v1.16.2
174+
goreleaser: $(LOCALBIN) ## Build a local copy of goreleaser
175+
GOBIN=$(LOCALBIN) go install github.com/goreleaser/goreleaser@$(GORELEASER_VERSION)
176+
177+
export ENABLE_RELEASE_PIPELINE ?= false
178+
export GORELEASER_ARGS ?= --snapshot --clean --timeout=60m
179+
release: IMAGE_TAG = $(GIT_VERSION)
180+
release: goreleaser ## Runs goreleaser. By default, this will run only as a snapshot and will not publish any artifacts unless it is run with different arguments. To override the arguments, run with "GORELEASER_ARGS=...". When run as a github action from a tag, this target will publish a full release.
181+
$(GORELEASER) $(GORELEASER_ARGS)
182+
172183
.DEFAULT_GOAL := help
173184
.PHONY: help
174185
help: ## Show this help screen.

hack/generate/samples/ansible/advanced_molecule.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ func updateDockerfile(dir string) {
201201
log.Info("replacing project Dockerfile to use ansible base image with the dev tag")
202202
err := kbutil.ReplaceRegexInFile(
203203
filepath.Join(dir, "Dockerfile"),
204-
"quay.io/operator-framework/ansible-operator:.*",
205-
"quay.io/operator-framework/ansible-operator:dev")
204+
"quay.io/operator-framework/ansible-operator-plugins:.*",
205+
"quay.io/operator-framework/ansible-operator-plugins:dev")
206206
pkg.CheckError("replacing Dockerfile", err)
207207

208208
log.Info("inserting code to Dockerfile")

hack/generate/samples/ansible/memcached_molecule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func ImplementMemcachedMolecule(sample sample.Sample, image string) {
7777
}
7878

7979
log.Info("replacing project Dockerfile to use ansible base image with the dev tag")
80-
err := kbutil.ReplaceRegexInFile(filepath.Join(sample.Dir(), "Dockerfile"), "quay.io/operator-framework/ansible-operator:.*", "quay.io/operator-framework/ansible-operator:dev")
80+
err := kbutil.ReplaceRegexInFile(filepath.Join(sample.Dir(), "Dockerfile"), "quay.io/operator-framework/ansible-operator-plugins:.*", "quay.io/operator-framework/ansible-operator-plugins:dev")
8181
pkg.CheckError("replacing Dockerfile", err)
8282

8383
log.Info("adding RBAC permissions")

images/ansible-operator/Dockerfile

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,66 @@
1-
# Build the manager binary
2-
FROM --platform=$BUILDPLATFORM golang:1.19 as builder
1+
# This Dockerfile defines the base image for the ansible-operator image.
2+
# It is built with dependencies that take a while to download, thus speeding
3+
# up ansible deploy jobs.
4+
5+
FROM registry.access.redhat.com/ubi8/ubi:8.8-1032 AS basebuilder
6+
7+
# Install Rust so that we can ensure backwards compatibility with installing/building the cryptography wheel across all platforms
8+
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
9+
ENV PATH="/root/.cargo/bin:${PATH}"
10+
RUN rustc --version
11+
12+
# Copy python dependencies (including ansible) to be installed using Pipenv
13+
COPY images/ansible-operator/Pipfile* ./
14+
# Instruct pip(env) not to keep a cache of installed packages,
15+
# to install into the global site-packages and
16+
# to clear the pipenv cache as well
17+
ENV PIP_NO_CACHE_DIR=1 \
18+
PIPENV_SYSTEM=1 \
19+
PIPENV_CLEAR=1
20+
# Ensure fresh metadata rather than cached metadata, install system and pip python deps,
21+
# and remove those not needed at runtime.
22+
RUN set -e && yum clean all && rm -rf /var/cache/yum/* \
23+
&& yum update -y \
24+
&& yum install -y libffi-devel openssl-devel python39-devel gcc python39-pip python39-setuptools \
25+
&& pip3 install --upgrade pip~=23.1.2 \
26+
&& pip3 install pipenv==2023.6.26 \
27+
&& pipenv install --deploy \
28+
&& pipenv check \
29+
&& yum remove -y gcc libffi-devel openssl-devel python39-devel \
30+
&& yum clean all \
31+
&& rm -rf /var/cache/yum
32+
33+
FROM registry.access.redhat.com/ubi8/ubi:8.8-1032 as base
334
ARG TARGETARCH
435

5-
WORKDIR /workspace
6-
# Copy the Go Modules manifests
7-
COPY go.mod go.mod
8-
COPY go.sum go.sum
9-
# cache deps before building and copying source so that we don't need to re-download as much
10-
# and so that source changes don't invalidate our downloaded layer
11-
RUN go mod download
36+
# Label this image with the repo and commit that built it, for freshmaking purposes.
37+
ARG GIT_COMMIT=devel
38+
LABEL git_commit=$GIT_COMMIT
39+
40+
RUN mkdir -p /etc/ansible \
41+
&& echo "localhost ansible_connection=local" > /etc/ansible/hosts \
42+
&& echo '[defaults]' > /etc/ansible/ansible.cfg \
43+
&& echo 'roles_path = /opt/ansible/roles' >> /etc/ansible/ansible.cfg \
44+
&& echo 'library = /usr/share/ansible/openshift' >> /etc/ansible/ansible.cfg
45+
46+
RUN set -e && yum clean all && rm -rf /var/cache/yum/* \
47+
&& yum update -y \
48+
&& yum install -y python39-pip python39-setuptools \
49+
&& pip3 install --upgrade pip~=23.1.2 \
50+
&& pip3 install pipenv==2023.6.26 \
51+
&& yum clean all \
52+
&& rm -rf /var/cache/yum
1253

13-
# Copy the go source
14-
COPY . .
54+
COPY --from=basebuilder /usr/local/lib64/python3.9/site-packages /usr/local/lib64/python3.9/site-packages
55+
COPY --from=basebuilder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
56+
COPY --from=basebuilder /usr/local/bin /usr/local/bin
1557

16-
# Build
17-
RUN GOOS=linux GOARCH=$TARGETARCH make build/ansible-operator
58+
ENV TINI_VERSION=v0.19.0
59+
RUN curl -L -o /tini https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-${TARGETARCH} \
60+
&& chmod +x /tini && /tini --version
1861

1962
# Final image.
20-
FROM quay.io/operator-framework/ansible-operator-base:master-50c6ac03746ff4edf582feb9a71d2a7ea6ae6c40
63+
FROM base
2164

2265
ENV HOME=/opt/ansible \
2366
USER_NAME=ansible \
@@ -32,6 +75,6 @@ RUN echo "${USER_NAME}:x:${USER_UID}:0:${USER_NAME} user:${HOME}:/sbin/nologin"
3275
WORKDIR ${HOME}
3376
USER ${USER_UID}
3477

35-
COPY --from=builder /workspace/build/ansible-operator /usr/local/bin/ansible-operator
78+
COPY ansible-operator /usr/local/bin/ansible-operator
3679

3780
ENTRYPOINT ["/tini", "--", "/usr/local/bin/ansible-operator", "run", "--watches-file=./watches.yaml"]

0 commit comments

Comments
 (0)