From 0e7145555cedc96d21354db9d2168ddbd292d2d8 Mon Sep 17 00:00:00 2001 From: Sunil Arora Date: Wed, 11 Apr 2018 15:16:57 -0700 Subject: [PATCH] kustomize: automate github release publishing An example release looks like: https://github.com/droot/kubectl/releases/tag/v0.1.0 --- build/.goreleaser.yml | 29 +++++++++++++++++++++++++++++ build/build.sh | 31 +++++++++++-------------------- build/cloudbuild.yaml | 17 ++++++----------- build/cloudbuild_local.yaml | 11 ++++++----- 4 files changed, 52 insertions(+), 36 deletions(-) create mode 100644 build/.goreleaser.yml diff --git a/build/.goreleaser.yml b/build/.goreleaser.yml new file mode 100644 index 000000000..fc91ac734 --- /dev/null +++ b/build/.goreleaser.yml @@ -0,0 +1,29 @@ +# This is an example goreleaser.yaml file with some sane defaults. +# Make sure to check the documentation at http://goreleaser.com +project_name: kustomize +builds: +- main: ./cmd/kustomize + binary: kustomize + ldflags: -s -X k8s.io/kubectl/cmd/kustomize/version.kustomizeVersion={{.Version}} -X k8s.io/kubectl/cmd/kustomize/version.gitCommit={{.Commit}} -X k8s.io/kubectl/cmd/kustomize/version.buildDate={{.Date}} + goos: + - darwin + - linux + - windows + env: + - CGO_ENABLED=0 +archive: + wrap_in_directory: true +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ .Tag }}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' +release: + github: + owner: droot + name: kubectl diff --git a/build/build.sh b/build/build.sh index a557fcf2a..1ceba94cc 100755 --- a/build/build.sh +++ b/build/build.sh @@ -22,30 +22,21 @@ set -x # - Create the directory to host the code that matches the expected GOPATH package locations # - Use /go as the default GOPATH because this is what the image uses # - Link our current directory (containing the source code) to the package location in the GOPATH + export PKG=k8s.io export REPO=kubectl export CMD=kustomize -mkdir -p /go/src/$PKG -ln -s $(pwd) /go/src/$PKG/$REPO +GO_PKG_OWNER=$GOPATH/src/$PKG +GO_PKG_PATH=$GO_PKG_OWNER/$REPO -# Create the output directory for the binaries we will build -# Make sure CGO is 0 so the binaries are statically compiled and linux which is necessary for cross compiling go -export CGO=0 -export DEST=/workspace/_output/$CMD/bin -mkdir -p $DEST || echo "" +mkdir -p $GO_PKG_OWNER +ln -s $(pwd) $GO_PKG_PATH -go build -o $DEST/$CMD $PKG/$REPO/cmd/$CMD +# When invoked in container builder, this script runs under /workspace which is +# not under $GOPATH, so we need to `cd` to repo under GOPATH for it to build +cd $GO_PKG_PATH -# Explicitly set the values of the variables in package "X" using ldflag so that they are statically compiled into -# the "version" command -export GITCOMMIT=$(git rev-parse HEAD) -export BUILDDATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') -export X=$PKG/$REPO/cmd/$CMD/version -go build -o $DEST/$CMD \ - -ldflags "-X $X.kustomizeVersion=$TAG -X $X.goos=$GOOS -X $X.goarch=$GOARCH -X $X.gitCommit=$GITCOMMIT -X $X.buildDate=$BUILDDATE" \ - $PKG/$REPO/cmd/$CMD - -# Generate the tar archive -cd /workspace/_output/ -tar -czvf /workspace/$CMD-$VERSION-$GOOS-$GOARCH.tar.gz $CMD +/goreleaser release --config=cmd/$CMD/build/.goreleaser.yml --debug --rm-dist +# --skip-validate +# --snapshot --skip-publish diff --git a/build/cloudbuild.yaml b/build/cloudbuild.yaml index a0b63264a..3df58ecd8 100644 --- a/build/cloudbuild.yaml +++ b/build/cloudbuild.yaml @@ -16,15 +16,10 @@ steps: - name: "ubuntu" args: ["mkdir", "-p", "/workspace/_output"] -- name: "golang:1.10-stretch" +- name: "gcr.io/kustomize-199618/golang_with_goreleaser:1.10-stretch" args: ["bash", "cmd/kustomize/build/build.sh"] - env: - - 'GOOS=${_GOOS}' - - 'GOARCH=${_GOARCH}' - - 'VERSION=${TAG_NAME}' -- name: 'gcr.io/cloud-builders/gsutil' - args: ['-h', 'Content-Type:application/gzip', 'cp', '-a', 'public-read', 'kustomize-${TAG_NAME}-${_GOOS}-${_GOARCH}.tar.gz', 'gs://kustomize-release/kustomize-${TAG_NAME}-${_GOOS}-${_GOARCH}.tar.gz'] - env: - - 'GOOS=${_GOOS}' - - 'GOARCH=${_GOARCH}' - - 'VERSION=${TAG_NAME}' + secretEnv: ['GITHUB_TOKEN'] +secrets: +- kmsKeyName: projects/kustomize-199618/locations/global/keyRings/github-tokens/cryptoKeys/gh-release-token + secretEnv: + GITHUB_TOKEN: CiQAyrREbPgXJOeT7M3t+WlxkhXwlMPudixBeiyWTjmLOMLqdK4SUQA0W+xUmDJKAhyfHCcwqSEzUn9OwKC7XAYcmwe0CCKTCbPbDgmioDK24q3LVapndXNvnnHvCjhOJNEr1o+P1DCF+LlzYV2YL8lP09rrKrslPg== diff --git a/build/cloudbuild_local.yaml b/build/cloudbuild_local.yaml index 66f9b87e5..6de58c4df 100644 --- a/build/cloudbuild_local.yaml +++ b/build/cloudbuild_local.yaml @@ -23,9 +23,10 @@ steps: - name: "ubuntu" args: ["mkdir", "-p", "/workspace/_output"] -- name: "golang:1.10-stretch" +- name: "gcr.io/kustomize-199618/golang_with_goreleaser:1.10-stretch" args: ["bash", "cmd/kustomize/build/build.sh"] - env: - - 'GOOS=${_GOOS}' - - 'GOARCH=${_GOARCH}' - - 'VERSION=${TAG_NAME}' + secretEnv: ['GITHUB_TOKEN'] +secrets: +- kmsKeyName: projects/kustomize-199618/locations/global/keyRings/github-tokens/cryptoKeys/gh-release-token + secretEnv: + GITHUB_TOKEN: CiQAyrREbPgXJOeT7M3t+WlxkhXwlMPudixBeiyWTjmLOMLqdK4SUQA0W+xUmDJKAhyfHCcwqSEzUn9OwKC7XAYcmwe0CCKTCbPbDgmioDK24q3LVapndXNvnnHvCjhOJNEr1o+P1DCF+LlzYV2YL8lP09rrKrslPg==