Skip to content

Commit 9df541f

Browse files
authored
Speed up workflow testing by getting away from Docker (PR #21)
## Feature * feat: requirement checker for merge testing * feat: use `golangci-lint` v1.36.0 -> latest * feat: coverage action w/out docker * feat: merge-tests.yaml w/out docker * feat: use pre-installed shellcheck * feat: ".shellceckrc" - For older version compatibility. (CI uses old version) ## Fixes * fix: shell check errors * fix: golangci-lint.yaml as dispatch workflow only - Since merge-tests.yaml does the same thing limit it to `workflow_dispatch` as an individual test
1 parent 1314f7c commit 9df541f

File tree

11 files changed

+251
-59
lines changed

11 files changed

+251
-59
lines changed

.devcontainer/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ RUN url_download="https://github.com/koalaman/shellcheck/releases/download/lates
7575
&& shellcheck --version \
7676
&& rm -r "$path_tmp_dir"
7777

78-
# golangci-lint - The fast Go linters runner.
79-
# binary will be $(go env GOPATH)/bin/golangci-lint
80-
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.36.0 \
78+
# golangci-lint - The fast Go linters runner. Version=latest
79+
# binary will be installed under: $(go env GOPATH)/bin/golangci-lint
80+
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin \
8181
# Smoke test
8282
&& golangci-lint --version
8383

.devcontainer/README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
<!-- markdownlint-disable MD033 -->
2-
# Dockerfile for CIs and Development
2+
# Dockerfile for GitHub and VSCode Users To Develop
33

4-
## For CIs
4+
This directory is for [GitHub Codespaces](https://github.com/features/codespaces) and/or [VS Code + Docker](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) users for development.
55

6-
The [CI](https://en.wikipedia.org/wiki/Continuous_integration) will run the [Dockerfile](Dockerfile) to build the image and then run the tests in a container.
6+
It includes most of the necessary packages and tools for developing Golang app. Aiming to provide the same environment to develop the app.
77

8-
- Current CI
9-
- [GitHub Actions](https://docs.github.com/en/free-pro-team@latest/actions): [../.github/workflows/](https://github.com/KEINOS/Hello-Cobra/tree/main/.github/workflows)
8+
## Developing Online
109

11-
## For DEVs
10+
If GitHub detects this directory (`.devcontainer`) in the repo, then you will be able to develop online via [GitHub Codespaces](https://github.com/features/codespaces).
1211

13-
This directory is for [GitHub Codespaces](https://github.com/features/codespaces) and [VS Code + Docker](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) users to develop the cloned/forked repo.
12+
## VS Code + Docker User
1413

15-
### VS Code + Docker User
14+
The container contains VS Code Server as well. If you already have installed the "[Remote - Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers)" extension, then press "<kbd>F1</kbd>" and select "`Remote-Containers: Open in Container`".
1615

17-
If you already have installed the "[Remote - Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers)" extension, press "<kbd>F1</kbd>" and select "`Remote-Containers: Open in Container`". After a while, you'll get most of the environment needed to develop and debug.
16+
After a while, you'll get most of the environment needed to develop and debug.
1817

19-
- File Description
20-
- [cobra.yaml](cobra.yaml): Default `cobra` command Settings.
21-
- [devcontainer.json](devcontainer.json): VSCode Extensions to be installed.
22-
- [Dockerfile](Dockerfile): Alpine based Golang development container.
23-
- [postCreateCommand.sh](postCreateCommand.sh): Initialization script that runs after the container and the VSCode server is up and before connection from VSCode.
24-
- [settings.vscode.json](settings.vscode.json): Additional VSCode Settings.
25-
- [welcome.sh](welcome.sh): Bash script to display the welcome message in the terminal when first login. It will display the basic info and TIPs to use.
18+
## File Description
19+
20+
- [cobra.yaml](cobra.yaml): Default `cobra` command Settings. Used for `$ cobra add ..`
21+
- [devcontainer.env](devcontainer.env): ENV variables to be loaded once when the container's created.
22+
- [devcontainer.json](devcontainer.json): VSCode Extensions to be installed and env settings.
23+
- [Dockerfile](Dockerfile): Debian 10 (buster) based Golang development container.
24+
- [postCreateCommand.sh](postCreateCommand.sh): Initialization script that runs after the container and the VSCode server is up.
25+
- [README.md](README.md): This file. ;-)
26+
- [welcome.sh](welcome.sh): Bash script to display the basic info and TIPs to use in the first shell login.

.github/check-requirements.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/sh
2+
# =============================================================================
3+
# This script checks if the commands and packages required for merge testing
4+
# are installed.
5+
# =============================================================================
6+
7+
# -----------------------------------------------------------------------------
8+
# Constants
9+
# -----------------------------------------------------------------------------
10+
SUCCESS=0
11+
FAILURE=1
12+
TRUE=0
13+
FALSE=1
14+
15+
# -----------------------------------------------------------------------------
16+
# Functions
17+
# -----------------------------------------------------------------------------
18+
isAvailable() {
19+
command_tmp="${1:?Command name missing}"
20+
msg_error="${2:?Error message missing}"
21+
url_reference="${3:?'URL for reference missing'}"
22+
23+
printf -- ' %s ... ' "$command_tmp"
24+
if ! which "$command_tmp" 1>/dev/null 2>/dev/null; then
25+
flag_covered_all=$FALSE
26+
echo 'NG'
27+
echo >&2 " - ABOUT : ${msg_error}"
28+
echo >&2 " - DETAILS: ${url_reference}"
29+
return $FALSE
30+
fi
31+
echo 'OK'
32+
}
33+
34+
# -----------------------------------------------------------------------------
35+
# Main
36+
# -----------------------------------------------------------------------------
37+
flag_covered_all=$TRUE
38+
39+
echo 'Checking requirements for:'
40+
41+
echo
42+
echo 'Shell scripts:'
43+
isAvailable \
44+
shellcheck \
45+
'"shellcheck" is a static analysis tool for shell scripts.' \
46+
'https://github.com/koalaman/shellcheck'
47+
48+
isAvailable shfmt \
49+
'"shfmt" is a linter for shell scripts to support POSIX Shell, Bash, and mksh.' \
50+
'https://github.com/mvdan/sh'
51+
52+
echo
53+
echo 'Go programs:'
54+
isAvailable \
55+
go \
56+
'"go" is required as a matter of course.' \
57+
'https://golang.org/'
58+
59+
isAvailable \
60+
gofmt \
61+
'"gofmt" is a formatter for golang.' \
62+
'https://golang.org/cmd/gofmt/'
63+
64+
isAvailable \
65+
golangci-lint \
66+
'"golangci-lint" is is a Go linters aggregator.' \
67+
'https://golangci-lint.run/'
68+
69+
if [ $flag_covered_all -ne 0 ]; then
70+
echo
71+
echo >&2 "Some requirements missing."
72+
exit $FAILURE
73+
fi
74+
75+
echo
76+
echo 'OK - All requirements were installed! You are good to Go testing!'
77+
exit $SUCCESS

.github/mergify.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pull_request_rules:
22
- name: automatic merge on CI success and 2 approved reviews
33
conditions:
44
- "#approved-reviews-by>=2"
5-
- "check-success=docker_test"
5+
- "check-success=merge_tests"
66
- base=main
77
- -draft
88
actions:
@@ -11,7 +11,7 @@ pull_request_rules:
1111
strict: smart+fasttrack
1212
- name: automatic merge on CI success if only markdown and/or Golang files were changed
1313
conditions:
14-
- "check-success=docker_test"
14+
- "check-success=merge_tests"
1515
- files~=.\.(?i)(md|go)$
1616
- base=main
1717
- -draft

.github/workflows/coverage-tests.yaml

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,45 @@ on:
55
pull_request:
66

77
jobs:
8-
docker_test:
9-
runs-on: ubuntu-latest
8+
coverage:
9+
name: Unit test and coverage
10+
11+
strategy:
12+
matrix:
13+
platform: [ubuntu-latest, macos-latest, windows-latest]
14+
15+
runs-on: ${{ matrix.platform }}
16+
1017
steps:
11-
- name: Checkout the code
12-
uses: actions/checkout@v2
13-
- name: Build the test image
18+
- uses: actions/checkout@v2
19+
20+
- uses: actions/setup-go@v2
21+
with:
22+
go-version: 1.15.x
23+
24+
- name: Use Cache
25+
uses: actions/cache@v2
26+
with:
27+
path: ~/go/pkg/mod
28+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
29+
restore-keys: |
30+
${{ runner.os }}-go-
31+
32+
- name: Download Modules
33+
if: steps.cache.outputs.cache-hit != 'true'
34+
run: go mod download
35+
36+
- name: Run coverage (Windows)
37+
if: runner.os == 'Windows'
1438
run: |
15-
pwd && \
16-
cd ./.devcontainer && \
17-
docker build -t test:ci .
18-
- name: Run the tests on the container
39+
go install
40+
go mod tidy
41+
go vet -v ./...
42+
go test -cover -v ./...
43+
44+
- name: Run coverage (Linux/macOS)
45+
if: runner.os != 'Windows'
1946
run: |
20-
pwd && \
21-
docker run \
22-
-v $(pwd):/workspaces/Hello-Cobra \
23-
-w /workspaces/Hello-Cobra \
24-
test:ci \
25-
/bin/bash ./.github/run-tests-coverage.sh --verbose
47+
go install
48+
go mod tidy
49+
/bin/bash ./.github/run-tests-coverage.sh --verbose

.github/workflows/golangci-lint.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name: golangci-lint
33

44
on:
55
workflow_dispatch:
6-
pull_request:
76

87
jobs:
98
golangci:

.github/workflows/merge-tests.yaml

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,53 @@ on:
55
pull_request:
66

77
jobs:
8-
docker_test:
8+
merge_tests:
99
runs-on: ubuntu-latest
1010
steps:
1111
- name: Checkout the code
1212
uses: actions/checkout@v2
13-
- name: Build the test image
13+
14+
- uses: actions/setup-go@v2
15+
with:
16+
go-version: 1.15.x
17+
18+
- name: Use Cache
19+
uses: actions/cache@v2
20+
with:
21+
path: ~/go/pkg/mod
22+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
23+
restore-keys: |
24+
${{ runner.os }}-go-
25+
26+
- name: Download Modules
27+
if: steps.cache.outputs.cache-hit != 'true'
28+
run: go mod download
29+
30+
- name: Run shellcheck (pre-installed)
1431
run: |
15-
pwd && \
16-
cd ./.devcontainer && \
17-
docker build -t test:ci .
18-
- name: Run full-tests on the container
32+
find . -name '*.sh' -type f -print0 | xargs -0 shellcheck
33+
34+
- name: Install and run shfmt
35+
run: |
36+
GO111MODULE=on go get mvdan.cc/sh/v3/cmd/shfmt
37+
find . -name '*.sh' -type f -print0 | xargs -0 shfmt -d
38+
39+
- name: Run gofmt
40+
uses: Jerome1337/[email protected]
41+
with:
42+
gofmt-path: '.'
43+
gofmt-flags: '-d -e' # display diffs and report all errors
44+
45+
- name: Run golangci-lint
46+
uses: golangci/golangci-lint-action@v2
47+
with:
48+
version: v1.36
49+
args: --config ./.github/golangci.yml
50+
# use pre-installed Go
51+
skip-go-installation: true
52+
53+
- name: Run requirement check
1954
run: |
20-
pwd && \
21-
docker run \
22-
-v $(pwd):/workspaces/Hello-Cobra \
23-
-w /workspaces/Hello-Cobra \
24-
test:ci \
25-
/bin/bash ./.github/run-tests-merge.sh
55+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
56+
./.github/check-requirements.sh
57+
./.github/run-tests-merge.sh

.shellcheckrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# =============================================================================
2+
# ShellCheck Configuration
3+
# =============================================================================
4+
# See: https://github.com/koalaman/shellcheck/wiki/
5+
6+
# Disable
7+
# SC2230: https://github.com/koalaman/shellcheck/wiki/SC2230
8+
disable=SC2230

bin/.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# We do not want to version control the built binary.
2-
# Though we need this directory for access control purposes.
1+
# Output directory of built/compiled application via "../build-app.sh"
2+
#
3+
# We do not want to version control the built binary. Though we need
4+
# this directory for access control purposes.
35
*
46
!.gitignore

build-app.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ GOARCH:
5353
For supported architectures specify '--list' option.
5454
5555
GOARM:
56-
The 3rd argument is the ARM version. Such as:
56+
The 3rd argument is the ARM variant/version. Such as:
5757
5858
"5", "6", "7".(Default: empty)
5959
@@ -69,20 +69,20 @@ Sample usage:
6969
./build-app.sh --list
7070
./build-app.sh -l
7171
72-
# Linux (Intel)
72+
# Build Linux (Intel) binary
7373
./build-app.sh linux
7474
75-
# macOS
75+
# Build macOS binary
7676
./build-app.sh darwin #Equivalent to: ./build-app.sh darwin amd64
7777
./build-app.sh darwin arm64
7878
79-
# Windows10
79+
# Build Windows10 binary
8080
./build-app.sh windows
8181
82-
# Raspberry Pi 3
82+
# Build Raspberry Pi 3 binary
8383
./build-app.sh linux arm 7
8484
85-
# QNAP ARM5
85+
# Build QNAP ARM5 binary
8686
./build-app linux arm 5
8787
8888
HEREDOC
@@ -104,7 +104,7 @@ listPlatforms() {
104104
echo "$list" | indentSTDIN 1>&2
105105
exit $FAILURE
106106
}
107-
echo 'List of available platforms. (OS/Architecture)'
107+
echo 'List of available platforms to build. (GOOS/GOARCH)'
108108
echo "$list" | indentSTDIN
109109
exit $SUCCESS
110110
}

0 commit comments

Comments
 (0)