Skip to content

Commit

Permalink
Initial Build harness (#2)
Browse files Browse the repository at this point in the history
* Initial commit

* Travis docker harness

* Travis docker harness

* Travis docker harness

* Add github download release

* WIP: Initial harness (#1)

* Travis docker harness

* Travis docker harness

* Travis docker harness

* Add github download release

* Implement harness support

* Implement harness support

* Added new harness

* Added new harness

* Added new harness

* Added new harness

* WIP - fix

* WIP - fix

* Added initial make files

* Added build-harness right way

* Added build-harness right way

* Added build-harness right way

* Added build-harness right way

* Move complex bash scripts to separete files

* Move complex bash scripts to separete files

* Move complex bash scripts to separete files

* Added build-harness right way

* Added build-harness right way

* Added build-harness right way

* Added build-harness right way

* Added build-harness right way

* Added build-harness right way

* Added build-harness right way

* Added build-harness right way

* Added build-harness right way

* Added build-harness right way

* Added build-harness right way
  • Loading branch information
goruha authored Jan 20, 2017
1 parent 513dd19 commit 04be672
Show file tree
Hide file tree
Showing 21 changed files with 363 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Override for Makefile
[{Makefile, makefile, GNUmakefile}]
indent_style = tab
indent_size = 4

[Makefile.*]
indent_style = tab
indent_size = 4
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include $(BUILD_HARNESS_PATH)/Makefile.*

include $(BUILD_HARNESS_PATH)/modules/*/*
41 changes: 41 additions & 0 deletions Makefile.helpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Helpers - stuff that's shared between make files
#

EDITOR ?= vim

SHELL = /bin/bash

green = $(shell echo -e '\x1b[32;01m$1\x1b[0m')
yellow = $(shell echo -e '\x1b[33;01m$1\x1b[0m')
red = $(shell echo -e '\x1b[33;31m$1\x1b[0m')


# Ensures that a variable is defined
define assert-set
@[ -n "$$$1" ] || (echo "$(1) not defined in $(@)"; exit 1)
endef

# Ensures that a variable is undefined
define assert-unset
@[ -z "$$$1" ] || (echo "$(1) should not be defined in $(@)"; exit 1)
endef

default: help

.PHONY : help
## This help screen
help:
@printf "Available targets:\n\n"
@awk '/^[a-zA-Z\-\_0-9%:\\]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = $$1; \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
gsub("\\\\", "", helpCommand); \
gsub(":+$$", "", helpCommand); \
printf " \x1b[32;01m%-35s\x1b[0m %s\n", helpCommand, helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST) | sort -u
@printf "\n"
56 changes: 56 additions & 0 deletions bin/github_download_release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
#
# gh-dl-release! It works!
#
# Source https://gist.github.com/maxim/6e15aa45ba010ab030c4
#
# This script downloads an asset from latest or specific Github release of a
# private repo. Feel free to extract more of the variables into command line
# parameters.
#
# PREREQUISITES
#
# curl, wget, jq
#
# USAGE
#
# Set all the variables inside the script, make sure you chmod +x it, then
# to download specific version to my_app.tar.gz:
#
# gh-dl-release 2.1.1 my_app.tar.gz
#
# to download latest version:
#
# gh-dl-release latest latest.tar.gz
#
# If your version/tag doesn't match, the script will exit with error.

#GITHUB_TOKEN="<github_access_token>"
#REPO="<user_or_org>/<repo_name>"
#FILE="<name_of_asset_file>" # the name of your release asset file, e.g. build.tar.gz
VERSION=$1 # tag name or the word "latest"
GITHUB="https://api.github.com"

alias errcho='>&2 echo'

function gh_curl() {
curl -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3.raw" \
$@
}

if [ "$VERSION" = "latest" ]; then
# Github should return the latest release first.
parser=".[0].assets | map(select(.name == \"$FILE\"))[0].id"
else
parser=". | map(select(.tag_name == \"$VERSION\"))[0].assets | map(select(.name == \"$FILE\"))[0].id"
fi;

asset_id=`gh_curl -s $GITHUB/repos/$REPO/releases | jq "$parser"`
if [ "$asset_id" = "null" ]; then
echo "ERROR: version not found $VERSION"
exit 1
fi;

curl -H 'Accept:application/octet-stream' -o $2 -L \
https://$GITHUB_TOKEN:@api.github.com/repos/$REPO/releases/assets/$asset_id
11 changes: 11 additions & 0 deletions bin/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
export BUILD_HARNESS_PROJECT=${2:-build-harness}
export BUILD_HARNESS_BRANCH=${3:-master}
export GITHUB_REPO="https://github.com/cloudposse/${BUILD_HARNESS_PROJECT}.git"

if [ "$BUILD_HARNESS_PROJECT" ] && [ -d "$BUILD_HARNESS_PROJECT" ]; then
echo "Removing existing $BUILD_HARNESS_PROJECT"
rm -rf "$BUILD_HARNESS_PROJECT"
fi

git clone -b $BUILD_HARNESS_BRANCH $GITHUB_REPO
30 changes: 30 additions & 0 deletions bin/travis_docker_tag_and_push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

DOCKER_TAGS=($TRAVIS_COMMIT)

if [ ! -z "$TRAVIS_TAG" ]; then
DOCKER_TAGS+=($TRAVIS_TAG)
fi

if [ ! -z "$TRAVIS_PULL_REQUEST_BRANCH" ]; then
DOCKER_TAGS+=(pr-$TRAVIS_PULL_REQUEST_BRANCH)
DOCKER_TAGS+=(pr-$TRAVIS_PULL_REQUEST_BRANCH-$TRAVIS_BUILD_NUMBER)
fi

if [[ -z "$TRAVIS_PULL_REQUEST_BRANCH" && ! -z "$TRAVIS_BRANCH" ]]; then
DOCKER_TAGS+=($TRAVIS_BRANCH)
DOCKER_TAGS+=($TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER)
fi

if [[ -z "$TRAVIS_TAG" && -z "$TRAVIS_PULL_REQUEST_BRANCH" && -z "$TRAVIS_BRANCH" ]]; then
DOCKER_TAGS+=(latest)
fi

if [[ -z "$TRAVIS_PULL_REQUEST_BRANCH" ]] && [[ "$TRAVIS_BRANCH" == "master" ]]; then
DOCKER_TAGS+=(latest)
fi


for TAG in "${DOCKER_TAGS[@]}"; do
docker tag $DOCKER_IMAGE_NAME $DOCKER_IMAGE_NAME:$TAG && docker push $DOCKER_IMAGE_NAME:$TAG;
done
1 change: 1 addition & 0 deletions modules/docker/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DOCKER:= $(shell which docker)
12 changes: 12 additions & 0 deletions modules/docker/Makefile.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.PHONY: docker\:build
## Use DOCKER_IMAGE_NAME envvar to specify docker image with tags
## Build docker image
docker\:build: $(DOCKER)
$(DOCKER) build --no-cache -t $(DOCKER_IMAGE_NAME) .

.PHONY: docker\:build-with-args
## Use DOCKER_IMAGE_NAME envvar to specify docker image with tags
## User ARGS to pass arguments
## Build docker image with args
docker\:build-with-args: $(DOCKER)
@$(DOCKER) build --no-cache --build-arg $(ARGS) -t $(DOCKER_IMAGE_NAME) .
5 changes: 5 additions & 0 deletions modules/docker/Makefile.hub
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.PHONY: docker\:login
## Use DOCKER_HUB_USERNAME and DOCKER_HUB_PASSWORD env variables to pass credentials
## Login into docker hub
docker\:login: $(DOCKER)
@$(DOCKER) login --username="$(DOCKER_HUB_USERNAME)" --password="$(DOCKER_HUB_PASSWORD)"
22 changes: 22 additions & 0 deletions modules/docs/Makefile.copyright
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
COPYRIGHT_CMD ?= docker run --rm --volume `pwd`:$(COPYRIGHT_OUTPUT_DIR) osterman/copyright-header:latest
COPYRIGHT_LICENSE ?= ASL2
COPYRIGHT_HOLDER ?= Cloud Posse, LLC <hello@cloudposse.com>
COPYRIGHT_YEAR ?= $(shell date +%Y)
COPYRIGHT_SOFTWARE ?= Example App
COPYRIGHT_SOFTWARE_DESCRIPTION ?= Example Software Description
COPYRIGHT_OUTPUT_DIR ?= /usr/src
COPYRIGHT_WORD_WRAP ?= 100

.PHONY : docs\:copyright-add
## Add copyright to source code
docs\:copyright-add:
$(COPYRIGHT_CMD) \
--license $(COPYRIGHT_LICENSE) \
--add-path cmd:main.go \
--guess-extension \
--copyright-holder '$(COPYRIGHT_HOLDER)' \
--copyright-software '$(COPYRIGHT_SOFTWARE)' \
--copyright-software-description '$(COPYRIGHT_SOFTWARE_DESCRIPTION)' \
--copyright-year $(COPYRIGHT_YEAR) \
--word-wrap $(COPYRIGHT_WORD_WRAP) \
--output-dir $(COPYRIGHT_OUTPUT_DIR)
4 changes: 4 additions & 0 deletions modules/docs/Makefile.docs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY : docs\:toc-update
## Update table of contents in README.md
docs\:toc-update:
@doctoc --notitle --github README.md
7 changes: 7 additions & 0 deletions modules/github/Makefile.release
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: github\:download-release
## GITHUB_TOKEN="<github_access_token>"
## REPO="<user_or_org>/<repo_name>"
## FILE="<name_of_asset_file>" # the name of your release asset file, e.g. build.tar.gz
## Download release from github
github\:download-release:
$(BUILD_HARNESS_PATH)/bin/github_download_release.sh $(VERSION) $(OUTPUT)
1 change: 1 addition & 0 deletions modules/go/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GO:= $(shell which go)
48 changes: 48 additions & 0 deletions modules/go/Makefile.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
GLIDE := $(shell which glide)
INSTALL_DIR ?= /usr/local/sbin
RELEASE_DIR ?= release
APP?=Example App

.PHONY: go\:build
## Build binary
go\:build: $(GO)
$(GO) build -o $(RELEASE_DIR)/$(APP)

.PHONY: go\:build-all
## Build binary for all platforms
go\:build-all: $(GO)
gox -output "${RELEASE_DIR}/${APP}_{{.OS}}_{{.Arch}}"

.PHONY: go\:deps
## Install dependencies
go\:deps: $(GLIDE)
$(GLIDE) install --strip-vendor
$(GLIDE) update --strip-vendor

.PHONY: go\:deps-build
## Install dependencies for build
go\:deps-build:
mkdir -p $(GOPATH)/bin
which $(GLIDE) || (curl https://glide.sh/get | sh)

.PHONY: deps-dev
## Install development dependencies
go\:deps-dev: $(GO)
$(GO) get -d -v "github.com/golang/lint"
$(GO) install -v "github.com/golang/lint/golint"
$(GO) get -d -v github.com/mitchellh/gox
$(GO) install -v github.com/mitchellh/gox

## Clean compiled binary
go\:clean:
rm -rf $(RELEASE_DIR)

## Clean compiled binary and dependency
go\:clean-all: go\:clean
rm -rf vendor
rm -rf glide.lock

## Install cli
go\:install: $(APP) go\:build
cp $(RELEASE_DIR)/$(APP) $(INSTALL_DIR)
chmod 555 $(INSTALL_DIR)/$(APP)
14 changes: 14 additions & 0 deletions modules/go/Makefile.style
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.PHONY: go\:lint
## Lint code
go\:lint: $(GO) go\:vet
find . ! -path "*/vendor/*" ! -path "*/.glide/*" -type f -name '*.go' | xargs -n 1 golint

.PHONY: go\:vet
## Vet code
go\:vet: $(GO)
find . ! -path "*/vendor/*" ! -path "*/.glide/*" -type f -name '*.go' | xargs -n 1 $(GO) vet -v

.PHONY: go\:fmt
## Format code according to Golang convention
go\:fmt: $(GO)
find . ! -path "*/vendor/*" ! -path "*/.glide/*" -type f -name '*.go' | xargs -n 1 gofmt -w -l -s
4 changes: 4 additions & 0 deletions modules/go/Makefile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: go\:test
## Run tests
go\:test: $(GO)
$(GO) test $(shell $(GO) list . | grep -v /vendor/)
4 changes: 4 additions & 0 deletions modules/travis/Makefile.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: travis\:docker-tag-and-push
## Tag according travis envvars and push
travis\:docker-tag-and-push:
$(BUILD_HARNESS_PATH)/bin/travis_docker_tag_and_push.sh
8 changes: 8 additions & 0 deletions templates/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Override for Makefile
[{Makefile, makefile, GNUmakefile}]
indent_style = tab
indent_size = 4

[Makefile.*]
indent_style = tab
indent_size = 4
17 changes: 17 additions & 0 deletions templates/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SHELL = /bin/bash
export BUILD_HARNESS_PATH ?= $(shell until [ -d "build-harness" ] || [ "`pwd`" == '/' ]; do cd ..; done; pwd)/build-harness
-include $(BUILD_HARNESS_PATH)/Makefile

####################### If go app ####################################################
override APP:=Example App
######################################################################################

.PHONY : init
## Init build-harness
init:
@curl --retry 5 --retry-delay 1 https://raw.githubusercontent.com/cloudposse/build-harness/master/bin/install.sh | bash

.PHONY : clean
## Clean build-harness
clean:
rm -rf $(BUILD_HARNESS_PATH)
20 changes: 20 additions & 0 deletions templates/docker.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
sudo: required
addons:
apt:
packages:
- git
- make
- curl
env:
- DOCKER_IMAGE_NAME=cloudposse/#####################
services:
- docker
install:
- make init
- make docker:login

script:
- make docker:build

after_success:
- make travis:docker-tag-and-push
47 changes: 47 additions & 0 deletions templates/go.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
sudo: required
language: go
go:
- 1.7.x
addons:
apt:
packages:
- git
- make
- curl

env:
- APP=##################

install:
- make init
- make go:deps-build
- make go:deps-dev

script:
- make go:deps
- make go:test
- make go:lint
- make go:build-all

deploy:
provider: releases
api_key: $GITHUB_OAUTH_TOKEN
file:
- release/$APP_darwin_386
- release/$APP_darwin_amd64
- release/$APP_freebsd_386
- release/$APP_freebsd_amd64
- release/$APP_freebsd_arm
- release/$APP_linux_386
- release/$APP_linux_amd64
- release/$APP_linux_arm
- release/$APP_netbsd_386
- release/$APP_netbsd_amd64
- release/$APP_netbsd_arm
- release/$APP_openbsd_386
- release/$APP_openbsd_amd64
- release/$APP_windows_386.exe
- release/$APP_windows_amd64.exe
skip_cleanup: true
on:
tags: true

0 comments on commit 04be672

Please sign in to comment.