Skip to content

Commit

Permalink
Merge pull request #56 from cvbarros/automated-releases
Browse files Browse the repository at this point in the history
Add Github actions to automate release publishing
  • Loading branch information
cvbarros authored Jul 31, 2019
2 parents 2e6b0ad + 3faf0e6 commit 9b493ed
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ 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

; 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

19 changes: 19 additions & 0 deletions .github/builder/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
60 changes: 60 additions & 0 deletions .github/builder/README.MD
Original file line number Diff line number Diff line change
@@ -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
```
77 changes: 77 additions & 0 deletions .github/builder/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions .github/main.workflow
Original file line number Diff line number Diff line change
@@ -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"
}
40 changes: 40 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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 ./...

0 comments on commit 9b493ed

Please sign in to comment.