Skip to content

Commit

Permalink
feat: Add labels on image build
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvey committed Dec 21, 2021
1 parent 1611521 commit ccd6858
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
29 changes: 28 additions & 1 deletion builder/docker/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package docker

import (
"fmt"
"time"

"github.com/radiofrance/dib/exec"
"github.com/radiofrance/dib/types"
"github.com/sirupsen/logrus"
Expand All @@ -25,8 +28,32 @@ func (b ImageBuilder) Build(opts types.ImageBuilderOpts) error {
logrus.Infof("[DRY-RUN] docker push %s", opts.Tag)
return nil
}
dockerArgs := []string{
"build",
"--no-cache",
"--pull",
}

if opts.CreationTime != nil {
dockerArgs = append(dockerArgs, "--label", fmt.Sprintf("org.opencontainers.image.created=%s",
opts.CreationTime.Format(time.RFC3339)))
}
if opts.Authors != nil {
dockerArgs = append(dockerArgs, "--label", fmt.Sprintf("org.opencontainers.image.authors=%s",
*opts.Authors))
}
if opts.Source != nil {
dockerArgs = append(dockerArgs, "--label", fmt.Sprintf("org.opencontainers.image.source=%s",
*opts.Source))
}
if opts.Revision != nil {
dockerArgs = append(dockerArgs, "--label", fmt.Sprintf("org.opencontainers.image.revision=%s",
*opts.Revision))
}

dockerArgs = append(dockerArgs, "-t", opts.Tag, opts.Context)

err := b.exec.ExecuteStdout("docker", "build", "--no-cache", "--pull", "-t", opts.Tag, opts.Context)
err := b.exec.ExecuteStdout("docker", dockerArgs...)
if err != nil {
return err
}
Expand Down
50 changes: 46 additions & 4 deletions dag/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package dag

import (
"fmt"
"os"
"strings"
"sync"
"time"

"github.com/radiofrance/dib/types"

Expand Down Expand Up @@ -100,10 +102,23 @@ func (img *Image) doRebuild(newTag string) error {
return err
}

err := img.Builder.Build(types.ImageBuilderOpts{
Context: img.Dockerfile.ContextPath,
Tag: fmt.Sprintf("%s:%s", img.Name, newTag),
})
now := time.Now()
opts := types.ImageBuilderOpts{
Context: img.Dockerfile.ContextPath,
Tag: fmt.Sprintf("%s:%s", img.Name, newTag),
CreationTime: &now,
}
if rev := findRevision(); rev != "" {
opts.Revision = &rev
}
if authors := findAuthors(); authors != "" {
opts.Authors = &authors
}
if source := findSource(); source != "" {
opts.Authors = &source
}

err := img.Builder.Build(opts)
if err != nil {
return err
}
Expand All @@ -115,6 +130,33 @@ func (img *Image) doRebuild(newTag string) error {
return nil
}

func findSource() string {
if url := os.Getenv("CI_PROJECT_URL"); url != "" { // gitlab predefined variable
return url
} else if url := os.Getenv("GITHUB_REPOSITORY"); url != "" { // github predefined variable
return fmt.Sprintf("https://github.com/%s", url)
}
return ""
}

func findAuthors() string {
if authors := os.Getenv("GITLAB_USER_NAME"); authors != "" { // gitlab predefined variable
return authors
} else if authors := os.Getenv("GITHUB_ACTOR"); authors != "" { // github predefined variable
return authors
}
return ""
}

func findRevision() string {
if rev := os.Getenv("CI_COMMIT_SHA"); rev != "" { // gitlab predefined variable
return rev
} else if rev := os.Getenv("GITHUB_SHA"); rev != "" { // github predefined variable
return rev
}
return ""
}

// runTests run docker tests for each TestRunner.
func (img *Image) runTests(ref, path string) error {
rateLimit <- struct{}{}
Expand Down
10 changes: 10 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import "time"

// ImageBuilder is the interface for building Docker images.
type ImageBuilder interface {
Build(opts ImageBuilderOpts) error
Expand All @@ -11,6 +13,14 @@ type ImageBuilderOpts struct {
Context string
// Name of the tag to build, same as passed to the '-t' flag of the docker build command.
Tag string
// Date and time on which the image was built (string, date-time as defined by RFC 3339).
CreationTime *time.Time
// Contact details of the people or organization responsible for the image (freeform string)
Authors *string
// URL to get source code for building the image (string)
Source *string
// Source control revision identifier for the packaged software.
Revision *string
}

// TestRunner is an interface for dealing with docker tests, such as dgoss, trivy.
Expand Down

0 comments on commit ccd6858

Please sign in to comment.