Skip to content

Commit

Permalink
enhancement(docker-promote): added setting an item property to the pr…
Browse files Browse the repository at this point in the history
…omoted image (#48)

* enhancement(docker-promote): added setting an item property to the promoted image

* enhance: added a flag to optionally set the promotion date

* refactor(docker-promote): changed flagging method to match bool pattern

* refactor(docker-promote): changed path formatting for promoted image and env var
  • Loading branch information
jacoblbeck authored Jul 1, 2020
1 parent cbae25e commit 5a90cab
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
19 changes: 17 additions & 2 deletions cmd/vela-artifactory/docker_promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"encoding/json"
"fmt"
"time"

"github.com/sirupsen/logrus"
goarty "github.com/target/go-arty/artifactory"
Expand All @@ -28,6 +29,8 @@ type DockerPromote struct {
TargetTags []string
// An optional value to set whether to copy instead of move. Default: true
Copy bool
// An optional value to set an item property to add a promoted date.
PromoteProperty bool
}

// Exec formats and runs the commands for uploading artifacts in Artifactory.
Expand Down Expand Up @@ -72,14 +75,26 @@ func (p *DockerPromote) Exec(c *Config) error {
}

for _, payload := range payloads {
logrus.Debugf("Promoting target tag %s", *payload.TargetTag)
logrus.Debugf("Promoting target tag %s", payload.GetTargetTag())

_, _, err := client.Docker.PromoteImage(p.TargetRepo, payload)
if err != nil {
return err
}

logrus.Infof("Promotion ended successfully for target tag %s", *payload.TargetTag)
if p.PromoteProperty {
promotedImagePath := fmt.Sprintf("%s/%s", payload.GetDockerRepository(), payload.GetTargetTag())

properties := make(map[string][]string)
properties["promoted_on"] = append(properties["promoted_on"], time.Now().UTC().Format(time.RFC3339))

_, err = client.Storage.SetItemProperties(payload.GetTargetRepo(), promotedImagePath, properties)
if err != nil {
return err
}
}

logrus.Infof("Promotion ended successfully for target tag %s", payload.GetTargetTag())
}

return nil
Expand Down
40 changes: 33 additions & 7 deletions cmd/vela-artifactory/docker_promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,41 @@ func TestArtifactory_DockerPromote_Exec(t *testing.T) {
Username: "octocat",
}

// setup types
p := &DockerPromote{
TargetRepo: "docker",
DockerRegistry: "github/octocat",
tests := []struct {
dockerPromote *DockerPromote
want *error
}{
{ // dockerPromote that does not have a promoteProperty
dockerPromote: &DockerPromote{
TargetRepo: "docker",
DockerRegistry: "github/octocat",
TargetTags: []string{"test"},
PromoteProperty: false,
},
want: nil,
},
//TODO: investigate ways of combining handlers
//
// Go-Arty API docs for handlers:
// "github.com/target/go-arty/artifactory/fixtures/docker"
// "github.com/target/go-arty/artifactory/fixtures/storage"
//
// { // dockerPromote that does have a promoteProperty
// dockerPromote: &DockerPromote{
// TargetRepo: "docker",
// DockerRegistry: "github/octocat",
// TargetTags: []string{"test"},
// PromoteProperty: true,
// },
// want: nil,
// },
}

err := p.Exec(config)
if err != nil {
t.Errorf("Exec should have returned err: %w", err)
for _, test := range tests {
err := test.dockerPromote.Exec(config)
if err != nil {
t.Errorf("Exec should have returned err: %w", err)
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions cmd/vela-artifactory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ func main() {
Name: "docker_promote.copy",
Usage: "set to copy instead of moving the image",
},
&cli.BoolFlag{
EnvVars: []string{"PARAMETER_PROPS_PROMOTE", "SET_PROP_PROMOTE"},
Name: "docker_prop.promote",
Usage: "propety to be set on the artifact when it is being promoted",
},

// Set Prop Flags

Expand Down Expand Up @@ -245,6 +250,7 @@ func run(c *cli.Context) error {
Tag: c.String("docker_promote.tag"),
TargetTags: c.StringSlice("docker_promote.target_tags"),
Copy: c.Bool("docker_promote.copy"),
PromoteProperty: c.Bool("docker_prop.promote"),
},
// set-prop configuration
SetProp: &SetProp{
Expand Down

0 comments on commit 5a90cab

Please sign in to comment.