diff --git a/.conform.yaml b/.conform.yaml index 708f32d4..89bd1595 100644 --- a/.conform.yaml +++ b/.conform.yaml @@ -1,5 +1,9 @@ metadata: repository: autonomy/conform + variables: + binaryPath: /conform + gitRepository: github.com/autonomy/conform + maintainer: Andrew Rynhard policies: - type: conventionalCommit @@ -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 " + 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"] diff --git a/.travis.yml b/.travis.yml index 7783137b..5ef7668a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Gopkg.lock b/Gopkg.lock index a445026b..e0b93d0e 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -251,6 +251,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "83275d38575279e8fa036d1ab31dc3016f68a856b417a399e6217adde4d0b57f" + inputs-digest = "f61681c793671cbaaf967addf12f3b2478c74a48bc0c0a44b7fcb0d2be54be64" solver-name = "gps-cdcl" solver-version = 1 diff --git a/cmd/enforce.go b/cmd/enforce.go index e6a36d97..211ed6df 100644 --- a/cmd/enforce.go +++ b/cmd/enforce.go @@ -24,6 +24,7 @@ import ( var ( skipArray []string + varArray []string ) // enforceCmd represents the enforce command @@ -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 { @@ -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") } diff --git a/pkg/metadata/metadata.go b/pkg/metadata/metadata.go index 257fbd77..0e40154f 100644 --- a/pkg/metadata/metadata.go +++ b/pkg/metadata/metadata.go @@ -1,6 +1,7 @@ package metadata import ( + "strings" "time" "github.com/Masterminds/semver" @@ -14,6 +15,7 @@ type Metadata struct { Docker *Docker Git *Git Version *Version + Variables VariablesMap `yaml:"variables"` Built string } @@ -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 { @@ -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 {