Skip to content

Commit

Permalink
feat(metadata): allow users to specify variables
Browse files Browse the repository at this point in the history
  • Loading branch information
test committed Jan 13, 2018
1 parent 4b7b903 commit 59cc0b7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
22 changes: 15 additions & 7 deletions .conform.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
metadata:
repository: autonomy/conform
variables:
binaryPath: /conform
gitRepository: github.com/autonomy/conform
maintainer: Andrew Rynhard <andrew.rynhard@autonomy.io>

policies:
- type: conventionalCommit
Expand Down Expand Up @@ -48,23 +52,27 @@ tasks:
binary:
template: |
FROM autonomy/golang:1.9 as {{ .Docker.CurrentStage }}
WORKDIR $GOPATH/src/github.com/{{ .Repository }}
WORKDIR $GOPATH/src/{{ index .Variables "gitRepository" }}
COPY ./ ./
{{ if and .Git.IsClean .Git.IsTag }}
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /conform -ldflags "-X \"github.com/{{ .Repository }}/cmd.Tag={{ trimAll "v" .Git.Tag }}\" -X \"github.com/{{ .Repository }}/cmd.SHA={{ .Git.SHA }}\" -X \"github.com/{{ .Repository }}/cmd.Built={{ .Built }}\""
RUN go build -o {{ index .Variables "binaryPath" }} -ldflags "-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 GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /conform -ldflags "-X \"github.com/{{ .Repository }}/cmd.SHA={{ .Git.SHA }}\" -X \"github.com/{{ .Repository }}/cmd.Built={{ .Built }}\""
RUN go build -o {{ index .Variables "binaryPath" }} -ldflags "-X \"{{ index .Variables "gitRepository" }}/cmd.SHA={{ .Git.SHA }}\" -X \"{{ index .Variables "gitRepository" }}/cmd.Built={{ .Built }}\""
{{ else }}
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /conform
RUN go build -o {{ index .Variables "binaryPath" }}
{{ end }}
test:
template: |
{{ fromURL "https://raw.githubusercontent.com/autonomy/conform-templates/master/golang/Dockerfile.test" }}
FROM autonomy/golang:1.9 as {{ .Docker.CurrentStage }}
WORKDIR $GOPATH/src/{{ index .Variables "gitRepository" }}
COPY ./ ./
RUN test.sh
image:
template: |
FROM alpine:3.6 as {{ .Docker.CurrentStage }}
LABEL maintainer="Andrew Rynhard <andrew.rynhard@autonomy.io>"
LABEL maintainer="{{ index .Variables "maintainer" }}"
RUN apk --update add bash \
&& rm -rf /var/cache/apk/*
COPY --from=binary /conform /bin
COPY --from=binary {{ index .Variables "binaryPath" }} /bin
ENTRYPOINT ["conform"]
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ language: generic

install:
- mkdir -p $HOME/goroot
- curl -L https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz | tar -xz --strip-components=1 -C $HOME/goroot
- curl -L https://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz | tar -xz --strip-components=1 -C $HOME/goroot
- export GOROOT=$HOME/goroot
- export GOPATH=$HOME/gopath
- export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
Expand Down
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion cmd/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

var (
skipArray []string
varArray []string
)

// enforceCmd represents the enforce command
Expand All @@ -47,6 +48,8 @@ var enforceCmd = &cobra.Command{
fmt.Println(err)
os.Exit(1)
}

e.Metadata.AddMetadataForVariables(varArray)
for _, skip := range skipArray {
for i, stage := range e.Pipeline.Stages {
if stage == skip {
Expand All @@ -63,5 +66,6 @@ var enforceCmd = &cobra.Command{

func init() {
RootCmd.AddCommand(enforceCmd)
enforceCmd.Flags().StringArrayVarP(&skipArray, "skip", "s", []string{}, "skip a stage in the pipeline")
enforceCmd.Flags().StringArrayVar(&skipArray, "skip", []string{}, "skip a stage in the pipeline")
enforceCmd.Flags().StringArrayVar(&varArray, "var", []string{}, "set a variable")
}
19 changes: 18 additions & 1 deletion pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metadata

import (
"strings"
"time"

"github.com/Masterminds/semver"
Expand All @@ -14,6 +15,7 @@ type Metadata struct {
Docker *Docker
Git *Git
Version *Version
Variables VariablesMap `yaml:"variables"`
Built string
}

Expand Down Expand Up @@ -45,16 +47,21 @@ type Version struct {
IsPrerelease bool
}

// VariablesMap is a map for user defined metadata.
type VariablesMap = map[string]interface{}

// UnmarshalYAML implements the yaml.UnmarshalYAML interface.
func (m *Metadata) UnmarshalYAML(unmarshal func(interface{}) error) error {
var aux struct {
Repository string `yaml:"repository"`
Repository string `yaml:"repository"`
Variables VariablesMap `yaml:"variables"`
}
if err := unmarshal(&aux); err != nil {
return err
}

m.Repository = aux.Repository
m.Variables = aux.Variables
m.Built = time.Now().UTC().Format(time.RFC1123)

if err := addMetadataForGit(m); err != nil {
Expand All @@ -70,6 +77,16 @@ func (m *Metadata) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}

func (m *Metadata) AddMetadataForVariables(v []string) {
for _, variable := range v {
s := strings.Split(variable, "=")
if len(s) != 2 {
panic("interface{}")
}
m.Variables[s[0]] = s[1]
}
}

func addMetadataForVersion(m *Metadata) error {
m.Version = &Version{}
if m.Git.IsTag {
Expand Down

0 comments on commit 59cc0b7

Please sign in to comment.