Skip to content

Commit

Permalink
Add release target and script
Browse files Browse the repository at this point in the history
So that we can push releases to Github
  • Loading branch information
joshmyers committed Jul 24, 2017
1 parent 4776b36 commit 8ba297e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
30 changes: 20 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,36 @@ cd $(1) && go test -v -parallel 128
endef

define cross_build
mkdir -p bin/$(1)/$(2);
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 go build -o bin/$(1)/$(2)/$(PKG) -a -tags "static_build $(PKG)" -installsuffix $(PKG) ${GO_LDFLAGS_STATIC};
mkdir -p ./bin;
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 go build -o bin/$(PKG)_$(1)_$(2) -a -tags "static_build $(PKG)" -installsuffix $(PKG) ${GO_LDFLAGS_STATIC};
endef

.PHONY: install-deps
install-deps: ## Install deps with Glide
@echo "Installing deps..."
glide install
@echo "==> Installing deps..."
@glide install

.PHONY: fmt
fmt: ## Run gofmt over all *.go files
@echo "Running source files through gofmt..."
$(GOFMT_CMD)
@echo "==> Running source files through gofmt..."
@$(GOFMT_CMD)

.PHONY: build
build: install-deps ## build Go binary for all GOARCH
@echo "Building dynolocker for all GOARCH/GOOS"
$(foreach GOARCH,$(GOARCHS),$(foreach GOOS,$(GOOSES),$(call cross_build,$(GOOS),$(GOARCH))))
@echo "==> Building dynolocker for all GOARCH/GOOS..."
@$(foreach GOARCH,$(GOARCHS),$(foreach GOOS,$(GOOSES),$(call cross_build,$(GOOS),$(GOARCH))))

.PHONY: test
test: install-deps ## Run tests
@echo "Running tests..."
$(foreach TEST_DIR,$(TEST_DIRS),$(call test,$(TEST_DIR)))
@echo "==> Running tests..."
@$(foreach TEST_DIR,$(TEST_DIRS),$(call test,$(TEST_DIR)))

check_github_token:
$(if ${GITHUB_OAUTH},,$(error GITHUB_OAUTH not set, please set ENV var))

.PHONY: release
release: test fmt build check_github_token ## Release to Github
@echo "==> Releasing binary artifacts..."
@sh -c "./scripts/release.sh"

export
63 changes: 63 additions & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash
#
# Pushes a release to Github via the Github releases API

set -e

if [[ -z "$PKG" ]] \
|| [[ -z "$VERSION" ]] \
|| [[ -z "$GITHUB_OAUTH" ]]
then
echo "Please don't run this script directly, instead run 'make release'"
exit 1
fi

AUTH="Authorization: token ${GITHUB_OAUTH}"
GH_API="https://api.github.com"
GH_ASSET_UPLOAD="https://uploads.github.com"
GH_REPO="${GH_API}/repos/alphagov/${PKG}"
RELEASE_FILE=$(mktemp)

# Validate token.
curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; }

# Get the parent directory of where this script is.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"

# Change into that directory
cd $DIR/bin

git tag v${VERSION}
git push origin --tags

cat << EOF > "${RELEASE_FILE}"
{
"tag_name": "v${VERSION}",
"target_commitish": "$(git rev-parse HEAD)",
"name": "v${VERSION}",
"body": "Release of ${PKG} v${VERSION}",
"draft": false,
"prerelease": false
}
EOF

release_id=$(curl -X POST \
--data @"${RELEASE_FILE}" \
-H "Authorization: token ${GITHUB_OAUTH}" \
-H "Content-Type: application/json" \
"${GH_REPO}/releases" | jq -r .id)

rm -rf ${RELEASE_FILE}

for bin in $(ls *"${PKG}"* | grep -v zip); do
echo "==> Zipping artifact... ${bin}" >&2
zip -r ${bin}.zip ${bin}
echo "==> Uploading artifact... ${bin}.zip" >&2
curl -X POST \
--data-binary @"${bin}.zip" \
-H "${AUTH}" \
-H "Content-Type: application/zip" \
"${GH_ASSET_UPLOAD}/repos/alphagov/${PKG}/releases/${release_id}/assets?name=${bin}.zip"
done

0 comments on commit 8ba297e

Please sign in to comment.