Skip to content

Commit

Permalink
CI: Bug fixes, one image per PR (#47)
Browse files Browse the repository at this point in the history
* One image per PR
* Don't use Travis deploys
* Fix if statement in YAML
  • Loading branch information
bzub committed Jul 10, 2017
1 parent 5af635a commit db54caf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 33 deletions.
32 changes: 8 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,22 @@ language: go
go:
- 1.7.x

branches:
only:
- master
- v0.*

env:
global:
- IMG_FQDN=quay.io
- REPO=cloudnativelabs/kube-router
- REPO_PATH=$HOME/gopath/src/github.com/$REPO
- GIT_BRANCH=$TRAVIS_BRANCH
- IMG_FQDN=quay.io

script:
- make all

after_success:
# All successfully built commits get an image placed in the kube-router-git
# image repo and tagged with the commit hash.
- make push

deploy:
# Images from Pull Requests get tagged with the PR number.
- provider: script
on:
condition: $TRAVIS_PULL_REQUEST != "false"
skip_cleanup: true
script:
- make push IMG_PREFIX=PR$TRAVIS_PULL_REQUEST-

# Images from tagged commits get released.
- provider: script
on:
tags: true
# condition: $TRAVIS_PULL_REQUEST == "false"
skip_cleanup: true
script:
- unset IMG_FQDN # Use DockerHub for releases
- make release
- build/travis-deploy.sh

# This fixes issues when a contributor uses their own Travis CI account to test
# code/CI changes.
Expand Down
33 changes: 25 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ NAME?=kube-router
DEV_SUFFIX?=-git
LOCAL_PACKAGES?=app app/controllers app/options app/watchers
IMG_NAMESPACE?=cloudnativelabs
IMG_TAG?=$(shell git describe --tags --dirty)
GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
GIT_COMMIT=$(shell git describe --tags --dirty)
IMG_TAG?=$(if $(IMG_TAG_PREFIX),$(IMG_TAG_PREFIX)-)
# GIT_BRANCH?=$(shell git rev-parse --abbrev-ref HEAD)
RELEASE_TAG?=$(shell build/get-git-tag.sh)
REGISTRY?=$(if $(IMG_FQDN),$(IMG_FQDN)/$(IMG_NAMESPACE)/$(NAME),$(IMG_NAMESPACE)/$(NAME))
REGISTRY_DEV?=$(REGISTRY)$(DEV_SUFFIX)
Expand All @@ -16,41 +17,57 @@ UPSTREAM_IMPORT_PATH=$(GOPATH)/src/github.com/cloudnativelabs/kube-router/
all: test kube-router container ## Default target. Runs tests, builds binaries and images.

kube-router: $(shell find . -name \*.go) ## Builds kube-router.
@echo Starting kube-router binary build.
CGO_ENABLED=0 go build -o kube-router kube-router.go
@echo Finished kube-router binary build.

test: gofmt ## Runs code quality pipelines (gofmt, tests, coverage, lint, etc)

run: kube-router ## Runs "kube-router --help".
./kube-router --help

container: kube-router ## Builds a Docker container image.
$(DOCKER) build -t "$(REGISTRY_DEV):$(IMG_PREFIX)$(IMG_TAG)" .

docker-login:
@if [ -z "$(NO_DOCKER_LOGIN)" ]; then \
@echo Starting kube-router container image build.
$(DOCKER) build -t "$(REGISTRY_DEV):$(IMG_TAG)" .
@echo Finished kube-router container image build.

docker-login: ## Logs into a docker registry using {DOCKER,QUAY}_{USERNAME,PASSWORD} variables.
@echo Starting docker login target.
@if [ -n "$(DOCKER_USERNAME)" ] && [ -n "$(DOCKER_PASSWORD)" ]; then \
echo Starting DockerHub registry login.; \
$(DOCKER) login -u="$(value DOCKER_USERNAME)" -p="$(value DOCKER_PASSWORD)"; \
echo Finished DockerHub registry login.; \
fi

@if [ -z "$(NO_QUAY_LOGIN)" ]; then \
@if [ -n "$(QUAY_USERNAME)" ] && [ -n "$(QUAY_PASSWORD)" ]; then \
echo Starting quay.io registry login.; \
$(DOCKER) login -u="$(value QUAY_USERNAME)" -p="$(value QUAY_PASSWORD)" quay.io; \
echo Finished quay.io registry login.; \
fi
@echo Finished docker login target.

push: container docker-login ## Pushes a Docker container image to a registry.
$(DOCKER) tag "$(REGISTRY_DEV):$(IMG_TAG)" "$(REGISTRY_DEV):$(GIT_BRANCH)-latest"
@echo Starting kube-router container image push.
$(DOCKER) push "$(REGISTRY_DEV)"
@echo Finished kube-router container image push.

push-release: push
@echo Starting kube-router release container image push.
@test -n "$(RELEASE_TAG)"
$(DOCKER) tag "$(REGISTRY_DEV):$(IMG_TAG)" "$(REGISTRY):$(RELEASE_TAG)"
$(DOCKER) tag "$(REGISTRY):$(RELEASE_TAG)" "$(REGISTRY):latest"
$(DOCKER) push "$(REGISTRY)"
@echo Finished kube-router release container image push.

github-release: kube-router
@echo Starting kube-router GitHub release creation.
@[ -n "$(value GITHUB_TOKEN)" ] && \
GITHUB_TOKEN=$(value GITHUB_TOKEN); \
curl -sL https://git.io/goreleaser | bash
@echo Finished kube-router GitHub release creation.

release: push-release github-release ## Pushes a release to DockerHub and GitHub
@echo Finished kube-router release target.

clean: ## Removes the kube-router binary and Docker images
rm -f kube-router
Expand Down
22 changes: 22 additions & 0 deletions build/travis-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail

# Pull Request image tag format: PR00-PRUSER
if [ "${TRAVIS_EVENT_TYPE}" = "pull_request" ]; then
PR_USER=$(echo "$TRAVIS_PULL_REQUEST_SLUG" | sed -e 's/\/.*//')
echo "Building/pushing PR$TRAVIS_PULL_REQUEST from $PR_USER"
make push IMG_TAG="PR$TRAVIS_PULL_REQUEST-$PR_USER"
exit 0
fi

# Release image tag format: v0.0.0 and latest
if [ -n "$TRAVIS_TAG" ]; then
echo "Running Release build on Travis"
make release RELEASE_TAG="$TRAVIS_TAG"
exit 0
fi

# Push image tag format: COMMIT
echo "Running push build on Travis"
make push
3 changes: 2 additions & 1 deletion build/verify-gofmt.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail
Expand All @@ -22,4 +21,6 @@ if [[ -n "${bad_files}" ]]; then
echo "or"
echo "Run \"${GOFMT} -w\" on each file."
exit 1
else
echo 'Everything is gofmt approved!'
fi

0 comments on commit db54caf

Please sign in to comment.