Skip to content
This repository was archived by the owner on Jun 22, 2021. It is now read-only.

Commit 4e90eff

Browse files
authored
Merge pull request #5 from docker/add-opencontainers-labels
add_git_labels adds opencontainers labels
2 parents 8f2fb76 + 5f96875 commit 4e90eff

File tree

10 files changed

+66
-85
lines changed

10 files changed

+66
-85
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ See the tagging section for information on tag inputs
3838

3939
##### Git labels
4040

41-
When `INPUT_ADD_GIT_LABELS` is `true` labels are automatically added to the image that contain data about the current state of the git repo:
41+
When `INPUT_ADD_GIT_LABELS` is `true` labels are automatically added to the image that contain data about the current state of the git repo based on the standards set out in https://github.com/opencontainers/image-spec/blob/master/annotations.md.
42+
43+
3 labels are supported:
4244

4345
|Label|Description|
4446
|---|---|
45-
|com.docker.github-actions-actor|The username of the user that kicked off this run of the actions (e.g. the user that did the git push)|
46-
|com.docker.github-actions-sha|The full git sha of this commit|
47+
|org.opencontainers.image.created|Date and time on which the image was built (string, date-time as defined by RFC 3339).|
48+
|org.opencontainers.image.source|URL to this repository. E.g. `https://github.com/myorg/myrepository`|
49+
|org.opencontainers.image.revision|The full git sha of this commit.|
4750

4851
### push
4952

e2e/build_push_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ func TestBuildPush(t *testing.T) {
1414
"localhost:5000/my-repository:build-push-test",
1515
}
1616
labels := map[string]string{
17-
"a": "a1",
18-
"com.docker.github-actions-actor": "actor",
19-
"com.docker.github-actions-sha": "sha",
17+
"a": "a1",
2018
}
2119
err := removeImages(tags)
2220
assert.NilError(t, err)

e2e/build_test.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package e2e
33
import (
44
"os/exec"
55
"testing"
6+
"time"
67

78
"github.com/hashicorp/go-multierror"
89
"gotest.tools/v3/assert"
@@ -34,18 +35,6 @@ func TestBuild(t *testing.T) {
3435
"b": "b1",
3536
},
3637
},
37-
{
38-
name: "auto-labels",
39-
envFile: "testdata/build_tests/auto_labels.env",
40-
expectedTags: []string{
41-
"localhost:5000/my-repository:auto-labels",
42-
},
43-
expectedLabels: map[string]string{
44-
"a": "a1",
45-
"com.docker.github-actions-actor": "actor",
46-
"com.docker.github-actions-sha": "sha",
47-
},
48-
},
4938
{
5039
name: "auto-tags-master",
5140
envFile: "testdata/build_tests/tag_master.env",
@@ -95,6 +84,26 @@ func TestBuild(t *testing.T) {
9584
}
9685
}
9786

87+
func TestBuildWithGitLabels(t *testing.T) {
88+
tags := []string{"localhost:5000/my-repository:auto-labels"}
89+
err := runActionsCommand("build", "testdata/build_tests/auto_labels.env")
90+
assert.NilError(t, err)
91+
defer removeImages(tags)
92+
93+
inspect, err := inspectImage(tags[0])
94+
assert.NilError(t, err)
95+
assert.DeepEqual(t, tags, inspect.RepoTags)
96+
97+
assert.Equal(t, 4, len(inspect.Config.Labels))
98+
assert.Equal(t, "a1", inspect.Config.Labels["a"])
99+
assert.Equal(t, "https://github.com/git/repository", inspect.Config.Labels["org.opencontainers.image.source"])
100+
assert.Equal(t, "sha", inspect.Config.Labels["org.opencontainers.image.revision"])
101+
102+
created, err := time.Parse(time.RFC3339, inspect.Config.Labels["org.opencontainers.image.created"])
103+
assert.NilError(t, err)
104+
assert.Assert(t, created.Before(time.Now()))
105+
}
106+
98107
func removeImages(tags []string) error {
99108
var result error
100109
for _, tag := range tags {
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
INPUT_PATH=./testdata/build_push_tests
22
INPUT_DOCKERFILE=./testdata/build_push_tests/Dockerfile
33
INPUT_TAG_WITH_REF=true
4-
INPUT_ADD_GIT_LABELS=true
54
INPUT_TAGS=build-push-test
65
INPUT_LABELS=a=a1
76
INPUT_REGISTRY=localhost:5000
87
INPUT_USERNAME=my_user
98
INPUT_PASSWORD=my_password
109
INPUT_REPOSITORY=my-repository
1110
INPUT_PUSH=true
12-
GITHUB_ACTOR=actor
13-
GITHUB_SHA=sha
1411
GITHUB_REF=refs/tags/build-push-tag1

e2e/testdata/build_tests/auto_labels.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ INPUT_TAGS=auto-labels
66
INPUT_LABELS=a=a1
77
INPUT_REGISTRY=localhost:5000
88
INPUT_REPOSITORY=my-repository
9-
GITHUB_ACTOR=actor
9+
GITHUB_REPOSITORY=git/repository
1010
GITHUB_SHA=sha

internal/command/args_test.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func TestBuildArgs(t *testing.T) {
2626
testCases := []struct {
2727
name string
2828
build options.Build
29-
github options.GitHub
3029
tags []string
3130
expected []string
3231
}{
@@ -54,19 +53,6 @@ func TestBuildArgs(t *testing.T) {
5453
},
5554
expected: []string{"build", "--progress", "plain", "--label", "label1", "--label", "label2", "."},
5655
},
57-
{
58-
name: "with-git-labels",
59-
build: options.Build{
60-
Path: ".",
61-
AddGitLabels: true,
62-
Labels: []string{"label1"},
63-
},
64-
github: options.GitHub{
65-
Actor: "actor",
66-
Sha: "sha",
67-
},
68-
expected: []string{"build", "--progress", "plain", "--label", "label1", "--label", "com.docker.github-actions-actor=actor", "--label", "com.docker.github-actions-sha=sha", "."},
69-
},
7056
{
7157
name: "with-target",
7258
build: options.Build{
@@ -95,7 +81,7 @@ func TestBuildArgs(t *testing.T) {
9581
for _, tc := range testCases {
9682
tc := tc
9783
t.Run(tc.name, func(t *testing.T) {
98-
args := BuildArgs(tc.build, tc.github, tc.tags)
84+
args := BuildArgs(tc.build, options.GitHub{}, tc.tags)
9985
assert.DeepEqual(t, tc.expected, args)
10086
})
10187
}

internal/options/build.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package options
22

33
import (
4-
"fmt"
54
"os"
65
"strings"
6+
"time"
77

88
"github.com/caarlos0/env/v6"
99
)
1010

11-
const githubLabelPrefix = "com.docker.github-actions"
11+
const opencontainersLabelPrefix = "org.opencontainers.image"
1212

1313
// Build contains the parsed build action environment variables
1414
type Build struct {
@@ -50,13 +50,17 @@ func GetLabels(build Build, github GitHub) []string {
5050
return labels
5151
}
5252

53-
if github.Actor != "" {
54-
labels = append(labels, fmt.Sprintf("%s-actor=%s", githubLabelPrefix, github.Actor))
53+
if github.Repository != "" {
54+
labels = append(labels, opencontainersLabelPrefix+".source=https://github.com/"+github.Repository)
5555
}
5656

5757
if github.Sha != "" {
58-
labels = append(labels, fmt.Sprintf("%s-sha=%s", githubLabelPrefix, github.Sha))
58+
labels = append(labels, opencontainersLabelPrefix+".revision="+github.Sha)
59+
5960
}
6061

62+
createdTime := time.Now().UTC().Format(time.RFC3339)
63+
labels = append(labels, opencontainersLabelPrefix+".created="+createdTime)
64+
6165
return labels
6266
}

internal/options/build_test.go

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package options
22

33
import (
44
"os"
5+
"strings"
56
"testing"
7+
"time"
68

79
"gotest.tools/v3/assert"
810
)
@@ -31,46 +33,31 @@ func TestGetBuildOptions(t *testing.T) {
3133
}
3234

3335
func TestGetLabels(t *testing.T) {
34-
testCases := []struct {
35-
name string
36-
addGitLabels bool
37-
labels []string
38-
github GitHub
39-
expected []string
40-
}{
41-
{
42-
name: "no-git-labels",
43-
labels: []string{"label1", "label2"},
44-
expected: []string{"label1", "label2"},
45-
},
46-
{
47-
name: "with-git-labels",
48-
labels: []string{"label1", "label2"},
49-
addGitLabels: true,
50-
github: GitHub{
51-
Actor: "actor",
52-
Sha: "sha",
53-
},
54-
expected: []string{
55-
"label1",
56-
"label2",
57-
"com.docker.github-actions-actor=actor",
58-
"com.docker.github-actions-sha=sha",
59-
},
60-
},
61-
}
36+
expected := []string{"label1", "label2"}
37+
labels := GetLabels(Build{Labels: expected}, GitHub{})
38+
assert.DeepEqual(t, expected, labels)
39+
}
6240

63-
for _, tc := range testCases {
64-
tc := tc
65-
t.Run(tc.name, func(t *testing.T) {
66-
labels := GetLabels(
67-
Build{
68-
AddGitLabels: tc.addGitLabels,
69-
Labels: tc.labels,
70-
},
71-
tc.github,
72-
)
73-
assert.DeepEqual(t, tc.expected, labels)
41+
func TestGetLabelsWithGit(t *testing.T) {
42+
labels := GetLabels(
43+
Build{
44+
Labels: []string{"label1", "label2"},
45+
AddGitLabels: true,
46+
},
47+
GitHub{
48+
Repository: "myrepository",
49+
Sha: "mysha",
7450
})
75-
}
51+
52+
assert.Equal(t, 5, len(labels))
53+
assert.Equal(t, "label1", labels[0])
54+
assert.Equal(t, "label2", labels[1])
55+
assert.Equal(t, "org.opencontainers.image.source=https://github.com/myrepository", labels[2])
56+
assert.Equal(t, "org.opencontainers.image.revision=mysha", labels[3])
57+
58+
timeLabel := strings.SplitN(labels[4], "=", 2)
59+
assert.Equal(t, "org.opencontainers.image.created", timeLabel[0])
60+
labelTime, err := time.Parse(time.RFC3339, timeLabel[1])
61+
assert.NilError(t, err)
62+
assert.Assert(t, time.Now().After(labelTime))
7663
}

internal/options/github.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ type GitHub struct {
4545
RunInActions bool `env:"GITHUB_ACTIONS"`
4646
Workflow string `env:"GITHUB_WORKFLOW"`
4747
Action string `env:"GITHUB_ACTION"`
48-
Actor string `env:"GITHUB_ACTOR"`
4948
Repository string `env:"GITHUB_REPOSITORY"`
5049
EventName string `env:"GITHUB_EVENT_NAME"`
5150
Sha string `env:"GITHUB_SHA"`

internal/options/github_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ func TestGetGitHubOptions(t *testing.T) {
1111
_ = os.Setenv("GITHUB_ACTIONS", "true")
1212
_ = os.Setenv("GITHUB_WORKFLOW", "workflow")
1313
_ = os.Setenv("GITHUB_ACTION", "action")
14-
_ = os.Setenv("GITHUB_ACTOR", "actor")
1514
_ = os.Setenv("GITHUB_REPOSITORY", "repository")
1615
_ = os.Setenv("GITHUB_EVENT_NAME", "event-name")
1716
_ = os.Setenv("GITHUB_SHA", "sha")
@@ -23,7 +22,6 @@ func TestGetGitHubOptions(t *testing.T) {
2322
RunInActions: true,
2423
Workflow: "workflow",
2524
Action: "action",
26-
Actor: "actor",
2725
Repository: "repository",
2826
EventName: "event-name",
2927
Sha: "sha",

0 commit comments

Comments
 (0)