From 3faf0e6d935043b9876526da157ef6cd1dd42de0 Mon Sep 17 00:00:00 2001 From: cvbarros Date: Wed, 31 Jul 2019 17:45:29 +0200 Subject: [PATCH] Add Github actions to automate release publishing --- .editorconfig | 6 +-- .github/builder/Dockerfile | 19 +++++++++ .github/builder/README.MD | 60 +++++++++++++++++++++++++++ .github/builder/entrypoint.sh | 77 +++++++++++++++++++++++++++++++++++ .github/main.workflow | 24 +++++++++++ Makefile | 40 ++++++++++++++++++ 6 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 .github/builder/Dockerfile create mode 100644 .github/builder/README.MD create mode 100755 .github/builder/entrypoint.sh create mode 100644 .github/main.workflow create mode 100644 Makefile diff --git a/.editorconfig b/.editorconfig index 93ea43d2..2f3daecc 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,7 +5,6 @@ root = true end_of_line = lf indent_size = 4 indent_style = space -insert_final_newline = true trim_trailing_whitespace = true insert_final_newline = true charset = utf-8 @@ -13,12 +12,13 @@ charset = utf-8 ; Golang [*.go] indent_style = tab -indent_size = 4 [*.{tf, md, markdown}] -indent_style = space indent_size = 2 [*.md] trim_trailing_whitespace = false +[{Makefile,**.mk}] +indent_style = tab + diff --git a/.github/builder/Dockerfile b/.github/builder/Dockerfile new file mode 100644 index 00000000..798bfe20 --- /dev/null +++ b/.github/builder/Dockerfile @@ -0,0 +1,19 @@ +FROM golang:1.11 + +LABEL "name"="Go Builder" +LABEL "version"="0.2.0" + +LABEL "com.github.actions.name"="Go Builder" +LABEL "com.github.actions.description"="Cross-complile Go programs" +LABEL "com.github.actions.icon"="package" +LABEL "com.github.actions.color"="#E0EBF5" + +RUN \ + apt-get update && \ + apt-get install -y ca-certificates openssl zip && \ + update-ca-certificates && \ + rm -rf /var/lib/apt + +COPY entrypoint.sh /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/.github/builder/README.MD b/.github/builder/README.MD new file mode 100644 index 00000000..95bd418b --- /dev/null +++ b/.github/builder/README.MD @@ -0,0 +1,60 @@ +# Go Builder + +This action is adapted from https://github.com/sosedoff/actions/tree/master/golang-build + +Github Action to cross-compile Go project binaries for multiple platforms in a single run. + +Uses `golang:1.11` Docker image with `CGO_ENABLED=0` flag. + +## Usage + +Basic usage: + +``` +action "build" { + uses = "./actions/build" +} +``` + +Basic workflow configuration will compile binaries for the following platforms: + +- linux: 386/amd64 +- darwin: 386/amd64 +- windows: 386/amd64 + +Alternatively you can provide a list of target architectures in `arg`: + +``` +action "build" { + uses = "./actions/build" + args = "linux/amd64 darwin/amd64" +} +``` + +Example output: + +``` +----> Setting up Go repository +----> Building project for: darwin/amd64 + adding: test-go-action_darwin_amd64 (deflated 50%) +----> Building project for: darwin/386 + adding: test-go-action_darwin_386 (deflated 45%) +----> Building project for: linux/amd64 + adding: test-go-action_linux_amd64 (deflated 50%) +----> Building project for: linux/386 + adding: test-go-action_linux_386 (deflated 45%) +----> Building project for: windows/amd64 + adding: test-go-action_windows_amd64 (deflated 50%) +----> Building project for: windows/386 + adding: test-go-action_windows_386 (deflated 46%) +----> Build is complete. List of files at /github/workspace/.release: +total 16436 +drwxr-xr-x 2 root root 4096 Feb 5 00:03 . +drwxr-xr-x 5 root root 4096 Feb 5 00:02 .. +-rw-r--r-- 1 root root 978566 Feb 5 00:02 test-go-action_darwin_386.zip +-rw-r--r-- 1 root root 1008819 Feb 5 00:02 test-go-action_darwin_amd64.zip +-rw-r--r-- 1 root root 918555 Feb 5 00:02 test-go-action_linux_386.zip +-rw-r--r-- 1 root root 952985 Feb 5 00:02 test-go-action_linux_amd64.zip +-rw-r--r-- 1 root root 930942 Feb 5 00:03 test-go-action_windows_386.zip +-rw-r--r-- 1 root root 972286 Feb 5 00:02 test-go-action_windows_amd64.zip +``` diff --git a/.github/builder/entrypoint.sh b/.github/builder/entrypoint.sh new file mode 100755 index 00000000..cfdcc67c --- /dev/null +++ b/.github/builder/entrypoint.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +set -e + +if [[ -z "$GITHUB_WORKSPACE" ]]; then + echo "Set the GITHUB_WORKSPACE env variable." + exit 1 +fi + +if [[ -z "$GITHUB_REPOSITORY" ]]; then + echo "Set the GITHUB_REPOSITORY env variable." + exit 1 +fi + +if [[ -z "$GITHUB_REF" ]]; then + echo "Set the GITHUB_REF env variable." + exit 1 +fi + +# Regex and validate-version function adapted from https://github.com/fsaintjacques/semver-tool/blob/master/src/semver +SEMVER_REGEX="^[v]?(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$" + +function validate-version { + local version=$1 + if [[ "$version" =~ $SEMVER_REGEX ]]; then + # if a second argument is passed, store the result in var named by $2 + if [ "$#" -eq "2" ]; then + local major=${BASH_REMATCH[1]} + local minor=${BASH_REMATCH[2]} + local patch=${BASH_REMATCH[3]} + local prere=${BASH_REMATCH[4]} + local build=${BASH_REMATCH[5]} + eval "$2=(\"$major\" \"$minor\" \"$patch\" \"$prere\" \"$build\")" + else + echo "$version" + fi + else + echo -e "version '$version' does not match the semver scheme 'X.Y.Z(-PRERELEASE)(+BUILD)'. Ensure this action is being run for a tag and not a branch." >&2 + exit 1 + fi +} + +root_path="/go/src/github.com/$GITHUB_REPOSITORY" +release_path="$GITHUB_WORKSPACE/.release" +repo_name="$(echo $GITHUB_REPOSITORY | cut -d '/' -f2)" +targets=${@-"darwin/amd64 darwin/386 linux/amd64 linux/386 windows/amd64 windows/386"} +version="${GITHUB_REF##*/}" + +validate-version $version + +echo "----> Setting up Go repository" +mkdir -p $release_path +mkdir -p $root_path +echo "----> $GITHUB_WORKSPACE contents" +ls $GITHUB_WORKSPACE/ +cp -a $GITHUB_WORKSPACE/* $root_path/ +cd $root_path + +for target in $targets; do + os="$(echo $target | cut -d '/' -f1)" + arch="$(echo $target | cut -d '/' -f2)" + output="${release_path}/${repo_name}_${os}_${arch}_${version}" + binary_name="${release_path}/${repo_name}_${version}" + + if [[ "$os" == "windows" ]]; then + binary_name="$binary_name.exe" + fi + + echo "----> Building project for: $target" + GOOS=$os GOARCH=$arch CGO_ENABLED=0 go build -o $binary_name + zip -j $output.zip $binary_name > /dev/null + rm -rf $binary_name +done + +echo "----> Build is complete. List of files at $release_path:" +cd $release_path +ls -al diff --git a/.github/main.workflow b/.github/main.workflow new file mode 100644 index 00000000..59f4a105 --- /dev/null +++ b/.github/main.workflow @@ -0,0 +1,24 @@ +workflow "Publish binaries on release" { + resolves = ["publish"] + on = "push" +} + +action "release-tag" { + uses = "actions/bin/filter@master" + args = "tag" +} + +action "build" { + uses = "./.github/builder" + env = { + GO111MODULE = "on" + } + needs = ["release-tag"] +} + +action "publish" { + uses = "docker://moonswitch/github-upload-release:master" + secrets = ["GITHUB_TOKEN"] + needs = ["build"] + args = "not actor nektos/act" +} diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..c89cbad2 --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ +.PHONY: build build-alpine clean test help default + +export GO111MODULE=on +BIN_NAME=terraform-provider-teamcity + +VERSION := $(shell grep "const Version " version/version.go | sed -E 's/.*"(.+)"$$/\1/') +GIT_COMMIT=$(shell git rev-parse HEAD) +GIT_DIRTY=$(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true) +BUILD_DATE=$(shell date '+%Y-%m-%d-%H:%M:%S') +BUILDER_IMAGE=cvbarros/terraform-provider-teamcity-builder + +default: test + +help: + @echo 'Management commands for sandbox:' + @echo + @echo 'Usage:' + @echo ' make build Compile the project.' + @echo ' make get-deps runs dep ensure, mostly used for ci.' + + @echo ' make clean Clean the directory tree.' + @echo + +build: + @echo "building ${BIN_NAME} ${VERSION}" + @echo "GOPATH=${GOPATH}" + go build -ldflags "-X github.com/cvbarros/terraform-provider-teamcity/version.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X github.com/cvbarros/terraform-provider-teamcity/version.BuildDate=${BUILD_DATE}" -o ${BIN_NAME} + +builder-action: + docker run -e GITHUB_WORKSPACE='/github/workspace' -e GITHUB_REPOSITORY='terraform-provider-teamcity' -e GITHUB_REF='v0.0.1-alpha' --name terraform-provider-teamcity-builder $(BUILDER_IMAGE):latest + +builder-image: + docker build .github/builder --tag $(BUILDER_IMAGE) + +clean: + @test ! -e bin/${BIN_NAME} || rm bin/${BIN_NAME} + +test: + go test ./... +