From 0f098aab2a0149ceae1316e688d4e94b9aa66ddd Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Mon, 18 Sep 2023 15:30:33 +0300 Subject: [PATCH] Enable go builds and image pushes on release Signed-off-by: Kimmo Lehto --- .github/workflows/release.yaml | 111 +++++++++++++++++++++++++++++++++ .gitignore | 1 + Makefile | 53 +++++++++++++--- go.mod | 4 +- go.sum | 15 +---- pkg/version/release.go | 23 ------- version.go | 26 +++----- version/version.go | 14 +++++ 8 files changed, 185 insertions(+), 62 deletions(-) create mode 100644 .github/workflows/release.yaml delete mode 100644 pkg/version/release.go create mode 100644 version/version.go diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..3a91711 --- /dev/null +++ b/.github/workflows/release.yaml @@ -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 diff --git a/.gitignore b/.gitignore index 018bbdd..7b873fd 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ cluster-key cluster-key.pub footloose +/bin diff --git a/Makefile b/Makefile index e3ceecf..538e194 100644 --- a/Makefile +++ b/Makefile @@ -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: @@ -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 diff --git a/go.mod b/go.mod index 5f7c87a..52d0ea9 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 509770a..bd5c7d7 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/pkg/version/release.go b/pkg/version/release.go deleted file mode 100644 index d17b432..0000000 --- a/pkg/version/release.go +++ /dev/null @@ -1,23 +0,0 @@ -package release - -import ( - "context" - "fmt" - - "github.com/google/go-github/v24/github" -) - -const ( - owner = "weaveworks" - repo = "footloose" -) - -// FindLastRelease searches latest release of the project -func FindLastRelease() (*github.RepositoryRelease, error) { - githubclient := github.NewClient(nil) - repoRelease, _, err := githubclient.Repositories.GetLatestRelease(context.Background(), owner, repo) - if err != nil { - return nil, fmt.Errorf("Failed to get latest release information") - } - return repoRelease, nil -} diff --git a/version.go b/version.go index 7bfdf9c..97aa321 100644 --- a/version.go +++ b/version.go @@ -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" ) @@ -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) - } -} \ No newline at end of file + fmt.Println(version.Version) +} diff --git a/version/version.go b/version/version.go new file mode 100644 index 0000000..5e311ec --- /dev/null +++ b/version/version.go @@ -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" +)