Skip to content

Commit

Permalink
feat(metadata): allow users to specify variables (siderolabs#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrynhard authored Jan 13, 2018
1 parent 4b7b903 commit 72061b1
Show file tree
Hide file tree
Showing 5 changed files with 35 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.

12 changes: 11 additions & 1 deletion cmd/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/autonomy/conform/pkg/enforcer"
"github.com/autonomy/conform/pkg/utilities"
Expand All @@ -24,6 +25,7 @@ import (

var (
skipArray []string
varArray []string
)

// enforceCmd represents the enforce command
Expand All @@ -47,6 +49,13 @@ var enforceCmd = &cobra.Command{
fmt.Println(err)
os.Exit(1)
}
for _, variable := range varArray {
s := strings.Split(variable, "=")
if len(s) != 2 {
panic("interface{}")
}
e.Metadata.Variables[s[0]] = s[1]
}
for _, skip := range skipArray {
for i, stage := range e.Pipeline.Stages {
if stage == skip {
Expand All @@ -63,5 +72,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")
}
8 changes: 7 additions & 1 deletion pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Metadata struct {
Docker *Docker
Git *Git
Version *Version
Variables VariablesMap `yaml:"variables"`
Built string
}

Expand Down Expand Up @@ -45,16 +46,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 Down

0 comments on commit 72061b1

Please sign in to comment.