Skip to content

Commit 570898e

Browse files
jdbaldrydsotirakis
andauthored
ci: add workflow that lints shell scripts with ShellCheck (#147)
* Add workflow that lints shell scripts with ShellCheck Signed-off-by: Jack Baldry <[email protected]> * Replace inline script with script file Signed-off-by: Jack Baldry <[email protected]> * Lint for style I've not bothered saving this patch for reapplication if upstream changes because we had already deviated from that before these changes. - Use consistent variable syntax - Prefer `-n` over `! -z` - Use appropriate quoting for words that have no variable expansion Signed-off-by: Jack Baldry <[email protected]> * Move to lint-shared-workflows action Signed-off-by: Jack Baldry <[email protected]> * Lint translate-secrets script - Remove useless echo subshell - Set readonly variables for to clarify when the script is no longer going to modify those variables. Signed-off-by: Jack Baldry <[email protected]> * Lint README for Grafana Labs style https://grafana.com/docs/writers-toolkit/ - Simplify some language for improved readability - Prefer [semantic line breaks](https://sembr.org/) for better line based diffing in the GitHub UI. Signed-off-by: Jack Baldry <[email protected]> * Document preference for separate shell scripts Signed-off-by: Jack Baldry <[email protected]> * Revert "Lint README for Grafana Labs style" This reverts commit 4278510. * Restore documentation dropped in conflict resolution Signed-off-by: Jack Baldry <[email protected]> * Clean up whitespace Signed-off-by: Jack Baldry <[email protected]> --------- Signed-off-by: Jack Baldry <[email protected]> Co-authored-by: Dimitris Sotirakis <[email protected]>
1 parent 6426ecd commit 570898e

File tree

6 files changed

+61
-28
lines changed

6 files changed

+61
-28
lines changed

.github/workflows/lint-shared-workflows.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ jobs:
3636
- name: Lint workflow files
3737
uses: raven-actions/actionlint@01fce4f43a270a612932cb1c64d40505a029f821 # v2.0.0
3838

39+
- name: Run ShellCheck on scripts
40+
uses: ludeeus/action-shellcheck@00cae500b08a931fb5698e11e79bfbd38e612a38 # v2.0.0
41+
3942
# A separate job so we can run in the `yq` container
4043
lint-action-yaml:
4144
name: Lint action YAMLs

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ will ensure actions in this repo are always used at the same commit. To do this:
6868
some-input: some-value
6969
```
7070
71+
### Use separate files for shell scripts so they're linted
72+
73+
Instead of embedding a shell script in the `run` string, write a separate script and refer to that.
74+
75+
For example, don't use the step:
76+
77+
```yaml
78+
id: echo-success
79+
shell: bash
80+
run: |
81+
echo "Success!"
82+
```
83+
84+
Instead, create the file `echo-success.bash` in the same directory and use the step:
85+
86+
```yaml
87+
id: echo-success
88+
shell: bash
89+
run: ./echo-success.bash
90+
```
91+
7192
### Releasing a version of a component in shared-workflows
7293

7394
When working with `shared-workflows`, it's essential to avoid breaking backwards compatibility. To ensure this, we must provide releasable actions for engineers to review incoming changes. This also helps automated update tools like `dependabot` and `renovate` to work effectively.

actions/aws-auth/action.yaml

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,9 @@ runs:
5757
chain-pass-claims: "${{ inputs.pass-claims }}"
5858
chain-set-in-environment: "${{ inputs.set-creds-in-environment }}"
5959

60-
- id: aws_region # Pulled from catnekaise/cognito-idpool-auth/action.yml
60+
- id: aws_region
6161
shell: bash
6262
env:
6363
AWS_REGION: "${{ inputs.aws-region }}"
6464
AWS_DEFAULT_REGION: "${{ inputs.aws-region }}"
65-
run: |
66-
value=""
67-
68-
if [ ! -z "${AWS_REGION}" ] && [ ! -z "${AWS_DEFAULT_REGION}" ]; then
69-
value="$AWS_REGION"
70-
fi
71-
72-
if [ -z "$value" ]; then
73-
echo "Unable to resolve what AWS Region to use"
74-
exit 1
75-
fi
76-
77-
# Some-effort validation of aws region
78-
if echo "$value" | grep -Eqv "^[a-z]{2}-[a-z]{4,9}-[0-9]$"; then
79-
echo "Resolved value for AWS Region is invalid"
80-
exit 1
81-
fi
82-
83-
echo "value=$value" >> "$GITHUB_OUTPUT"
84-
echo "AWS_REGION=${AWS_REGION}" >> "$GITHUB_ENV"
85-
echo "AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}" >> "$GITHUB_ENV"
65+
run: ./resolve-aws-region.sh
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/sh
2+
# Pulled from catnekaise/cognito-idpool-auth/action.yml
3+
# https://github.com/catnekaise/cognito-idpool-auth/blob/83ae9e159de469b3acd87ecb361d6b5957ee35ae/action.yml#L192-L227
4+
value=""
5+
6+
if [ -n "${AWS_REGION}" ] && [ -n "${AWS_DEFAULT_REGION}" ]; then
7+
value="$AWS_REGION"
8+
fi
9+
10+
readonly value
11+
12+
if [ -z "${value}" ]; then
13+
echo 'Unable to resolve what AWS region to use'
14+
exit 1
15+
fi
16+
17+
# Some-effort validation of aws region
18+
if echo "${value}" | grep -Eqv '^[a-z]{2}-[a-z]{4,9}-[0-9]$'; then
19+
echo 'Resolved value for AWS region is invalid'
20+
exit 1
21+
fi
22+
23+
echo "value=${value}" >> "${GITHUB_OUTPUT}"
24+
echo "AWS_REGION=${AWS_REGION}" >> "${GITHUB_ENV}"
25+
echo "AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}" >> "${GITHUB_ENV}"

actions/get-vault-secrets/translate-secrets.sh renamed to actions/get-vault-secrets/translate-secrets.bash

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

33
# Input env:
4-
# - REPO => Repository name
4+
# - REPO => Repository name
55
# - COMMON_SECRETS => Common secrets (in the ci/data/common/<path> vault path): {{ Env Variable Name }}={{ Secret Path }}:{{ Secret Key }}
66
# - REPO_SECRETS => Repo secrets (in the ci/data/repo/${REPO}/<path> vault path): {{ Env Variable Name }}={{ Secret Path }}:{{ Secret Key }}
77
# Output format: "{{ Secret Path }} {{ Secret Key }} | {{ Env Variable Name }}" in the $GITHUB_OUTPUT file
@@ -19,6 +19,8 @@ if [ -z "$GITHUB_OUTPUT" ]; then
1919
exit 1
2020
fi
2121

22+
readonly COMMON_SECRETS GITHUB_OUTPUT REPO REPO_SECRETS
23+
2224
RESULT=""
2325

2426
# Function to split a string into parts
@@ -43,18 +45,20 @@ split_string() {
4345
if [ -n "$COMMON_SECRETS" ]; then
4446
for common_secret in $COMMON_SECRETS; do
4547
split_string "$common_secret"
46-
RESULT="${RESULT}$(echo "ci/data/common/$secret_path $secret_key | $env_variable_name");\n"
48+
RESULT="${RESULT}ci/data/common/$secret_path $secret_key | $env_variable_name;\n"
4749
done
4850
fi
4951

5052
# Translate the repo secrets
5153
if [ -n "$REPO_SECRETS" ]; then
5254
for repo_secret in $REPO_SECRETS; do
5355
split_string "$repo_secret"
54-
RESULT="${RESULT}$(echo "ci/data/repo/$REPO/$secret_path $secret_key | $env_variable_name");\n"
56+
RESULT="${RESULT}ci/data/repo/$REPO/$secret_path $secret_key | $env_variable_name;\n"
5557
done
5658
fi
5759

60+
readonly RESULT
61+
5862
# Print the contents of the output file
5963
echo -e "Secrets that will be queried from Vault:\n$RESULT"
6064
echo -e "secrets<<EOF\n${RESULT}EOF" > "$GITHUB_OUTPUT"

actions/get-vault-secrets/translate-secrets.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ teardown() {
1818

1919
@test "Check if REPO environment variable is set" {
2020
export REPO=
21-
run ./translate-secrets.sh
21+
run ./translate-secrets.bash
2222
[ "$status" -ne 0 ]
2323
[ "${lines[0]}" = "Error: REPO environment variable is not set." ]
2424
}
2525

2626
@test "Check if GITHUB_OUTPUT environment variable is set" {
2727
export GITHUB_OUTPUT=
28-
run ./translate-secrets.sh
28+
run ./translate-secrets.bash
2929
[ "$status" -ne 0 ]
3030
[ "${lines[0]}" = "Error: GITHUB_OUTPUT environment variable is not set." ]
3131
}
3232

3333
@test "Translate secrets" {
34-
run ./translate-secrets.sh
34+
run ./translate-secrets.bash
3535
echo "$output" >&3
3636
[ "$status" -eq 0 ]
3737
[ "$output" = "Secrets that will be queried from Vault:

0 commit comments

Comments
 (0)