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

Commit fed5809

Browse files
authored
Merge pull request #3 from docker/DESKTOP-2410-action-definition
DESKTOP-2410 action definition
2 parents 20414ad + 8f75de3 commit fed5809

File tree

12 files changed

+107
-34
lines changed

12 files changed

+107
-34
lines changed

.github/workflows/main.yml

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
name: CI
2-
on: [push, pull_request]
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
tags:
8+
- '*'
9+
pull_request:
10+
311
jobs:
412
build:
513
name: build
@@ -9,14 +17,17 @@ jobs:
917
- name: Checkout
1018
uses: actions/checkout@v2
1119

12-
- name: Print env
13-
run: env
14-
15-
- name: Read event json
16-
run: cat $GITHUB_EVENT_PATH
17-
18-
- name: Build
19-
run: DOCKER_BUILDKIT=1 make -f docker.Makefile build
20+
- name: build and test code
21+
run: DOCKER_BUILDKIT=1 make -f docker.Makefile all
2022

21-
- name: E2E
22-
run: DOCKER_BUILDKIT=1 make -f docker.Makefile test-e2e
23+
- name: build and push image
24+
uses: ./
25+
env:
26+
DOCKER_BUILDKIT: 1
27+
with:
28+
username: ${{ secrets.DOCKER_USERNAME }}
29+
password: ${{ secrets.DOCKER_PASSWORD }}
30+
repository: docker/github-actions
31+
tag_with_ref: true
32+
build_args: MAKE_TARGET=build
33+
push: ${{ startsWith(github.ref, 'refs/tags/') }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Auto tags depend on the git reference that the run is associated with. The refer
8888

8989
If the reference is `refs/heads/{branch-name}` then the tag `{branch-name}` is added. For the master branch the `{branch-name}` is replaced with `latest`.
9090

91-
If the reference is `refs/pull-requests/{pr}` then the tag `pr-{pr}` is added.
91+
If the reference is `refs/pull/{pr}` then the tag `pr-{pr}` is added.
9292

9393
If the reference is `refs/tags/{tag-name}` then the tag `{tag-name}` is added.
9494

action.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Build and push docker images
2+
description: Builds and pushes docker images and will log in to a docker registry if required
3+
author: Docker
4+
runs:
5+
using: docker
6+
image: docker://docker/github-actions:v0.0.1
7+
args:
8+
- build-push
9+
inputs:
10+
username:
11+
description: Username used to log in to a docker registry. If not set then no login will occur
12+
required: false
13+
password:
14+
description: Password used to log in to a docker registry. If not set then no login will occur
15+
required: false
16+
registry:
17+
description: Server address of docker registry. If not set then will default to Docker Hub
18+
required: false
19+
repository:
20+
description: Docker repository to tag the image with
21+
required: true
22+
tags:
23+
description: Comma-delimited list of tags. These will be added to the server/repository to form the image's tags
24+
required: false
25+
tag_with_ref:
26+
description: Automatically tags the built image with the git reference as per the readme
27+
required: false
28+
default: false
29+
tag_with_sha:
30+
description: Automatically tags the built image with the git short sha as per the readme
31+
required: false
32+
default: false
33+
path:
34+
description: Path to run docker build from
35+
required: false
36+
default: "."
37+
dockerfile:
38+
description: Name of the Dockerfile (Default is 'path/Dockerfile')
39+
required: false
40+
target:
41+
description: Sets the target build stage to build
42+
required: false
43+
always_pull:
44+
description: Always attempt to pull a newer version of the image
45+
required: false
46+
default: false
47+
build_args:
48+
description: Comma-delmited list of build-time variables
49+
required: false
50+
labels:
51+
description: Comma-delimited list of labels to add to the built image. E.g. `label_name_1=label_value_1,label_name_2=label_value_2`
52+
required: false
53+
add_git_labels:
54+
description: Adds labels with git repo info to the built image
55+
required: false
56+
default: false
57+
push:
58+
description: Whether to push the image
59+
required: false
60+
default: true

cmd/build_push.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"fmt"
5+
46
"github.com/docker/github-actions/internal/command"
57
"github.com/docker/github-actions/internal/options"
68
)
@@ -37,7 +39,10 @@ func buildPush(cmd command.Runner) error {
3739
return err
3840
}
3941
}
42+
43+
return command.RunPush(cmd, tags)
4044
}
4145

42-
return command.RunPush(cmd, tags)
46+
fmt.Println("Skipping push")
47+
return nil
4348
}

cmd/main.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,10 @@ import (
55
"os"
66

77
"github.com/docker/github-actions/internal/command"
8-
"github.com/docker/github-actions/internal/options"
98
"github.com/spf13/cobra"
109
)
1110

1211
func main() {
13-
_, err := options.GetGitHubOptions()
14-
if err != nil {
15-
fmt.Println(err)
16-
os.Exit(1)
17-
}
18-
1912
runner := command.NewRunner()
2013

2114
rootCmd := &cobra.Command{
@@ -53,7 +46,7 @@ func main() {
5346
},
5447
)
5548

56-
if err = rootCmd.Execute(); err != nil {
49+
if err := rootCmd.Execute(); err != nil {
5750
fmt.Println(err)
5851
os.Exit(1)
5952
}

docker.Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ DOCKER_BUILD = docker build --progress=plain
44
ROOT_DIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
55
ROOT_DIR := $(subst .\,,$(ROOT_DIR))
66

7-
all: build test-unit
7+
all: build test-e2e
88

99
build:
1010
$(DOCKER_BUILD) -t docker/github-actions-default --build-arg MAKE_TARGET=default .

e2e/testdata/build_tests/tag_pr.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ INPUT_TAG_WITH_REF=true
44
INPUT_ADD_GIT_LABELS=false
55
INPUT_REGISTRY=localhost:5000
66
INPUT_REPOSITORY=my-repository
7-
GITHUB_REF=refs/pulls/pr1
7+
GITHUB_REF=refs/pull/pr1

internal/command/args.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func LoginArgs(o options.Login, registry string) []string {
1515

1616
// BuildArgs converts build options into the cli arguments used to call `docker build`
1717
func BuildArgs(o options.Build, github options.GitHub, tags []string) []string {
18-
args := []string{"build"}
18+
args := []string{"build", "--progress", "plain"}
1919

2020
for _, tag := range tags {
2121
args = append(args, "-t", tag)

internal/command/args_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,26 @@ func TestBuildArgs(t *testing.T) {
3333
{
3434
name: "basic",
3535
build: options.Build{Path: "path"},
36-
expected: []string{"build", "path"},
36+
expected: []string{"build", "--progress", "plain", "path"},
3737
},
3838
{
3939
name: "with-dockerfile",
4040
build: options.Build{Path: ".", Dockerfile: "dockerfile"},
41-
expected: []string{"build", "--file", "dockerfile", "."},
41+
expected: []string{"build", "--progress", "plain", "--file", "dockerfile", "."},
4242
},
4343
{
4444
name: "with-tags",
4545
build: options.Build{Path: "."},
4646
tags: []string{"tag1", "tag2"},
47-
expected: []string{"build", "-t", "tag1", "-t", "tag2", "."},
47+
expected: []string{"build", "--progress", "plain", "-t", "tag1", "-t", "tag2", "."},
4848
},
4949
{
5050
name: "with-static-labels",
5151
build: options.Build{
5252
Path: ".",
5353
Labels: []string{"label1", "label2"},
5454
},
55-
expected: []string{"build", "--label", "label1", "--label", "label2", "."},
55+
expected: []string{"build", "--progress", "plain", "--label", "label1", "--label", "label2", "."},
5656
},
5757
{
5858
name: "with-git-labels",
@@ -65,31 +65,31 @@ func TestBuildArgs(t *testing.T) {
6565
Actor: "actor",
6666
Sha: "sha",
6767
},
68-
expected: []string{"build", "--label", "label1", "--label", "com.docker.github-actions-actor=actor", "--label", "com.docker.github-actions-sha=sha", "."},
68+
expected: []string{"build", "--progress", "plain", "--label", "label1", "--label", "com.docker.github-actions-actor=actor", "--label", "com.docker.github-actions-sha=sha", "."},
6969
},
7070
{
7171
name: "with-target",
7272
build: options.Build{
7373
Path: ".",
7474
Target: "target",
7575
},
76-
expected: []string{"build", "--target", "target", "."},
76+
expected: []string{"build", "--progress", "plain", "--target", "target", "."},
7777
},
7878
{
7979
name: "with-always-pull",
8080
build: options.Build{
8181
Path: ".",
8282
AlwaysPull: true,
8383
},
84-
expected: []string{"build", "--pull", "."},
84+
expected: []string{"build", "--progress", "plain", "--pull", "."},
8585
},
8686
{
8787
name: "with-build-args",
8888
build: options.Build{
8989
Path: ".",
9090
BuildArgs: []string{"build-arg-1", "build-arg-2"},
9191
},
92-
expected: []string{"build", "--build-arg", "build-arg-1", "--build-arg", "build-arg-2", "."},
92+
expected: []string{"build", "--progress", "plain", "--build-arg", "build-arg-1", "--build-arg", "build-arg-2", "."},
9393
},
9494
}
9595
for _, tc := range testCases {

internal/command/runner.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package command
22

33
import (
4+
"fmt"
45
"os"
56
"os/exec"
67

@@ -30,18 +31,21 @@ func (runner execRunner) Run(name string, args ...string) error {
3031

3132
// RunLogin runs a docker login
3233
func RunLogin(cmd Runner, opt options.Login, registry string) error {
34+
fmt.Println("Logging in to registry", registry)
3335
args := LoginArgs(opt, registry)
3436
return cmd.Run("docker", args...)
3537
}
3638

3739
// RunBuild runs a docker build and tags the resulting image
3840
func RunBuild(cmd Runner, opt options.Build, github options.GitHub, tags []string) error {
41+
fmt.Println("Building image", tags)
3942
args := BuildArgs(opt, github, tags)
4043
return cmd.Run("docker", args...)
4144
}
4245

4346
// RunPush runs a docker push for each tag
4447
func RunPush(cmd Runner, tags []string) error {
48+
fmt.Println("Pushing image", tags)
4549
for _, tag := range tags {
4650
args := PushArgs(tag)
4751
if err := cmd.Run("docker", args...); err != nil {

0 commit comments

Comments
 (0)