Skip to content

Commit

Permalink
refactor: use Kaniko for builds (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrynhard authored Nov 30, 2018
1 parent 0fdd552 commit 8ced588
Show file tree
Hide file tree
Showing 32 changed files with 336 additions and 1,263 deletions.
98 changes: 0 additions & 98 deletions .conform.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
metadata:
repository: autonomy/conform
variables:
linuxBinaryPath: /conform-linux-amd64
darwinBinaryPath: /conform-darwin-amd64
gitRepository: github.com/autonomy/conform
maintainer: Andrew Rynhard <[email protected]>
policies:
- type: conventionalCommit
spec:
Expand All @@ -16,96 +9,5 @@ policies:
- style
- test
scopes:
- ci
- cli
- docker
- fmt
- git
- metadata
- pipeline
- policy
- readme
- renderer
- service
- '*'
script:
template: |
#!/bin/bash
set -e
{{ if and (.Git.IsClean) (or (.Git.IsTag) (eq .Git.Branch "master")) }}
docker login --username=$DOCKER_USERNAME --password=$DOCKER_PASSWORD
docker tag {{ .Docker.Image.Name }}:{{ .Docker.Image.Tag }} {{ .Docker.Image.Name }}:latest
docker push {{ .Docker.Image.Name }}:{{ .Docker.Image.Tag }}
docker push {{ .Docker.Image.Name }}:latest
{{ if .Git.IsTag }}
docker tag {{ .Docker.Image.Name }}:{{ .Docker.Image.Tag }} {{ .Docker.Image.Name }}:{{ .Version.Original }}
docker push {{ .Docker.Image.Name }}:{{ .Version.Original }}
{{ end }}
{{ end }}
pipeline:
stages:
- src
- test
- build
- image
stages:
build:
artifacts:
- source: /conform-linux-amd64
destination: ./build/conform-linux-amd64
- source: /conform-darwin-amd64
destination: ./build/conform-darwin-amd64
tasks:
- build
image:
tasks:
- image
src:
tasks:
- src
test:
artifacts:
- source: /src/github.com/autonomy/conform/coverage.txt
destination: coverage.txt
tasks:
- test
tasks:
build:
template: |
FROM autonomy/conform:src AS {{ .Docker.CurrentStage }}
{{ if and .Git.IsClean .Git.IsTag }}
RUN go build -o {{ index .Variables "linuxBinaryPath" }} -ldflags "-s -w -X \"{{ index .Variables "gitRepository" }}/cmd.Tag={{ trimAll "v" .Git.Tag }}\" -X \"{{ index .Variables "gitRepository" }}/cmd.SHA={{ .Git.SHA }}\" -X \"{{ index .Variables "gitRepository" }}/cmd.Built={{ .Built }}\""
RUN GOOS=darwin go build -o {{ index .Variables "darwinBinaryPath" }} -ldflags "-s -w -X \"{{ index .Variables "gitRepository" }}/cmd.Tag={{ trimAll "v" .Git.Tag }}\" -X \"{{ index .Variables "gitRepository" }}/cmd.SHA={{ .Git.SHA }}\" -X \"{{ index .Variables "gitRepository" }}/cmd.Built={{ .Built }}\""
{{ else if .Git.IsClean }}
RUN go build -o {{ index .Variables "linuxBinaryPath" }} -ldflags "-s -w -X \"{{ index .Variables "gitRepository" }}/cmd.SHA={{ .Git.SHA }}\" -X \"{{ index .Variables "gitRepository" }}/cmd.Built={{ .Built }}\""
RUN GOOS=darwin go build -o {{ index .Variables "darwinBinaryPath" }} -ldflags "-s -w -X \"{{ index .Variables "gitRepository" }}/cmd.SHA={{ .Git.SHA }}\" -X \"{{ index .Variables "gitRepository" }}/cmd.Built={{ .Built }}\""
{{ else }}
RUN go build -o {{ index .Variables "linuxBinaryPath" }}
RUN GOOS=darwin go build -o {{ index .Variables "darwinBinaryPath" }}
{{ end }}
image:
template: |
FROM alpine:3.8 AS {{ .Docker.CurrentStage }}
LABEL maintainer="{{ index .Variables "maintainer" }}"
RUN apk --no-cache add bash
COPY --from={{ .Repository}}:build {{ index .Variables "linuxBinaryPath" }} /bin/conform
ENTRYPOINT ["conform"]
src:
template: |
FROM golang:1.11.1 AS {{ .Docker.CurrentStage }}
ENV GO111MODULE on
ENV CGO_ENABLED 0
WORKDIR /src/{{ index .Variables "gitRepository" }}
COPY ./ ./
RUN go mod download
RUN go mod verify
test:
template: |
FROM autonomy/conform:src AS {{ .Docker.CurrentStage }}
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.10.1
RUN chmod +x ./hack/test.sh
RUN ./hack/test.sh --lint ./hack/golangci-lint.yaml
RUN ./hack/test.sh --unit
RUN ./hack/test.sh --coverage
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
conform-*
coverage.txt
build
cache
vendor
36 changes: 36 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
ARG GOLANG_IMAGE
FROM ${GOLANG_IMAGE}

ENV CGO_ENABLED 0
ENV GO111MODULES on

WORKDIR /conform
COPY ./ ./
RUN go mod download
RUN go mod verify
RUN go mod tidy
RUN go mod vendor

ARG TAG
ARG SHA
ARG BUILT
ENV GOOS linux
ENV GOARCH amd64
RUN go build -o /build/conform-${GOOS}-${GOARCH} -ldflags "-s -w -X \"github.com/autonomy/conform/cmd.Tag=${TAG}\" -X \"github.com/autonomy/conform/cmd.SHA=${SHA}\" -X \"github.com/autonomy/conform/cmd.Built=${BUILT}\"" .

ARG TAG
ARG SHA
ARG BUILT
ENV GOOS darwin
ENV GOARCH amd64
RUN go build -o /build/conform-${GOOS}-${GOARCH} -ldflags "-s -w -X \"github.com/autonomy/conform/cmd.Tag=${TAG}\" -X \"github.com/autonomy/conform/cmd.SHA=${SHA}\" -X \"github.com/autonomy/conform/cmd.Built=${BUILT}\"" .

ENV GOOS linux
ENV GOARCH amd64
COPY ./hack ./hack
RUN chmod +x ./hack/test.sh
RUN ./hack/test.sh --all

FROM scratch
COPY /build/conform-linux-amd64 /conform
ENTRYPOINT [ "/conform" ]
57 changes: 57 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
REPO ?= docker.io/autonomy
EXECUTOR ?= gcr.io/kaniko-project/executor
EXECUTOR_TAG ?= latest
WARMER ?= gcr.io/kaniko-project/warmer
WARMER_TAG ?= latest
GOLANG_IMAGE ?= golang:1.11.2
AUTH_CONFIG ?= $(HOME)/.kaniko/config.json

SHA := $(shell gitmeta git sha)
TAG := $(shell gitmeta image tag)
BUILT := $(shell gitmeta built)

EXECUTOR_ARGS := --context=/workspace --cache=true --cache-dir=/cache --cleanup
EXECUTOR_VOLUMES := --volume $(AUTH_CONFIG):/kaniko/.docker/config.json:ro --volume $(PWD)/cache:/cache --volume $(PWD)/build:/build

all: enforce clean conform

enforce:
conform enforce

conform: cache
docker run \
--rm \
$(EXECUTOR_VOLUMES) \
--volume $(PWD):/workspace \
$(EXECUTOR):$(EXECUTOR_TAG) \
$(EXECUTOR_ARGS) \
--dockerfile=Dockerfile \
--cache-repo=$(REPO)/$@ \
--destination=$(REPO)/$@:$(TAG) \
--single-snapshot \
--no-push \
--build-arg GOLANG_IMAGE=$(GOLANG_IMAGE) \
--build-arg SHA=$(SHA) \
--build-arg TAG=$(TAG) \
--build-arg BUILT="$(BUILT)"

.PHONY: cache
cache:
docker run \
--rm \
$(EXECUTOR_VOLUMES) \
$(WARMER):$(WARMER_TAG) \
--cache-dir=/cache \
--image=$(GOLANG_IMAGE)

debug:
docker run \
--rm \
-it \
$(EXECUTOR_VOLUMES) \
--volume $(PWD):/workspace \
--entrypoint=/busybox/sh \
$(EXECUTOR):debug

clean:
rm -rf ./build
43 changes: 10 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,29 @@

---

**Conform** is a tool for building projects in a flexible and reliabale manner.
**Conform** is a tool for building enforcing policies on your build pipelines.

The key features of Conform are:
- **DRY**: Templatized multi-stage Docker builds.
- **Hygienic**: Builds run in Docker.
- **Fast**: Leverages Docker caching, building only what has changed.
Some of the policies included are:

- **Convetion Commits**: Enforce [conventional commits](https://www.conventionalcommits.org) for all commit messages.

## Getting Started

Getting Started
---------------
Create a file named `.conform.yaml` with the following contents:
```yaml
metadata:
repository: hello/world

policies:
```yaml:
- type: conventionalCommit
spec:
types:
- "type"
scopes:
- "scope"

script:
template: |
#!/bin/bash
echo "Hello, world!"
pipeline:
stages:
- example

stages:
example:
tasks:
- task

tasks:
task:
template: |
FROM scratch
```

In the same directory, run:
```
$ conform build

```bash
conform enforce
```

### License
Expand Down
Empty file removed build/.keep
Empty file.
89 changes: 0 additions & 89 deletions cmd/build.go

This file was deleted.

Loading

0 comments on commit 8ced588

Please sign in to comment.