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

Commit 25af2ba

Browse files
authored
Merge pull request #4 from dougnukem/doug/go-versions
Actions support for multiple go version go1.10 ... go1.12
2 parents 25dfed5 + 248f6a0 commit 25af2ba

File tree

14 files changed

+403
-43
lines changed

14 files changed

+403
-43
lines changed

.dockerfile_lint/default_rules.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
required: true
2525

2626
FROM:
27-
paramSyntaxRegex: /^[\w./\-:]+(:[\w.]+)?(-[\w]+)?( as \w+)?$/i
27+
paramSyntaxRegex: /^[\w./\-:]+(:\$*[\w.]+)?(-[\w]+)?( as \w+)?$/i
2828
rules:
2929
-
3030
label: "is_latest_tag"

.dockerfile_lint/github_actions.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ line_rules:
3232
regex: /.+/
3333
level: "info"
3434
message: "Avoid using ADD"
35-
description: "It is generally an anti-pattern to us ADD, use COPY instead."
35+
description: "It is generally an anti-pattern to use ADD, use COPY instead."
3636
EXPOSE:
3737
paramSyntaxRegex: /.+/
3838
rules:

.github/main.workflow

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ workflow "Build on Push" {
33
resolves = "Build"
44
}
55

6-
workflow "Build and Publish" {
7-
on = "release"
8-
resolves = "Docker Publish"
9-
}
10-
116
action "Lint" {
127
uses = "actions/action-builder/shell@master"
138
runs = "make"
@@ -22,10 +17,15 @@ action "Test" {
2217

2318
action "Build" {
2419
needs = ["Lint", "Test"]
25-
uses = "actions/docker/cli@master"
26-
args = "build -t golang-action ."
20+
uses = "actions/action-builder/docker@master"
21+
runs = "make"
22+
args = "build"
2723
}
2824

25+
workflow "Build and Publish" {
26+
on = "release"
27+
resolves = "Docker Publish"
28+
}
2929

3030
action "Docker Login" {
3131
uses = "actions/docker/login@master"
@@ -34,8 +34,9 @@ action "Docker Login" {
3434

3535
action "Docker Publish" {
3636
needs = ["Build", "Docker Login", "Docker Tag"]
37-
uses = "actions/docker/cli@master"
38-
args = "push cedrickring/golang-action"
37+
uses = "actions/action-builder/docker@master"
38+
runs = "make"
39+
args = "publish"
3940
}
4041

4142
action "Docker Tag" {

.github/publish.workflow

Lines changed: 0 additions & 22 deletions
This file was deleted.

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM golang:1.11
22

33
LABEL name="Golang Action"
44
LABEL maintainer="Cedric Kring"
5-
LABEL version="1.1.0"
5+
LABEL version="1.1.1"
66
LABEL repository="https://github.com/cedrickring/golang-action"
77

88
LABEL com.github.actions.name="Golang Action"

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This Action allows you to run Go commands with your code. It will automatically
55
## How to use
66

77
1. Add an Action
8-
2. Enter "cedrickring/golang-action@1.1.0" (`@1.1.0-go-1.10` for Golang 1.10)
8+
2. Enter "cedrickring/golang-action@1.1.1" (`cedrickring/golang-action/go1.10@1.1.1` for Golang 1.10, 1.11, 1.12)
99
3. If your repo builds with `make` or `go build && go test`, that's all you need. Otherwise, add a command in the args section like:
1010
```bash
1111
go build -o my_executable main.go
@@ -25,7 +25,7 @@ block like this:
2525
2626
```hcl
2727
action "ci" {
28-
uses="cedrickring/golang-action@1.1.0"
28+
uses="cedrickring/golang-action@1.1.1"
2929
3030
# optional build command:
3131
args="./build.sh"
@@ -36,3 +36,12 @@ action "ci" {
3636
}
3737
}
3838
```
39+
40+
To use a specific golang version (1.10, 1.11, 1.12):
41+
42+
```hcl
43+
action "ci" {
44+
uses="cedrickring/golang-action/go1.12@1.1.1"
45+
...
46+
}
47+
```

build/semver

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit -o nounset -o pipefail
4+
5+
SEMVER_REGEX="^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
6+
7+
PROG=semver
8+
PROG_VERSION=2.1.0
9+
10+
USAGE="\
11+
Usage:
12+
$PROG bump (major|minor|patch|release|prerel <prerel>|build <build>) <version>
13+
$PROG compare <version> <other_version>
14+
$PROG get (major|minor|patch|release|prerel|build) <version>
15+
$PROG --help
16+
$PROG --version
17+
18+
Arguments:
19+
<version> A version must match the following regex pattern:
20+
\"${SEMVER_REGEX}\".
21+
In english, the version must match X.Y.Z(-PRERELEASE)(+BUILD)
22+
where X, Y and Z are positive integers, PRERELEASE is an optionnal
23+
string composed of alphanumeric characters and hyphens and
24+
BUILD is also an optional string composed of alphanumeric
25+
characters and hyphens.
26+
27+
<other_version> See <version> definition.
28+
29+
<prerel> String that must be composed of alphanumeric characters and hyphens.
30+
31+
<build> String that must be composed of alphanumeric characters and hyphens.
32+
33+
Options:
34+
-v, --version Print the version of this tool.
35+
-h, --help Print this help message.
36+
37+
Commands:
38+
bump Bump <version> by one of major, minor, patch, prerel, build
39+
or a forced potentialy conflicting version. The bumped version is
40+
shown to stdout.
41+
42+
compare Compare <version> with <other_version>, output to stdout the
43+
following values: -1 if <other_version> is newer, 0 if equal, 1 if
44+
older.
45+
46+
get Extract given part of <version>, where part is one of major, minor,
47+
patch, prerel, build."
48+
49+
50+
function error {
51+
echo -e "$1" >&2
52+
exit 1
53+
}
54+
55+
function usage-help {
56+
error "$USAGE"
57+
}
58+
59+
function usage-version {
60+
echo -e "${PROG}: $PROG_VERSION"
61+
exit 0
62+
}
63+
64+
function validate-version {
65+
local version=$1
66+
if [[ "$version" =~ $SEMVER_REGEX ]]; then
67+
# if a second argument is passed, store the result in var named by $2
68+
if [ "$#" -eq "2" ]; then
69+
local major=${BASH_REMATCH[1]}
70+
local minor=${BASH_REMATCH[2]}
71+
local patch=${BASH_REMATCH[3]}
72+
local prere=${BASH_REMATCH[4]}
73+
local build=${BASH_REMATCH[6]}
74+
eval "$2=(\"$major\" \"$minor\" \"$patch\" \"$prere\" \"$build\")"
75+
else
76+
echo "$version"
77+
fi
78+
else
79+
error "version $version does not match the semver scheme 'X.Y.Z(-PRERELEASE)(+BUILD)'. See help for more information."
80+
fi
81+
}
82+
83+
function compare-version {
84+
validate-version "$1" V
85+
validate-version "$2" V_
86+
87+
# MAJOR, MINOR and PATCH should compare numericaly
88+
for i in 0 1 2; do
89+
local diff=$((${V[$i]} - ${V_[$i]}))
90+
if [[ $diff -lt 0 ]]; then
91+
echo -1; return 0
92+
elif [[ $diff -gt 0 ]]; then
93+
echo 1; return 0
94+
fi
95+
done
96+
97+
# PREREL should compare with the ASCII order.
98+
if [[ -z "${V[3]}" ]] && [[ -n "${V_[3]}" ]]; then
99+
echo -1; return 0;
100+
elif [[ -n "${V[3]}" ]] && [[ -z "${V_[3]}" ]]; then
101+
echo 1; return 0;
102+
elif [[ -n "${V[3]}" ]] && [[ -n "${V_[3]}" ]]; then
103+
if [[ "${V[3]}" > "${V_[3]}" ]]; then
104+
echo 1; return 0;
105+
elif [[ "${V[3]}" < "${V_[3]}" ]]; then
106+
echo -1; return 0;
107+
fi
108+
fi
109+
110+
echo 0
111+
}
112+
113+
function command-bump {
114+
local new; local version; local sub_version; local command;
115+
116+
case $# in
117+
2) case $1 in
118+
major|minor|patch|release) command=$1; version=$2;;
119+
*) usage-help;;
120+
esac ;;
121+
3) case $1 in
122+
prerel|build) command=$1; sub_version=$2 version=$3 ;;
123+
*) usage-help;;
124+
esac ;;
125+
*) usage-help;;
126+
esac
127+
128+
validate-version "$version" parts
129+
# shellcheck disable=SC2154
130+
local major="${parts[0]}"
131+
local minor="${parts[1]}"
132+
local patch="${parts[2]}"
133+
local prere="${parts[3]}"
134+
local build="${parts[4]}"
135+
136+
case "$command" in
137+
major) new="$((major + 1)).0.0";;
138+
minor) new="${major}.$((minor + 1)).0";;
139+
patch) new="${major}.${minor}.$((patch + 1))";;
140+
release) new="${major}.${minor}.${patch}";;
141+
prerel) new=$(validate-version "${major}.${minor}.${patch}-${sub_version}");;
142+
build) new=$(validate-version "${major}.${minor}.${patch}${prere}+${sub_version}");;
143+
*) usage-help ;;
144+
esac
145+
146+
echo "$new"
147+
exit 0
148+
}
149+
150+
function command-compare {
151+
local v; local v_;
152+
153+
case $# in
154+
2) v=$(validate-version "$1"); v_=$(validate-version "$2") ;;
155+
*) usage-help ;;
156+
esac
157+
158+
compare-version "$v" "$v_"
159+
exit 0
160+
}
161+
162+
163+
# shellcheck disable=SC2034
164+
function command-get {
165+
local part version
166+
167+
if [[ "$#" -ne "2" ]] || [[ -z "$1" ]] || [[ -z "$2" ]]; then
168+
usage-help
169+
exit 0
170+
fi
171+
172+
part="$1"
173+
version="$2"
174+
175+
validate-version "$version" parts
176+
local major="${parts[0]}"
177+
local minor="${parts[1]}"
178+
local patch="${parts[2]}"
179+
local prerel="${parts[3]:1}"
180+
local build="${parts[4]:1}"
181+
182+
case "$part" in
183+
major|minor|patch|release|prerel|build) echo "${!part}" ;;
184+
*) usage-help ;;
185+
esac
186+
187+
exit 0
188+
}
189+
190+
case $# in
191+
0) echo "Unknown command: $*"; usage-help;;
192+
esac
193+
194+
case $1 in
195+
--help|-h) echo -e "$USAGE"; exit 0;;
196+
--version|-v) usage-version ;;
197+
bump) shift; command-bump "$@";;
198+
get) shift; command-get "$@";;
199+
compare) shift; command-compare "$@";;
200+
*) echo "Unknown arguments: $*"; usage-help;;
201+
esac

docker.mk

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,61 @@
1-
IMAGE_NAME=$(shell basename $(CURDIR))
2-
DOCKER_REPO?="cedrickring"
1+
IMAGE_NAME?=golang-action
2+
GITHUB_REPO=$(shell git remote get-url origin | sed 's/.*\/\(.*\)\/\(.*\)\.git/\1\/\2/')
3+
DOCKER_REPO=$(shell echo $(GITHUB_REPO) | sed 's/\(.*\)\/\(.*\)/\1/')
34
ROOT_DIR?=$(CURDIR)
5+
GO_VERSION_DIRS=$(wildcard go*)
6+
GO_VERSIONS=$(shell echo $(GO_VERSION_DIRS) | sed 's/go//g')
7+
ACTION_VERSION=$(shell cat Dockerfile | grep "LABEL version" | sed 's/LABEL version\="\(.*\)"/\1/')
8+
ACTION_MAJOR_VERSION=$(shell build/semver get major $(ACTION_VERSION) )
9+
ACTION_MINOR_VERSION=$(shell build/semver get minor $(ACTION_VERSION))
410

511
.PHONY: docker-lint
6-
docker-lint: ## Run Dockerfile Lint on all dockerfiles.
12+
docker-lint: update-docker-go-versions ## Run Dockerfile Lint on all dockerfiles.
713
dockerfile_lint -r $(ROOT_DIR)/.dockerfile_lint/github_actions.yaml $(wildcard Dockerfile* */Dockerfile*)
814

915
.PHONY: docker-build
10-
docker-build: ## Build the top level Dockerfile using the directory or $IMAGE_NAME as the name.
11-
docker build -t $(IMAGE_NAME) .
12-
16+
docker-build: update-docker-go-versions ## Build the top level Dockerfile using the directory or $IMAGE_NAME as the name.
17+
## Build the main action
18+
docker build $(DOCKER_BUILD_ARG) -t $(IMAGE_NAME) -t $(IMAGE_NAME):latest .
19+
## Build specific golang versions
20+
for version in $(GO_VERSIONS) ; do \
21+
docker build $(DOCKER_BUILD_ARG) -t $(IMAGE_NAME) -t $(IMAGE_NAME):$$version go$$version/; \
22+
done
23+
1324
.PHONY: docker-tag
1425
docker-tag: ## Tag the docker image using the tag script.
15-
docker tag $(IMAGE_NAME) $(DOCKER_REPO)/$(IMAGE_NAME)
26+
docker tag $(IMAGE_NAME):latest $(DOCKER_REPO)/$(IMAGE_NAME):$(ACTION_VERSION)
27+
docker tag $(IMAGE_NAME):latest $(DOCKER_REPO)/$(IMAGE_NAME):$(ACTION_MAJOR_VERSION)
28+
docker tag $(IMAGE_NAME):latest $(DOCKER_REPO)/$(IMAGE_NAME):$(ACTION_MAJOR_VERSION).$(ACTION_MINOR_VERSION)
29+
for version in $(GO_VERSIONS) ; do \
30+
docker tag $(IMAGE_NAME):$$version $(DOCKER_REPO)/$(IMAGE_NAME):$(ACTION_VERSION)-go$$version; \
31+
docker tag $(IMAGE_NAME):$$version $(DOCKER_REPO)/$(IMAGE_NAME):$(ACTION_MAJOR_VERSION)-go$$version; \
32+
docker tag $(IMAGE_NAME):$$version $(DOCKER_REPO)/$(IMAGE_NAME):$(ACTION_MAJOR_VERSION).$(ACTION_MINOR_VERSION)-go$$version; \
33+
done
1634

1735
.PHONY: docker-publish
1836
docker-publish: docker-tag ## Publish the image and tags to a repository.
1937
docker push $(DOCKER_REPO)/$(IMAGE_NAME)
38+
39+
.PHONY: update-docker-go-versions
40+
update-docker-go-versions: ## Updates go go1.10, go1.11 from the main Dockerfile
41+
for version in $(GO_VERSIONS) ; do \
42+
sed -e 's/FROM golang:.*/FROM golang:'$$version'/' Dockerfile > go$$version/Dockerfile; \
43+
cp entrypoint.sh go$$version/; \
44+
done
45+
46+
update-version = sed -i.bak -e 's/LABEL version=".*"/LABEL version="'`build/semver bump $(1) $(ACTION_VERSION)`'"/' Dockerfile && rm Dockerfile.bak
47+
48+
.PHONY: version-bump-major
49+
version-bump-major: docker-lint
50+
$(call update-version,major)
51+
$(MAKE) -C . update-docker-go-versions
52+
53+
.PHONY: version-bump-minor
54+
version-bump-minor: docker-lint
55+
$(call update-version,minor)
56+
$(MAKE) -C . update-docker-go-versions
57+
58+
.PHONY: version-bump-patch
59+
version-bump-patch: docker-lint
60+
$(call update-version,patch)
61+
$(MAKE) -C . update-docker-go-versions

0 commit comments

Comments
 (0)