Skip to content

Commit

Permalink
Use the same version of sdk as variant for variant export (#41)
Browse files Browse the repository at this point in the history
This changes the build and release scripts to embed the version number of variant deduced from GITHUB_REF in GitHub Actions, and enhances `variant export` to embed that version in generated go.mod files.

Fixes #36
  • Loading branch information
mumoshu authored Dec 6, 2020
1 parent 43ded37 commit ac91ec8
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 32 deletions.
46 changes: 45 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,30 @@ jobs:
which kubectl
sudo apt-get update -y
sudo apt-get install ruby -y
make test smoke
GITHUB_REF=refs/heads/v0.0.0 make test
smoke:
runs-on: ubuntu-latest
strategy:
matrix:
go:
- 1.15.x
name: Go ${{ matrix.go }} smoke test
steps:
- uses: actions/checkout@master
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go }}
- name: Run go mod download
run: go mod download
- name: Install SSH key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_KEY }}
known_hosts: ${{ secrets.KNOWN_HOSTS }}
- name: Run tests
run: |
GITHUB_REF=refs/heads/v0.0.0 make smoke
lint:
runs-on: ubuntu-latest
strategy:
Expand All @@ -44,3 +67,24 @@ jobs:
run: go mod download
- name: Run golangci-lint
run: make lint
goreleaser-test:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v1
-
name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.15.x
-
name: Set goreleaser .Env
run: |
GITHUB_REF=refs/heads/v0.0.0 hack/sdk-vars.sh
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
version: latest
args: release --rm-dist --skip-publish
4 changes: 4 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ jobs:
uses: actions/setup-go@v1
with:
go-version: 1.15.x
-
name: Set goreleaser .Env
run: |
hack/sdk-vars.sh
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
Expand Down
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ builds:
- CGO_ENABLED=0
ldflags:
- -s -w -X github.com/mumoshu/variant2/Version={{.Version}}
- -X github.com/mumoshu/variant2/pkg/sdk.Version={{.Env.VERSION}}
- -X github.com/mumoshu/variant2/pkg/sdk.ModReplaces={{.Env.MOD_REPLACES}}
changelog:
filters:
# commit messages matching the regexp listed here will be removed from
Expand Down
24 changes: 21 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
VARIANT_SDK = github.com/mumoshu/variant2/pkg/sdk

# NOTE:
# You can test the versioned build with e.g. `GITHUB_REF=refs/heads/v0.36.0 make build`
.PHONY: build
build:
go build -o variant ./pkg/cmd
@echo "Building variant"
@{ \
set -e ;\
. hack/sdk-vars.sh ;\
echo Using $(VAARIANT_SDK).Version=$${VERSION} ;\
echo Using $(VAARIANT_SDK).ModReplaces=$${MOD_REPLACES} ;\
set -x ;\
go build \
-ldflags "-X $(VARIANT_SDK).Version=$${VERSION} -X $(VARIANT_SDK).ModReplaces=$${MOD_REPLACES}" \
-o variant ./pkg/cmd ;\
}

bin/goimports:
echo "Installing goimports"
Expand Down Expand Up @@ -98,13 +112,17 @@ smoke: build

make build
rm -rf build/simple
PATH=${PATH}:$(GOBIN) ./variant export go examples/simple build/simple
VARIANT_BUILD_VARIANT_REPLACE=$(shell pwd) \
PATH=${PATH}:$(GOBIN) \
./variant export go examples/simple build/simple
cd build/simple; go build -o simple ./
build/simple/simple -h | tee smoke.log
grep "Namespace to interact with" smoke.log

rm build/simple/simple
PATH=${PATH}:$(GOBIN) ./variant export binary examples/simple build/simple
VARIANT_BUILD_VARIANT_REPLACE=$(shell pwd) \
PATH=${PATH}:$(GOBIN) \
./variant export binary examples/simple build/simple
build/simple/simple -h | tee smoke2.log
grep "Namespace to interact with" smoke2.log

Expand Down
4 changes: 3 additions & 1 deletion examples/advanced/import-multi/export.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
PROJECT_ROOT=../../..
VARIANT=${PROJECT_ROOT}/variant

export VARIANT_BUILD_VER=v0.33.3
export VARIANT_BUILD_VER=v0.36.0
export VARIANT_BUILD_VARIANT_REPLACE=$(pwd)/${PROJECT_ROOT}

rm -rf ../exported
rm -rf ../compiled

export GITHUB_REF=refs/heads/${VARIANT_BUILD_VER}

(cd ${PROJECT_ROOT}; make build)
${VARIANT} export go ../import-multi ../exported
${VARIANT} export binary ../import-multi ../compiled
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/AlecAivazis/survey/v2 v2.0.5
github.com/PaesslerAG/jsonpath v0.1.1 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/fluxcd/pkg/apis/meta v0.0.2
github.com/fluxcd/pkg/untar v0.0.5
github.com/fluxcd/source-controller/api v0.2.0
github.com/go-logr/logr v0.2.1
Expand Down Expand Up @@ -44,11 +45,14 @@ require (
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
gopkg.in/go-playground/validator.v9 v9.31.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
k8s.io/api v0.18.9
k8s.io/apimachinery v0.18.9
k8s.io/client-go v10.0.0+incompatible
sigs.k8s.io/controller-runtime v0.6.4
)

// Required until https://github.com/summerwind/whitebox-controller/pull/8 is merged
replace github.com/summerwind/whitebox-controller v0.7.1 => github.com/mumoshu/whitebox-controller v0.5.1-0.20201028130131-ac7a0743254b

// Required to fix go mod issue that k8s.io/client-go is somehow "updated" to invalid "v10.0.0+incompatible" on build
replace k8s.io/client-go v10.0.0+incompatible => k8s.io/client-go v0.18.9
38 changes: 38 additions & 0 deletions hack/print-replaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"strings"
)

func main() {
content, err := ioutil.ReadFile("go.mod")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}

var replaces []string

for _, l := range strings.Split(string(content), "\n") {
if !strings.HasPrefix(l, "replace ") {
continue
}

if !strings.Contains(l, " => ") {
fmt.Fprintf(os.Stderr, "Unexpected line: ` => ` expected: %s\n", l)
os.Exit(1)
}

l = strings.ReplaceAll(l, "replace ", "")
l = strings.ReplaceAll(l, " => ", "=")
l = strings.ReplaceAll(l, " ", "@")
l = strings.TrimRight(l, "\n")

replaces = append(replaces, l)
}

fmt.Print(strings.Join(replaces, ","))
}
18 changes: 18 additions & 0 deletions hack/sdk-vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

set -e

tag=${GITHUB_REF##*/}

if [ -z "${tag}" ]; then
echo GITHUB_REF must be set 1>&2
exit 1
fi

export VERSION=${tag}
export MOD_REPLACES=$(go run hack/print-replaces.go)

if [ ! -z "${GITHUB_ENV}" ]; then
echo "VERSION=${VERSION}" >> $GITHUB_ENV
echo "MOD_REPLACES=${MOD_REPLACES}" >> $GITHUB_ENV
fi
59 changes: 32 additions & 27 deletions pkg/app/app_shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -13,6 +14,7 @@ import (

"github.com/mumoshu/variant2/pkg/conf"
"github.com/mumoshu/variant2/pkg/fs"
"github.com/mumoshu/variant2/pkg/sdk"
)

func (app *App) ExportBinary(srcDir, dstFile string) error {
Expand Down Expand Up @@ -50,6 +52,18 @@ func (app *App) ExportBinary(srcDir, dstFile string) error {
}

func (app *App) ExportGo(srcDir, dstDir string) error {
variantVer := sdk.Version
if variantVer == "" {
return errors.New("detected empty sdk.Version. This variant2 binary seems to be broken. " +
"Please rebuild it with `go build -ldflags \"-X github.com/mumoshu/variant2/pkg/sdk.Version=v${VERSION}\"")
}

modReplaces := strings.Split(sdk.ModReplaces, ",")
if len(modReplaces) == 0 {
return errors.New("detected empty sdk.ModReplaces. This variant2 binary seems to be broken. " +
"Please rebuild it with `go build -ldflags \"-X github.com/mumoshu/variant2/pkg/sdk.ModReplaces=v${MOD_REPLACES}\"")
}

if err := os.MkdirAll(dstDir, 0o755); err != nil {
return err
}
Expand Down Expand Up @@ -146,20 +160,21 @@ func main() {
return err
}

variantVer := os.Getenv("VARIANT_BUILD_VER")
if variantVer != "" {
_, err = app.execCmd(
nil,
Command{
Name: "sh",
Args: []string{"-c", fmt.Sprintf("cd %s; go mod edit -require=github.com/mumoshu/variant2@%s", dstDir, variantVer)},
Env: map[string]string{},
},
true,
)
if err != nil {
return err
}
if v := os.Getenv("VARIANT_BUILD_VER"); v != "" {
variantVer = v
}

_, err = app.execCmd(
nil,
Command{
Name: "sh",
Args: []string{"-c", fmt.Sprintf("cd %s; go mod edit -require=github.com/mumoshu/variant2@%s", dstDir, variantVer)},
Env: map[string]string{},
},
true,
)
if err != nil {
return err
}

variantReplace := os.Getenv("VARIANT_BUILD_VARIANT_REPLACE")
Expand All @@ -178,22 +193,12 @@ func main() {
}
}

var modReplaces []string
if rs := os.Getenv("VARIANT_BUILD_MOD_REPLACE"); rs != "" {
reps := strings.Split(rs, ",")

modReplace := os.Getenv("VARIANT_BUILD_MOD_REPLACE")

if modReplace != "" {
reps := strings.Split(modReplace, ",")

modReplaces = append(modReplaces, reps...)
modReplaces = reps
}

// Required until https://github.com/summerwind/whitebox-controller/pull/8 is merged
modReplaces = append(modReplaces, "github.com/summerwind/whitebox-controller@v0.7.1=github.com/mumoshu/whitebox-controller@v0.5.1-0.20201028130131-ac7a0743254b")

// Required to fix go mod issue that k8s.io/client-go is somehow "updated" to invalid "v10.0.0+incompatible" on build
modReplaces = append(modReplaces, "k8s.io/client-go@v10.0.0+incompatible=k8s.io/client-go@v0.18.9")

for _, modReplace := range modReplaces {
_, err = app.execCmd(
nil,
Expand Down
5 changes: 5 additions & 0 deletions pkg/sdk/vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sdk

var Version string

var ModReplaces string

0 comments on commit ac91ec8

Please sign in to comment.