diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 04d20be..0ede45e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index bc518a5..319ca64 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/.goreleaser.yml b/.goreleaser.yml index 954cfc1..9d1cc1c 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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 diff --git a/Makefile b/Makefile index 0ea640b..969a892 100644 --- a/Makefile +++ b/Makefile @@ -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" @@ -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 diff --git a/examples/advanced/import-multi/export.sh b/examples/advanced/import-multi/export.sh index 01bac65..c2bf2ee 100755 --- a/examples/advanced/import-multi/export.sh +++ b/examples/advanced/import-multi/export.sh @@ -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 diff --git a/go.mod b/go.mod index 995c9a4..9582bef 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/hack/print-replaces.go b/hack/print-replaces.go new file mode 100644 index 0000000..c2dbb48 --- /dev/null +++ b/hack/print-replaces.go @@ -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, ",")) +} diff --git a/hack/sdk-vars.sh b/hack/sdk-vars.sh new file mode 100755 index 0000000..fec5a87 --- /dev/null +++ b/hack/sdk-vars.sh @@ -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 diff --git a/pkg/app/app_shim.go b/pkg/app/app_shim.go index da8ee89..b93f300 100644 --- a/pkg/app/app_shim.go +++ b/pkg/app/app_shim.go @@ -2,6 +2,7 @@ package app import ( "bytes" + "errors" "fmt" "io" "io/ioutil" @@ -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 { @@ -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 } @@ -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") @@ -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, diff --git a/pkg/sdk/vars.go b/pkg/sdk/vars.go new file mode 100644 index 0000000..ee284a6 --- /dev/null +++ b/pkg/sdk/vars.go @@ -0,0 +1,5 @@ +package sdk + +var Version string + +var ModReplaces string