Skip to content

Commit f0d46e7

Browse files
authored
Refactor shell scripts (#141)
## Description This is the second part of the refactoring effort to ensure that the shell scripts: - Follow a consistent convention, - Pass the ShellCheck linting test (please run `make shellscript-lint-all`), and - Work either with the pre-installed CLI tooling or run commands using Docker containers. ## Context Information for a reviewer on how to test the scripts with the `scan-secrets.sh` as an example: Install the CLI tool ``` # On macOS brew install gitleaks # On Ubuntu apt install gitleaks ``` Run a test ``` ./scripts/githooks/scan-secrets.sh VERBOSE=1 ./scripts/githooks/scan-secrets.sh # Use the gitleaks CLI tool if installed VERBOSE=1 FORCE_USE_DOCKER=1 ./scripts/githooks/scan-secrets.sh # Use Docker ``` ## Type of changes - [x] Refactoring (non-breaking change) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would change existing functionality) - [ ] Bug fix (non-breaking change which fixes an issue) ## Checklist - [x] I am familiar with the [contributing guidelines](../docs/CONTRIBUTING.md) - [x] I have followed the code style of the project - [ ] I have added tests to cover my changes - [ ] I have updated the documentation accordingly - [ ] This PR is a result of pair or mob programming --- ## Sensitive Information Declaration To ensure the utmost confidentiality and protect your and others privacy, we kindly ask you to NOT including [PII (Personal Identifiable Information) / PID (Personal Identifiable Data)](https://digital.nhs.uk/data-and-information/keeping-data-safe-and-benefitting-the-public) or any other sensitive data in this PR (Pull Request) and the codebase changes. We will remove any PR that do contain any sensitive information. We really appreciate your cooperation in this matter. - [x] I confirm that neither PII/PID nor sensitive data are included in this PR and the codebase changes.
1 parent 773ff6e commit f0d46e7

24 files changed

+256
-140
lines changed

.github/actions/cloc-repository/action.yaml renamed to .github/actions/create-lines-of-code-report/action.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ runs:
2626
shell: bash
2727
run: |
2828
export BUILD_DATETIME=${{ inputs.build_datetime }}
29-
./scripts/reports/cloc-repository.sh
29+
./scripts/reports/create-lines-of-code-report.sh
3030
- name: "Compress CLOC report"
3131
shell: bash
32-
run: zip cloc-report.json.zip cloc-report.json
32+
run: zip lines-of-code-report.json.zip lines-of-code-report.json
3333
- name: "Upload CLOC report as an artefact"
3434
if: ${{ !env.ACT }}
3535
uses: actions/upload-artifact@v3
3636
with:
37-
name: cloc-report.json.zip
38-
path: ./cloc-report.json.zip
37+
name: lines-of-code-report.json.zip
38+
path: ./lines-of-code-report.json.zip
3939
retention-days: 21
4040
- name: "Check prerequisites for sending the report"
4141
shell: bash
@@ -53,5 +53,5 @@ runs:
5353
if: steps.check.outputs.secrets_exist == 'true'
5454
run: |
5555
aws s3 cp \
56-
./cloc-report.json.zip \
57-
${{ inputs.idp_aws_report_upload_bucket_endpoint }}/${{ inputs.build_timestamp }}-cloc-report.json.zip
56+
./lines-of-code-report.json.zip \
57+
${{ inputs.idp_aws_report_upload_bucket_endpoint }}/${{ inputs.build_timestamp }}-lines-of-code-report.json.zip

.github/actions/scan-dependencies/action.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ runs:
2626
shell: bash
2727
run: |
2828
export BUILD_DATETIME=${{ inputs.build_datetime }}
29-
./scripts/reports/generate-sbom.sh
29+
./scripts/reports/create-sbom-report.sh
3030
- name: "Compress SBOM report"
3131
shell: bash
3232
run: zip sbom-repository-report.json.zip sbom-repository-report.json

.github/workflows/stage-1-commit.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575
uses: actions/checkout@v4
7676
- name: "Lint Terraform"
7777
uses: ./.github/actions/lint-terraform
78-
cloc-repository:
78+
count-lines-of-code:
7979
name: "Count lines of code"
8080
runs-on: ubuntu-latest
8181
permissions:
@@ -86,7 +86,7 @@ jobs:
8686
- name: "Checkout code"
8787
uses: actions/checkout@v4
8888
- name: "Count lines of code"
89-
uses: ./.github/actions/cloc-repository
89+
uses: ./.github/actions/create-lines-of-code-report
9090
with:
9191
build_datetime: "${{ inputs.build_datetime }}"
9292
build_timestamp: "${{ inputs.build_timestamp }}"

.tool-versions

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ pre-commit 3.4.0
77
# The section below is reserved for Docker image versions.
88

99
# TODO: Move this section - consider using a different file for the repository template dependencies.
10+
# docker/ghcr.io/anchore/grype v0.69.1@sha256:d41fcb371d0af59f311e72123dff46900ebd6d0482391b5a830853ee4f9d1a76 # SEE: https://github.com/anchore/grype/pkgs/container/grype
11+
# docker/ghcr.io/anchore/syft v0.92.0@sha256:63c60f0a21efb13e80aa1359ab243e49213b6cc2d7e0f8179da38e6913b997e0 # SEE: https://github.com/anchore/syft/pkgs/container/syft
1012
# docker/ghcr.io/gitleaks/gitleaks v8.18.0@sha256:fd2b5cab12b563d2cc538b14631764a1c25577780e3b7dba71657d58da45d9d9 # SEE: https://github.com/gitleaks/gitleaks/pkgs/container/gitleaks
13+
# docker/ghcr.io/igorshubovych/markdownlint-cli v0.37.0@sha256:fb3e79946fce78e1cde84d6798c6c2a55f2de11fc16606a40d49411e281d950d # SEE: https://github.com/igorshubovych/markdownlint-cli/pkgs/container/markdownlint-cli
14+
# docker/ghcr.io/make-ops-tools/gocloc latest@sha256:6888e62e9ae693c4ebcfed9f1d86c70fd083868acb8815fe44b561b9a73b5032 # SEE: https://github.com/make-ops-tools/gocloc/pkgs/container/gocloc
1115
# docker/ghcr.io/nhs-england-tools/github-runner-image 20230909-321fd1e-rt@sha256:ce4fd6035dc450a50d3cbafb4986d60e77cb49a71ab60a053bb1b9518139a646 # SEE: https://github.com/nhs-england-tools/github-runner-image/pkgs/container/github-runner-image
1216
# docker/hadolint/hadolint 2.12.0-alpine@sha256:7dba9a9f1a0350f6d021fb2f6f88900998a4fb0aaf8e4330aa8c38544f04db42 # SEE: https://hub.docker.com/r/hadolint/hadolint/tags
1317
# docker/hashicorp/terraform 1.5.6@sha256:180a7efa983386a27b43657ed610e9deed9e6c3848d54f9ea9b6cb8a5c8c25f5 # SEE: https://hub.docker.com/r/hashicorp/terraform/tags
1418
# docker/koalaman/shellcheck latest@sha256:e40388688bae0fcffdddb7e4dea49b900c18933b452add0930654b2dea3e7d5c # SEE: https://hub.docker.com/r/koalaman/shellcheck/tags
19+
# docker/mstruebing/editorconfig-checker 2.7.1@sha256:dd3ca9ea50ef4518efe9be018d669ef9cf937f6bb5cfe2ef84ff2a620b5ddc24 # SEE: https://hub.docker.com/r/mstruebing/editorconfig-checker/tags
1520
# docker/sonarsource/sonar-scanner-cli 5.0.1@sha256:494ecc3b5b1ee1625bd377b3905c4284e4f0cc155cff397805a244dee1c7d575 # SEE: https://hub.docker.com/r/sonarsource/sonar-scanner-cli/tags

docs/developer-guides/Bash_and_Make.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ VERBOSE=1 scripts/shellscript-linter.sh
140140
141141
### Scripts
142142
143-
Most scripts provided with this repository template can utilise tools installed on your `PATH` if they are available or run them from within a Docker container. To force a script to use Docker, the `FORCE_USE_DOCKER` variable is provided. Here is an example of how to use it:
143+
Most scripts provided with this repository template can utilise tools installed on your `PATH` if they are available or run them from within a Docker container. To force a script to use Docker, the `FORCE_USE_DOCKER` variable is provided. This feature increases configurability of the development environment, allowing you to use custom tooling by default if present on the command-line path. Here is an example of how to use it:
144144
145145
```shell
146146
FORCE_USE_DOCKER=1 scripts/shellscript-linter.sh

docs/user-guides/Scan_dependencies.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ In modern software development, leveraging third-party dependencies is a common
1515

1616
## Key files
1717

18-
- [generate-sbom.sh](../../scripts/reports/generate-sbom.sh): A shell script that generates SBOM (Software Bill of Materials)
18+
- [create-sbom-report.sh](../../scripts/reports/create-sbom-report.sh): A shell script that generates SBOM (Software Bill of Materials)
1919
- [syft.yaml](../../scripts/config/syft.yaml): A configuration file for the SBOM generator
2020
- [scan-vulnerabilities.sh](../../scripts/reports/scan-vulnerabilities.sh): A shell script that performs CVE analysis
2121
- [grype.yaml](../../scripts/config/grype.yaml): A configuration file for the CVE scanner
@@ -41,7 +41,7 @@ You can run and test the process locally on a developer's workstation using the
4141
SBOM generator
4242

4343
```shell
44-
./scripts/reports/generate-sbom.sh
44+
./scripts/reports/create-sbom-report.sh
4545
cat sbom-repository-report.json | jq
4646
```
4747

docs/user-guides/Test_GitHub_Actions_locally.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The following command-line tools are expected to be installed:
2828
Here is an example on how to run a GitHub workflow job:
2929

3030
```shell
31-
$ make runner-act workflow="stage-1-commit" job="cloc-repository"
31+
$ make runner-act workflow="stage-1-commit" job="create-lines-of-code-report"
3232

3333
[Commit stage/Count lines of code] 🚀 Start image=ghcr.io/nhs-england-tools/github-runner-image:20230101-abcdef0-rt
3434
[Commit stage/Count lines of code] 🐳 docker pull image=ghcr.io/nhs-england-tools/github-runner-image:20230101-abcdef0-rt platform=linux/amd64 username= forcePull=false
@@ -42,7 +42,7 @@ $ make runner-act workflow="stage-1-commit" job="cloc-repository"
4242
[Commit stage/Count lines of code] ✅ Success - Main Create CLOC report
4343
[Commit stage/Count lines of code] ⭐ Run Main Compress CLOC report
4444
[Commit stage/Count lines of code] 🐳 docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/1-composite-1.sh] user= workdir=
45-
| updating: cloc-report.json (deflated 68%)
45+
| updating: lines-of-code-report.json (deflated 68%)
4646
[Commit stage/Count lines of code] ✅ Success - Main Compress CLOC report
4747
[Commit stage/Count lines of code] ☁ git clone 'https://github.com/actions/upload-artifact' # ref=v3
4848
[Commit stage/Count lines of code] ⭐ Run Main Check prerequisites for sending the report

scripts/docker/docker.lib.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ function docker-build() {
2727

2828
version-create-effective-file
2929
_create-effective-dockerfile
30-
# The current directory must be changed for the image build script to access
31-
# assets that need to be copied
30+
# The current directory must be changed for the image build script to access
31+
# assets that need to be copied
3232
current_dir=$(pwd)
3333
cd "$dir"
3434
docker build \
@@ -164,7 +164,7 @@ function docker-get-image-version-and-pull() {
164164
# digest="sha256:hash"
165165

166166
# Get the image full version from the '.tool-versions' file,
167-
# match it by name and version regex, if given.
167+
# match it by name and version regex, if given.
168168
local versions_file="${TOOL_VERSIONS:=$(git rev-parse --show-toplevel)/.tool-versions}"
169169
local version="latest"
170170
if [ -f "$versions_file" ]; then

scripts/docker/dockerfile-linter.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ set -euo pipefail
88
# otherwise it will run it in a Docker container.
99
#
1010
# Usage:
11-
# $ ./dockerfile-linter.sh
11+
# $ [options] ./dockerfile-linter.sh
1212
#
1313
# Arguments (provided as environment variables):
1414
# file=Dockerfile # Path to the Dockerfile to lint, relative to the project's top-level directory, default is './Dockerfile.effective'
15-
# VERBOSE=true # Show all the executed commands, default is 'false'
1615
# FORCE_USE_DOCKER=true # If set to true the command is run in a Docker container, default is 'false'
16+
# VERBOSE=true # Show all the executed commands, default is 'false'
1717

1818
# ==============================================================================
1919

@@ -23,16 +23,16 @@ function main() {
2323

2424
local file=${file:-./Dockerfile.effective}
2525
if command -v hadolint > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
26-
file="$file" cli-run-hadolint
26+
file="$file" run-hadolint-natively
2727
else
28-
file="$file" docker-run-hadolint
28+
file="$file" run-hadolint-in-docker
2929
fi
3030
}
3131

3232
# Run hadolint natively.
3333
# Arguments (provided as environment variables):
3434
# file=[path to the Dockerfile to lint, relative to the project's top-level directory]
35-
function cli-run-hadolint() {
35+
function run-hadolint-natively() {
3636

3737
# shellcheck disable=SC2001
3838
hadolint "$(echo "$file" | sed "s#$PWD#.#")"
@@ -41,7 +41,7 @@ function cli-run-hadolint() {
4141
# Run hadolint in a Docker container.
4242
# Arguments (provided as environment variables):
4343
# file=[path to the Dockerfile to lint, relative to the project's top-level directory]
44-
function docker-run-hadolint() {
44+
function run-hadolint-in-docker() {
4545

4646
# shellcheck disable=SC1091
4747
source ./scripts/docker/docker.lib.sh

scripts/docker/tests/docker.test.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ function test-docker-build() {
7373

7474
function test-docker-image-from-signature() {
7575

76-
# Arrange
76+
# Arrange
7777
TOOL_VERSIONS="$(git rev-parse --show-toplevel)/scripts/docker/tests/.tool-versions.test"
7878
cp Dockerfile Dockerfile.effective
79-
# Act
79+
# Act
8080
_replace-image-latest-by-specific-version
81-
# Assert
81+
# Assert
8282
grep -q "FROM python:.*-alpine.*@sha256:.*" Dockerfile.effective && return 0 || return 1
8383
}
8484

0 commit comments

Comments
 (0)