Skip to content

Commit

Permalink
Fix bad tag detection and setup CI (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrynhard authored Jun 5, 2017
1 parent 0c55035 commit 426abe1
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 85 deletions.
17 changes: 7 additions & 10 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
.git
.gitignore
conform.yaml
coverage.txt
Dockerfile
docs
examples
LICENSE
Makefile
README.md
*
!cmd
!conform
!Gopkg*
!main.go
!scripts
!vendor
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
coverage.txt
Dockerfile
docs/build
vendor
32 changes: 32 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
sudo: required
dist: trusty

language: go

go:
- 1.8.3

before_install:
- sudo apt-get -y remove docker docker-engine
- sudo apt-get -y update
- sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) edge"
- sudo apt-get update
- sudo apt-get -y install docker-ce
- go get -u github.com/autonomy/conform

script:
- conform enforce test
- conform enforce image

after_success:
- bash <(curl -s https://codecov.io/bash)

deploy:
provider: script
script: scripts/deploy.sh
skip_cleanup: true
on:
tags: true
branch: master
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,42 @@ scripts:
set -e
echo "Hello, world!"
echo "Initialize any dependencies here."
deploy : |
#!/bin/bash
set -e
echo "Deploy you image here."
templates:
build: |
FROM alpine:latest as build
RUN echo "Run your build here!"
RUN echo "Run your build here."
RUN touch artifact
test: |
FROM alpine:latest as test
COPY --from=build artifact .
RUN echo "Run your tests here!"
RUN echo "Run your tests here."
image: |
FROM scratch as image
RUN echo "Deply your image here!"
RUN echo "Prepare your final image here."
COPY --from=build artifact .
rules:
all:
image:
before:
- init
templates:
- build
- test
- image

after:
- deploy
```
In the same directory, run:
```
$ conform enforce all
$ conform enforce image
```
> **Note:** Conform is still under design. The YAML layout is subject to change.

Devloping Conform
----------------
Expand Down
8 changes: 5 additions & 3 deletions cmd/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ var enforceCmd = &cobra.Command{
if err != nil {
return err
}

dp := conform.NewExecuter(args[0])
err = dp.ExecuteRule()
e, err := conform.NewEnforcer(args[0])
if err != nil {
return err
}
err = e.ExecuteRule()
if err != nil {
return err
}
Expand Down
27 changes: 13 additions & 14 deletions conform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,28 @@ scripts:
BUILDDEPS=( github.com/golang/dep/cmd/dep )
for b in ${BUILDDEPS[@]}; do
echo "Installing $b"
go get -u $b
go get $b
done
test_artifacts: |
cp_coverage_report: |
#!/bin/bash
set -e
docker run --rm -i --volume $(pwd):/out ${CONFORM_IMAGE} cp coverage.txt /out
if [ ! -f coverage.txt ]; then
echo "No coverage report found."
exit 1
fi
deploy: |
#!/bin/bash
set -e
if [ ${CONFORM_IS_DIRTY} == "true" ]; then
echo "The working tree is dirty, aborting ..."
echo "The working tree is dirty."
exit 1
fi
Expand All @@ -45,6 +50,7 @@ scripts:
fi
clean: |
cat .gitignore | while read line; do rm -rf "$line"; done
dep ensure
dep prune
Expand All @@ -56,7 +62,6 @@ templates:
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /conform -a -ldflags "-X \"github.com/autonomy/conform/version.Tag={{ trimAll "v" .GitInfo.Tag }}\" -X \"github.com/autonomy/conform/version.SHA={{ .GitInfo.SHA }}\" -X \"github.com/autonomy/conform/version.Built={{ .Built }}\""
test: |
FROM golang:1.8.3 as test
MAINTAINER Andrew Rynhard <[email protected]>
WORKDIR /go/src/github.com/autonomy/conform
RUN go get -u github.com/golang/lint/golint
COPY --from=build /go/src/github.com/autonomy/conform .
Expand All @@ -76,26 +81,20 @@ rules:
- build

test:
before:
- init
- clean
templates:
- build
- test
after:
- test_artifacts
- cp_coverage_report

image:
templates:
- build
- image

all:
before:
- init
- clean
templates:
- build
- test
- image

deploy:
templates:
- build
Expand Down
8 changes: 4 additions & 4 deletions conform/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ type Rule struct {
}

// NewConfig instantiates and returns a config.
func NewConfig() *Config {
func NewConfig() (*Config, error) {
rBytes, err := ioutil.ReadFile("conform.yaml")
if err != nil {
fmt.Printf("Unable to load conform.yaml: %v", err)
return nil, fmt.Errorf("Unable to load conform.yaml: %v", err)
}
c := Config{}
err = yaml.Unmarshal(rBytes, &c)
if err != nil {
fmt.Printf("Unable to load conform.yaml: %v", err)
return nil, fmt.Errorf("Unable to load conform.yaml: %v", err)
}

return &c
return &c, nil
}
39 changes: 21 additions & 18 deletions conform/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ import (
"github.com/autonomy/conform/conform/git"
)

// Executer performs all the build actions for a rule.
type Executer struct {
// Enforcer performs all the build actions for a rule.
type Enforcer struct {
config *config.Config
rule string
GitInfo *git.Info
Built string
}

// NewExecuter instantiates and returns an executer.
func NewExecuter(rule string) *Executer {
e := &Executer{}
// NewEnforcer instantiates and returns an executer.
func NewEnforcer(rule string) (*Enforcer, error) {
e := &Enforcer{}
gitInfo := git.NewInfo()
date := []byte{}
if gitInfo.IsTag {
Expand All @@ -37,17 +37,20 @@ func NewExecuter(rule string) *Executer {
date = _date
}

c := config.NewConfig()
c, err := config.NewConfig()
if err != nil {
return nil, err
}
e.config = c
e.GitInfo = gitInfo
e.Built = strings.TrimSuffix(string(date), "\n")
e.rule = rule

return e
return e, nil
}

// ExecuteBuild executes a docker build.
func (e *Executer) ExecuteBuild() error {
func (e *Enforcer) ExecuteBuild() error {
image := e.FormatImageNameSHA()
if e.GitInfo.IsDirty {
image = e.FormatImageNameDirty()
Expand All @@ -69,7 +72,7 @@ func (e *Executer) ExecuteBuild() error {
}

// RenderDockerfile writes the final Dockerfile to disk.
func (e *Executer) RenderDockerfile(target *config.Rule) error {
func (e *Enforcer) RenderDockerfile(target *config.Rule) error {
dockerfile := `# THIS FILE IS AUTOGENERATED BY CONFORM. DO NOT EDIT!
# Note: This file should be ignored by git.
`
Expand All @@ -91,7 +94,7 @@ func (e *Executer) RenderDockerfile(target *config.Rule) error {
}

// RenderTemplate executes the template and returns it.
func (e *Executer) RenderTemplate(s string) (*string, error) {
func (e *Enforcer) RenderTemplate(s string) (*string, error) {
if _s, ok := e.config.Templates[s]; ok {
var wr bytes.Buffer
tmpl, err := template.New("").Funcs(sprig.TxtFuncMap()).Parse(_s)
Expand All @@ -112,12 +115,12 @@ func (e *Executer) RenderTemplate(s string) (*string, error) {
}

// ExtractArtifact copies an artifact from a build.
func (e *Executer) ExtractArtifact(artifact string) error {
func (e *Enforcer) ExtractArtifact(artifact string) error {
return fmt.Errorf("Artifact %q is not defined in conform.yaml", artifact)
}

// ExecuteScript executes a script for a rule.
func (e *Executer) ExecuteScript(script string) error {
func (e *Enforcer) ExecuteScript(script string) error {
if s, ok := e.config.Scripts[script]; ok {
log.Printf("Running %s script", script)
command := exec.Command("bash", "-c", s)
Expand All @@ -136,9 +139,9 @@ func (e *Executer) ExecuteScript(script string) error {
}

// ExecuteRule performs all the relevant actions specified in its' declaration.
func (e *Executer) ExecuteRule() error {
func (e *Enforcer) ExecuteRule() error {
if t, ok := e.config.Rules[e.rule]; ok {
log.Printf("Executing %q", e.rule)
log.Printf("Enforcing %q", e.rule)
for _, s := range t.Before {
err := e.ExecuteScript(s)
if err != nil {
Expand Down Expand Up @@ -167,21 +170,21 @@ func (e *Executer) ExecuteRule() error {
}

// FormatImageNameDirty formats the image name.
func (e *Executer) FormatImageNameDirty() string {
func (e *Enforcer) FormatImageNameDirty() string {
return fmt.Sprintf("%s:%s", *e.config.Metadata.Repository, "dirty")
}

// FormatImageNameSHA formats the image name.
func (e *Executer) FormatImageNameSHA() string {
func (e *Enforcer) FormatImageNameSHA() string {
return fmt.Sprintf("%s:%s", *e.config.Metadata.Repository, e.GitInfo.SHA)
}

// FormatImageNameTag formats the image name.
func (e *Executer) FormatImageNameTag() string {
func (e *Enforcer) FormatImageNameTag() string {
return fmt.Sprintf("%s:%s", *e.config.Metadata.Repository, e.GitInfo.Tag)
}

// FormatImageNameLatest formats the image name.
func (e *Executer) FormatImageNameLatest() string {
func (e *Enforcer) FormatImageNameLatest() string {
return fmt.Sprintf("%s:%s", *e.config.Metadata.Repository, "latest")
}
Loading

0 comments on commit 426abe1

Please sign in to comment.