Skip to content
This repository was archived by the owner on Aug 4, 2025. It is now read-only.

Commit 2b4de5f

Browse files
fix: find command failing and other improvements (#3)
* feat: better dockerfile, ci and cd for automatic new shellcheck release and better scripts compatible with POSIX * fix: reviews * fix: better logical syntax * fix: added pipefail to script * Add bash back to dockerfile --------- Co-authored-by: Felipe Santos <felipecassiors@gmail.com>
1 parent 54f2ab8 commit 2b4de5f

5 files changed

Lines changed: 323 additions & 156 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ name: ci
22

33
on:
44
push:
5+
branches: [main]
56
tags: ['v*.*.*']
7+
pull_request:
8+
branches: [main]
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
12+
cancel-in-progress: true
613

714
jobs:
815
check:
@@ -24,15 +31,17 @@ jobs:
2431

2532
release:
2633
runs-on: ubuntu-latest
34+
env:
35+
VERSION: ${{ github.ref_name }}
2736
needs: check
28-
if: needs.check.result == 'success'
37+
if: needs.check.result == 'success' && startsWith(github.ref, 'refs/tags/')
2938
steps:
3039
- name: Checkout
3140
uses: actions/checkout@v3
41+
with:
42+
fetch-depth: 0
3243
- name: Set up Docker Buildx
3344
uses: docker/setup-buildx-action@v2
34-
- name: Extract VERSION from tag
35-
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
3645
- name: Build packages
3746
uses: docker/bake-action@v2
3847
- name: Create GitHub release

.github/workflows/new.yaml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: new
2+
3+
on:
4+
schedule:
5+
- cron: '0 0 * * *'
6+
7+
jobs:
8+
tag:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v3
13+
14+
- name: ShellCheck latest release
15+
id: shellcheck
16+
run: |
17+
_release=$(
18+
curl \
19+
--fail \
20+
--silent \
21+
--location \
22+
--show-error \
23+
https://api.github.com/repos/koalaman/shellcheck/releases/latest \
24+
| jq \
25+
--raw-output \
26+
'.tag_name'
27+
)
28+
29+
printf '{release}={%s}\n' "$_release" >> "$GITHUB_OUTPUT"
30+
31+
- name: ShellCheck binaries latest release
32+
id: shellcheck_binaries
33+
run: |
34+
_release=$(
35+
curl \
36+
--fail \
37+
--silent \
38+
--location \
39+
--show-error \
40+
https://api.github.com/repos/vscode-shellcheck/shellcheck-binaries/releases/latest \
41+
| jq \
42+
--raw-output \
43+
'.tag_name'
44+
)
45+
46+
printf '{release}={%s}\n' "$_release" >> "$GITHUB_OUTPUT"
47+
48+
- name: Shellcheck homebrew container image exist
49+
id: shellcheck_homebrew
50+
run: |
51+
_release_exist=$(
52+
curl \
53+
--fail \
54+
--silent \
55+
--location \
56+
--show-error \
57+
--header "Authorization: Bearer $(printf '%s\n' "${{ secrets.GITHUB_TOKEN }}" | base64)" \
58+
https://ghcr.io/v2/homebrew/core/shellcheck/tags/list \
59+
| jq \
60+
--raw-output \
61+
--arg release "$(printf '%s\n' "${{ steps.shellcheck.outputs.release }}" | sed 's/^.\{1\}//')" \
62+
'any(.tags[]; . == $release)'
63+
)
64+
65+
printf '{release_exist}={%s}\n' "$_release_exist" >> "$GITHUB_OUTPUT"
66+
67+
- name: Tag ${{ steps.shellcheck.outputs.release }}
68+
uses: rickstaa/action-create-tag@v1
69+
if: steps.shellcheck.outputs.release != steps.shellcheck_binaries.outputs.release && steps.shellcheck_homebrew.outputs.release_exist == 'true'
70+
with:
71+
tag: ${{ steps.shellcheck.outputs.release }}

Dockerfile

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
11
# syntax=docker/dockerfile:1
22

3+
# ================
4+
# CONFIGURATION
5+
# ================
6+
# ShellCheck version
37
ARG VERSION
48

5-
FROM --platform=darwin/arm64 ghcr.io/homebrew/core/shellcheck:${VERSION} AS darwin-arm64
9+
# ================
10+
# DARWIN ARM64
11+
# ================
12+
FROM --platform=darwin/arm64 ghcr.io/homebrew/core/shellcheck:$VERSION AS darwin-arm64
613

14+
# ================
15+
# ARCHIVES
16+
# ================
717
FROM alpine AS archives
18+
ARG VERSION
819

9-
RUN apk add --no-cache bash curl tar unzip xz
20+
# Install packages
21+
RUN apk add --no-cache \
22+
bash \
23+
findutils \
24+
tar \
25+
unzip \
26+
xz
1027

11-
COPY scripts/download_and_archive.sh /scripts/download_and_archive.sh
28+
# Copy Darwin arm64 binary
29+
COPY --from=darwin-arm64 shellcheck/$VERSION/bin/shellcheck /shellcheck.darwin.arm64.data/
1230

13-
ARG VERSION
14-
COPY --from=darwin-arm64 shellcheck/${VERSION}/bin/shellcheck /shellcheck.darwin.arm64.data/
31+
# Copy scripts directory
32+
COPY scripts /scripts
33+
34+
# Execute script
1535
RUN /scripts/download_and_archive.sh
1636

37+
# ================
38+
# SCRATCH
39+
# ================
1740
FROM scratch
1841

42+
# Copy archives directory
1943
COPY --from=archives /archives/ /

scripts/__commons.sh

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# shellcheck shell=bash
2+
3+
# ================
4+
# GLOBALS
5+
# ================
6+
# Downloader
7+
DOWNLOADER=
8+
9+
# ================
10+
# LOGGER
11+
# ================
12+
# Fatal log level. Cause exit failure
13+
LOG_LEVEL_FATAL=100
14+
# Error log level
15+
LOG_LEVEL_ERROR=200
16+
# Warning log level
17+
LOG_LEVEL_WARN=300
18+
# Informational log level
19+
LOG_LEVEL_INFO=500
20+
# Debug log level
21+
LOG_LEVEL_DEBUG=600
22+
# Log level
23+
LOG_LEVEL=$LOG_LEVEL_INFO
24+
25+
# Print log message
26+
# @param $1 Log level
27+
# @param $2 Message
28+
_log_print_message() {
29+
log_level=${1:-LOG_LEVEL_FATAL}
30+
shift
31+
log_level_name=
32+
log_message=${*:-}
33+
34+
# Check log level
35+
[ "$log_level" -le "$LOG_LEVEL" ] || return 0
36+
37+
case $log_level in
38+
"$LOG_LEVEL_FATAL")
39+
log_level_name=FATAL
40+
;;
41+
"$LOG_LEVEL_ERROR")
42+
log_level_name=ERROR
43+
;;
44+
"$LOG_LEVEL_WARN")
45+
log_level_name=WARN
46+
;;
47+
"$LOG_LEVEL_INFO")
48+
log_level_name=INFO
49+
;;
50+
"$LOG_LEVEL_DEBUG")
51+
log_level_name=DEBUG
52+
;;
53+
esac
54+
55+
# Log
56+
printf '[%-5s] %b\n' "$log_level_name" "$log_message"
57+
}
58+
59+
# Fatal log message
60+
# @param $1 Message
61+
FATAL() {
62+
_log_print_message "$LOG_LEVEL_FATAL" "$@" >&2
63+
exit 1
64+
}
65+
66+
# Error log message
67+
# @param $1 Message
68+
ERROR() { _log_print_message "$LOG_LEVEL_ERROR" "$@" >&2; }
69+
70+
# Warning log message
71+
# @param $1 Message
72+
WARN() { _log_print_message "$LOG_LEVEL_WARN" "$@" >&2; }
73+
74+
# Informational log message
75+
# @param $1 Message
76+
INFO() { _log_print_message "$LOG_LEVEL_INFO" "$@"; }
77+
78+
# Debug log message
79+
# @param $1 Message
80+
DEBUG() { _log_print_message "$LOG_LEVEL_DEBUG" "$@"; }
81+
82+
# ================
83+
# ASSERT
84+
# ================
85+
# Assert command is installed
86+
# @param $1 Command name
87+
assert_cmd() {
88+
check_cmd "$1" || FATAL "Command '$1' not found"
89+
DEBUG "Command '$1' found at '$(command -v "$1")'"
90+
}
91+
92+
# Assert executable downloader
93+
assert_downloader() {
94+
[ -z "$DOWNLOADER" ] || return 0
95+
96+
_assert_downloader() {
97+
# Return failure if it doesn't exist or is no executable
98+
[ -x "$(command -v "$1")" ] || return 1
99+
100+
# Set downloader
101+
DOWNLOADER=$1
102+
return 0
103+
}
104+
105+
# Downloader command
106+
_assert_downloader "curl" \
107+
|| _assert_downloader "wget" \
108+
|| FATAL "No executable downloader found: 'curl' or 'wget'"
109+
DEBUG "Downloader '$DOWNLOADER' found at '$(command -v "$DOWNLOADER")'"
110+
}
111+
112+
# ================
113+
# FUNCTIONS
114+
# ================
115+
# Check command is installed
116+
# @param $1 Command name
117+
check_cmd() {
118+
command -v "$1" > /dev/null 2>&1
119+
}
120+
121+
# Download a file
122+
# @param $1 Output location
123+
# @param $2 Download URL
124+
download() {
125+
assert_downloader
126+
127+
# Download
128+
INFO "Downloading file '$2' to '$1'"
129+
case $DOWNLOADER in
130+
curl)
131+
curl --fail --silent --location --show-error --output "$1" "$2" || FATAL "Download file '$2' failed"
132+
;;
133+
wget)
134+
wget --quiet --output-document="$1" "$2" || FATAL "Download file '$2' failed"
135+
;;
136+
*) FATAL "Unknown downloader '$DOWNLOADER'" ;;
137+
esac
138+
DEBUG "Successfully downloaded file '$2' to '$1'"
139+
}

0 commit comments

Comments
 (0)