Skip to content

Commit

Permalink
refactor build tooling
Browse files Browse the repository at this point in the history
- adds required `GCLOUD_SDK_VERSION` `--build-arg` to `Dockerfile`
- removes `Dockerfile.1.10`, `Dockerfile.1.11` (can now be customized via `GCLOUD_SDK_VERSION` build arg)
- removes `bin/dist`; replaced with `make`-based implementation
- adds several misc config / rc files
- updates `README`, `DOCS.md` to reflect changes, address linting
  • Loading branch information
montmanu committed Jul 18, 2019
1 parent 2c9c772 commit 59cdf82
Show file tree
Hide file tree
Showing 12 changed files with 371 additions and 114 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.git
.github
.vscode
local-example

15 changes: 11 additions & 4 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ build_config: &build_config
image: golang:1
environment:
- GO111MODULE=on
- GOPROXY=https://proxy.golang.org
- GOPROXY=https://proxy.golang.org
- CGO_ENABLED=0
commands:
- go build -a -ldflags "-X main.version=${DRONE_TAG:-other} -X main.rev=${DRONE_COMMIT}"

publish_config: &publish_config
image: plugins/docker
repo: nytimes/drone-gke
dockerfile: Dockerfile
secrets:
- docker_username
- docker_password
Expand All @@ -24,7 +25,7 @@ pipeline:
image: golang:1
environment:
- GO111MODULE=on
- GOPROXY=https://proxy.golang.org
- GOPROXY=https://proxy.golang.org
- CGO_ENABLED=0
commands:
- go mod download
Expand All @@ -45,6 +46,8 @@ pipeline:

publish_develop:
<<: *publish_config
build_args:
- CLOUD_SDK_VERSION=alpine
tag:
- "develop"
when:
Expand All @@ -53,19 +56,23 @@ pipeline:

publish_latest:
<<: *publish_config
build_args:
- CLOUD_SDK_VERSION=alpine
auto_tag: true

publish_1_10:
<<: *publish_config
build_args:
- CLOUD_SDK_VERSION=217.0.0
auto_tag: true
auto_tag_suffix: "k8s-1.10"
dockerfile: Dockerfile.1.10

publish_1_11:
<<: *publish_config
build_args:
- CLOUD_SDK_VERSION=241.0.0
auto_tag: true
auto_tag_suffix: "k8s-1.11"
dockerfile: Dockerfile.1.11

slack:
image: plugins/slack
Expand Down
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[*.go]
indent_style = tab

[{go.mod,go.sum}]
indent_style = tab

[Makefile]
indent_style = tab
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/drone-gke
.vscode
24 changes: 12 additions & 12 deletions DOCS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# drone-gke Usage

Use this plugin to deploy Docker images to [Google Container Engine (GKE)][gke].

[gke]: https://cloud.google.com/container-engine/
Expand Down Expand Up @@ -47,7 +49,7 @@ Simply copy the contents of the JSON credentials file and paste it directly in t

#### CLI

```
```sh
drone secret add \
--event push \
--event pull_request \
Expand Down Expand Up @@ -78,19 +80,17 @@ Kubernetes expects secrets to be base64 encoded, `drone-gke` does that for you.

These variables are always available to reference in any manifest, and cannot be overwritten by `vars` or `secrets`:

```
---START VARIABLES AVAILABLE FOR ALL TEMPLATES---
```json
{
"BRANCH": "master",
"BUILD_NUMBER": "12",
"COMMIT": "4923x0c3380413ec9288e3c0bfbf534b0f18fed1",
"TAG": "",
"cluster": "my-gke-cluster",
"namespace": "",
"project": "my-gcp-proj",
"zone": "us-east1-a"
"BRANCH": "master",
"BUILD_NUMBER": "12",
"COMMIT": "4923x0c3380413ec9288e3c0bfbf534b0f18fed1",
"TAG": "",
"cluster": "my-gke-cluster",
"namespace": "",
"project": "my-gcp-proj",
"zone": "us-east1-a"
}
---END VARIABLES AVAILABLE FOR ALL TEMPLATES---
```

## Expanding environment variables
Expand Down
20 changes: 9 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# latest kubectl in the SDK
# https://cloud.google.com/sdk/docs/release-notes
FROM google/cloud-sdk:alpine

# Install kubectl
RUN gcloud components install kubectl && \
rm -rf ./google-cloud-sdk/.install
# see https://hub.docker.com/r/google/cloud-sdk/tags for available tags / versions
ARG GCLOUD_SDK_VERSION

FROM google/cloud-sdk:${GCLOUD_SDK_VERSION} AS cloud-sdk
ENV CLOUDSDK_CONTAINER_USE_APPLICATION_DEFAULT_CREDENTIALS=true
ENV CLOUDSDK_CORE_DISABLE_PROMPTS=1
ADD bin/install-kubectl /usr/local/bin/
RUN install-kubectl && rm -f /usr/local/bin/install-kubectl

# Add the Drone plugin
ADD drone-gke /bin/

ENTRYPOINT ["/bin/drone-gke"]
FROM cloud-sdk
ADD drone-gke /usr/local/bin/
ENTRYPOINT ["drone-gke"]
14 changes: 0 additions & 14 deletions Dockerfile.1.10

This file was deleted.

14 changes: 0 additions & 14 deletions Dockerfile.1.11

This file was deleted.

191 changes: 191 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# executables
docker := $(shell which docker) --log-level error
gcloud := $(shell which gcloud) --quiet --verbosity=error
git := $(shell which git)
go := $(shell which go)
kubectl := $(shell which kubectl)

# plugin's binary options
binary_name = drone-gke

# git options
git_default_branch = $(shell cat $(CURDIR)/.git/refs/remotes/origin/HEAD | cut -d ' ' -f 2 | cut -d '/' -f 4)
git_current_branch = $(shell $(git) rev-parse --abbrev-ref HEAD)
git_current_revision = $(shell $(git) rev-parse --short HEAD)

# plugin's docker options
docker_default_repo_name = nytimes
docker_image_name = drone-gke
docker_default_tag = latest
ifneq ($(git_current_branch), $(git_default_branch))
docker_default_tag = $(git_current_branch)
endif
docker_default_file = $(CURDIR)/Dockerfile
docker_default_build_context = $(CURDIR)

# gcloud SDK options
gcloud_sdk_image_name = google/cloud-sdk
gcloud_default_sdk_version = alpine

# print usage (default target)
usage :
@echo "Usage:"
@echo ""
@echo " \$$ make check # run tests for required software"
@echo " \$$ make clean # delete plugin binary, docker image"
@echo " \$$ make dist # build and push plugin docker image"
@echo " \$$ make docker-image # build plugin docker image"
@echo " \$$ make docker-release # push plugin docker image"
@echo " \$$ make docker-run-local # run docker container using local-example"
@echo " \$$ make $(binary_name) # build plugin binary"
@exit 0

.PHONY : usage

# check that docker is installed
check-docker :
ifneq (0,$(shell command -v $(docker) 2>&1 >/dev/null ; echo $$?))
$(error "docker not installed")
else
@exit 0
endif

.PHONY : check-docker

# check that gcloud is installed
check-gcloud :
ifneq (0,$(shell command -v $(gcloud) 2>&1 >/dev/null ; echo $$?))
$(error "gcloud not installed")
else
@exit 0
endif

.PHONY : check-gcloud

# check that git is installed
check-git :
ifneq (0,$(shell command -v $(git) 2>&1 >/dev/null ; echo $$?))
$(error "git not installed")
else
@exit 0
endif

.PHONY : check-git

# check that go is installed
check-go :
ifneq (0,$(shell command -v $(go) 2>&1 >/dev/null ; echo $$?))
$(error "go not installed")
else
@exit 0
endif

.PHONY : check-go

# check that kubectl is installed
check-kubectl :
ifneq (0,$(shell command -v $(kubectl) 2>&1 >/dev/null ; echo $$?))
$(error "kubectl not installed")
else
@exit 0
endif

.PHONY : check-kubectl

# check that all required executables are installed
check : check-docker check-gcloud check-git check-go check-kubectl

.PHONY : check

# clean configuration
clean : export docker_repo_name ?= $(docker_default_repo_name)
clean : export docker_tag ?= $(docker_default_tag)

# delete binary, docker image
clean :
@rm -f $(binary_name)
@$(docker) rmi --force $(docker_repo_name)/$(docker_image_name):$(docker_tag) 2>/dev/null

.PHONY : clean

# compilation configuration
$(binary_name) : export CGO_ENABLED ?= 0
$(binary_name) : export GO111MODULE ?= on
$(binary_name) : export GOARCH ?= amd64
$(binary_name) : export GOOS ?= linux
$(binary_name) : export GOPROXY ?= https://proxy.golang.org
$(binary_name) : export revision ?= $(git_current_revision)

# compile binary
$(binary_name) : main.go exec.go go.sum
@$(go) build -a -ldflags "-X main.rev=$(revision)"

# docker build configuration
docker-image : export docker_build_context ?= $(docker_default_build_context)
docker-image : export docker_file ?= $(docker_default_file)
docker-image : export docker_repo_name ?= $(docker_default_repo_name)
docker-image : export docker_tag ?= $(docker_default_tag)
docker-image : export gcloud_sdk_version ?= $(gcloud_default_sdk_version)

# build docker image
docker-image : $(binary_name) $(docker_file)
@$(docker) build \
--build-arg GCLOUD_SDK_VERSION=$(gcloud_sdk_version) \
--tag $(docker_repo_name)/$(docker_image_name):$(docker_tag) \
--file $(docker_file) $(docker_build_context)

.PHONY : docker-image

# docker push configuration
docker-release : export docker_repo_name ?= $(docker_default_repo_name)
docker-release : export docker_tag ?= $(docker_default_tag)

# push docker image
docker-release :
@$(docker) push $(docker_repo_name)/$(docker_image_name):$(docker_tag)

.PHONY : docker-release

# compile binary, build and push docker image
dist : docker-image docker-release

# local docker run configuration
docker-run-local : export CONFIG_HOME ?= $(CURDIR)/local-example
docker-run-local : export GOOGLE_APPLICATION_CREDENTIALS ?= xxx
docker-run-local : export PLUGIN_CLUSTER ?= yyy
docker-run-local : export PLUGIN_NAMESPACE ?= drone-gke
docker-run-local : export PLUGIN_SECRET_TEMPLATE ?= $(CONFIG_HOME)/.kube.sec.yml
docker-run-local : export PLUGIN_TEMPLATE ?= $(CONFIG_HOME)/.kube.yml
docker-run-local : export PLUGIN_VARS_PATH ?= $(CONFIG_HOME)/vars.json
docker-run-local : export PLUGIN_ZONE ?= zzz
docker-run-local : export SECRET_API_TOKEN ?= 123
docker-run-local : export SECRET_BASE64_P12_CERT ?= "cDEyCg=="
docker-run-local : export docker_repo_name ?= $(docker_default_repo_name)
docker-run-local : export docker_tag ?= $(docker_default_tag)
docker-run-local : export docker_cmd ?= --dry-run --verbose

# run docker container using local-example
docker-run-local :
@[ -d $(CONFIG_HOME) ] || ( echo "$(CONFIG_HOME) does not exist" ; exit 1 )
@[ -f $(PLUGIN_SECRET_TEMPLATE) ] || ( echo "$(PLUGIN_SECRET_TEMPLATE) does not exist" ; exit 1 )
@[ -f $(PLUGIN_TEMPLATE) ] || ( echo "$(PLUGIN_TEMPLATE) does not exist" ; exit 1 )
@[ -f $(PLUGIN_VARS_PATH) ] || ( echo "$(PLUGIN_VARS_PATH) does not exist" ; exit 1 )
@[ -f $(GOOGLE_APPLICATION_CREDENTIALS) ] || ( echo "$(GOOGLE_APPLICATION_CREDENTIALS) does not exist" ; exit 1 )
@cd $(CONFIG_HOME) ; \
$(docker) run \
--env PLUGIN_CLUSTER=$(PLUGIN_CLUSTER) \
--env PLUGIN_NAMESPACE=$(PLUGIN_NAMESPACE) \
--env PLUGIN_SECRET_TEMPLATE=$(PLUGIN_SECRET_TEMPLATE) \
--env PLUGIN_TEMPLATE=$(PLUGIN_TEMPLATE) \
--env PLUGIN_VARS="$$(cat $(PLUGIN_VARS_PATH))" \
--env PLUGIN_ZONE=$(PLUGIN_ZONE) \
--env SECRET_API_TOKEN=$(SECRET_API_TOKEN) \
--env SECRET_BASE64_P12_CERT=$(SECRET_BASE64_P12_CERT) \
--env TOKEN="$$(cat $(GOOGLE_APPLICATION_CREDENTIALS))" \
--volume $(CONFIG_HOME):$(CONFIG_HOME) \
--workdir $(CONFIG_HOME) \
$(docker_repo_name)/$(docker_image_name):$(docker_tag) $(docker_cmd)

.PHONY : docker-run-local

.PHONY : dist
Loading

0 comments on commit 59cdf82

Please sign in to comment.