Skip to content

Commit

Permalink
Enable go builds and image pushes on release
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
  • Loading branch information
kke committed Sep 18, 2023
1 parent a754c64 commit 0f098aa
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 62 deletions.
111 changes: 111 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: Release

on:
push:
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
prepare:
runs-on: ubuntu-20.04
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
hash: ${{ steps.set-hash.outputs.hash }}
changed_files: ${{ steps.changed-files.outputs.all_changed_files }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Populate list of changed images
id: set-matrix-changed-images
run: |
changed_images=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -E 'images/.+/Dockerfile' | awk -F/ '{print $2}' | jq -R -c -s 'split("\n")[:-1]')
echo "matrix_changed_images={\"image\":$changed_images}" >> $GITHUB_OUTPUT
- name: Calculate a hash for images/
id: set-hash
run: echo "hash=${{ hashFiles('images/**') }}" >> $GITHUB_OUTPUT

publish_images:
needs: prepare
runs-on: ubuntu-20.04
if: needs.prepare.outputs.matrix_changed_images != 'null'
strategy:
matrix: ${{fromJson(needs.prepare.outputs.matrix_changed_images)}}
fail-fast: false

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Login to Quay
uses: docker/login-action@v2
with:
username: ${{ secrets.QUAY_USER }}
password: ${{ secrets.QUAY_PASSWORD }}
registry: quay.io

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Cache Buildx Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ needs.prepare.outputs.hash }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build ${{ matrix.image }}
uses: docker/build-push-action@v5
with:
platforms:
- linux/amd64
- linux/arm64
- linux/arm/v7
push: true
load: true
tags:
- ${{ matrix.image }}:latest
- quay.io/k0sproject/footloose-${{ matrix.image }}:latest
context: images/${{ matrix.image }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache

publish_binaries:
name: release
needs: prepare
runs-on: ubuntu-20.04
steps:
- name: Check out code
uses: actions/checkout@v4

# Ugly hack to get the tag name
# github.ref gives the full reference like refs.tags.v0.0.1-beta1
- name: Branch name
id: branch_name
run: echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: go.mod

- name: Build binaries
id: build_bins
env:
TAG_NAME: ${{ steps.branch_name.outputs.TAG_NAME }}
run: make build-all

- name: Create release and upload binaries
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
bin/footloose-*
bin/sha256sums.txt
body_path: bin/checksums.md
tag_name: ${{ steps.branch_name.outputs.TAG_NAME }}
name: ${{ steps.branch_name.outputs.TAG_NAME }}
draft: true # So we can manually edit before publishing
prerelease: ${{ contains(steps.branch_name.outputs.TAG_NAME, '-') }} # v0.1.2-beta1, 1.2.3-rc1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
cluster-key
cluster-key.pub
footloose
/bin
53 changes: 46 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
all: footloose
GO_SRCS := $(shell find . -type f -name '*.go' -a ! \( -name 'zz_generated*' -o -name '*_test.go' \))
GO_TESTS := $(shell find . -type f -name '*_test.go')
TAG_NAME = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
GIT_COMMIT = $(shell git rev-parse --short=7 HEAD)
ifdef TAG_NAME
ENVIRONMENT = production
endif
ENVIRONMENT ?= development
LD_FLAGS = -s -w -X github.com/k0sproject/footloose/version.Environment=$(ENVIRONMENT) -X github.com/carlmjohnson/versioninfo.Revision=$(GIT_COMMIT) -X github.com/carlmjohnson/versioninfo.Version=$(TAG_NAME)
BUILD_FLAGS = -trimpath -a -tags "netgo,osusergo,static_build" -installsuffix netgo -ldflags "$(LD_FLAGS) -extldflags '-static'"
PREFIX = /usr/local

BIN_PREFIX := footloose-

footloose: bin/footloose
all: footloose

bin/footloose:
footloose:
go build -v -o bin/footloose .
go build -v $(BUILD_FLAGS) -o bin/footloose .

PLATFORMS := linux-amd64 linux-arm64 linux-arm darwin-amd64 darwin-arm64
bins := $(foreach platform, $(PLATFORMS), bin/$(BIN_PREFIX)$(platform))

$(bins):
$(eval temp := $(subst -, ,$(subst $(BIN_PREFIX),,$(notdir $@))))
$(eval OS := $(word 1, $(subst -, ,$(temp))))
$(eval ARCH := $(word 2, $(subst -, ,$(temp))))
GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build $(BUILD_FLAGS) -o $@ .

bin/%: $(GO_SRCS)
GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=$(CGO_ENABLED) go build $(BUILD_FLAGS) -o $@ .

bin/sha256sums.txt: $(bins)
sha256sum -b $(bins) | sed 's|bin/||' > $@

# for use in release markdown
bin/sha256sums.md: bin/sha256sums.txt
@echo "### SHA256 Checksums" > $@
@echo >> $@
@echo "\`\`\`" >> $@
@cat $< >> $@
@echo "\`\`\`" >> $@

build-all: $(bins) bin/sha256sums.md

install:
go install -v .
install: footloose
install -d $(DESTDIR)$(PREFIX)/bin/
install -m 755 footloose $(DESTDIR)$(PREFIX)/bin/

# Build all images
images:
Expand Down Expand Up @@ -36,7 +75,7 @@ list-images:
# Clean up all stamps and other generated files
clean:
@$(MAKE) -C images clean
rm -f bin/footloose
rm -f footloose bin/

# Phony targets
.PHONY: images image-% test-unit test-e2e test-e2e-% list-images clean
.PHONY: install images image-% test-unit test-e2e test-e2e-% list-images clean build-all
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.21.0

require (
github.com/blang/semver v3.5.1+incompatible
github.com/carlmjohnson/versioninfo v0.22.5
github.com/docker/docker v24.0.6+incompatible
github.com/ghodss/yaml v1.0.0
github.com/google/go-github/v24 v24.0.1
github.com/gorilla/mux v1.8.0
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
Expand All @@ -22,8 +22,6 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
Expand Down
15 changes: 2 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/carlmjohnson/versioninfo v0.22.5 h1:O00sjOLUAFxYQjlN/bzYTuZiS0y6fWDQjMRvwtKgwwc=
github.com/carlmjohnson/versioninfo v0.22.5/go.mod h1:QT9mph3wcVfISUKd0i9sZfVrPviHuSF+cUtLjm2WSf8=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -14,16 +16,8 @@ github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-github/v24 v24.0.1 h1:KCt1LjMJEey1qvPXxa9SjaWxwTsCWSq6p2Ju57UR4Q4=
github.com/google/go-github/v24 v24.0.1/go.mod h1:CRqaW1Uns1TCkP0wqTpxYyRxRjxwvKU/XSS44u6X74M=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down Expand Up @@ -53,22 +47,18 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180824143301-4910a1d54f87/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -87,7 +77,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
23 changes: 0 additions & 23 deletions pkg/version/release.go

This file was deleted.

26 changes: 10 additions & 16 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"fmt"
"strings"

release "github.com/k0sproject/footloose/pkg/version"
_ "github.com/carlmjohnson/versioninfo" // Needed to set version info during go install
"github.com/k0sproject/footloose/version"

"github.com/spf13/cobra"
)
Expand All @@ -16,22 +16,16 @@ var versionCmd = &cobra.Command{
}

func init() {
versionCmd.Flags().BoolP("long", "l", false, "Print long version")
footloose.AddCommand(versionCmd)
}

var version = "git"

func showVersion(cmd *cobra.Command, args []string) {
fmt.Println("version:", version)
if version == "git" {
return
}
release, err := release.FindLastRelease()
if err != nil {
fmt.Println("version: failed to check for new versions. You may want to check yourself at https://github.com/k0sproject/footloose/releases.")
func showVersion(cmd *cobra.Command, _ []string) {
if long, err := cmd.Flags().GetBool("long"); err == nil && long {
fmt.Println("version:", version.Version)
fmt.Printf("commit: %s\n", version.GitCommit)
fmt.Printf("environment: %s\n", version.Environment)
return
}
if strings.Compare(version, *release.TagName) != 0 {
fmt.Printf("New version %v is available. More information at: %v\n", *release.TagName, *release.HTMLURL)
}
}
fmt.Println(version.Version)
}
14 changes: 14 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package version

import (
"github.com/carlmjohnson/versioninfo"
)

var (
// Version of the product, is set during the build
Version = versioninfo.Version
// GitCommit is set during the build
GitCommit = versioninfo.Revision
// Environment of the product, is set during the build
Environment = "development"
)

0 comments on commit 0f098aa

Please sign in to comment.