Skip to content

Commit 6a17c44

Browse files
committed
feat(workflows): enhance CI/CD pipeline with metadata and line counting
1 parent 2195e04 commit 6a17c44

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: "Count lines of code"
2+
description: "Count lines of code"
3+
inputs:
4+
build_datetime:
5+
description: "Build datetime, set by the CI/CD pipeline workflow"
6+
required: true
7+
build_timestamp:
8+
description: "Build timestamp, set by the CI/CD pipeline workflow"
9+
required: true
10+
runs:
11+
using: "composite"
12+
steps:
13+
- name: "Create CLOC report"
14+
shell: bash
15+
run: |
16+
export BUILD_DATETIME=${{ inputs.build_datetime }}
17+
./scripts/reports/create-lines-of-code-report.sh
18+
- name: "Compress CLOC report"
19+
shell: bash
20+
run: zip lines-of-code-report.json.zip lines-of-code-report.json
21+
- name: "Upload CLOC report as an artefact"
22+
if: ${{ !env.ACT }}
23+
uses: actions/upload-artifact@v4
24+
with:
25+
name: lines-of-code-report.json.zip
26+
path: ./lines-of-code-report.json.zip
27+
retention-days: 21

.github/workflows/cicd-1-pull-request.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,27 @@ on:
88
types: [opened, reopened, synchronize]
99

1010
jobs:
11+
metadata:
12+
name: "Set CI/CD metadata"
13+
runs-on: ubuntu-latest
14+
timeout-minutes: 1
15+
outputs:
16+
build_datetime: ${{ steps.variables.outputs.build_datetime }}
17+
build_timestamp: ${{ steps.variables.outputs.build_timestamp }}
18+
steps:
19+
- name: "Checkout code"
20+
uses: actions/checkout@v6
21+
- name: "Set CI/CD variables"
22+
id: variables
23+
run: |
24+
datetime=$(date -u +'%Y-%m-%dT%H:%M:%S%z')
25+
echo "build_datetime=$datetime" >> $GITHUB_OUTPUT
26+
echo "build_timestamp=$(date --date=$datetime -u +'%Y%m%d%H%M%S')" >> $GITHUB_OUTPUT
1127
commit-stage:
1228
name: "Commit stage"
29+
needs: [metadata]
1330
uses: ./.github/workflows/stage-1-commit.yaml
31+
with:
32+
build_datetime: "${{ needs.metadata.outputs.build_datetime }}"
33+
build_timestamp: "${{ needs.metadata.outputs.build_timestamp }}"
1434
secrets: inherit

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ name: "Commit stage"
22

33
on:
44
workflow_call:
5+
inputs:
6+
build_datetime:
7+
description: "Build datetime, set by the CI/CD pipeline workflow"
8+
required: true
9+
type: string
10+
build_timestamp:
11+
description: "Build timestamp, set by the CI/CD pipeline workflow"
12+
required: true
13+
type: string
514

615
jobs:
716
scan-secrets:
@@ -48,3 +57,18 @@ jobs:
4857
fetch-depth: 0 # Full history is needed to compare branches
4958
- name: "Check Markdown links"
5059
uses: ./.github/actions/check-markdown-links
60+
count-lines-of-code:
61+
name: "Count lines of code"
62+
runs-on: ubuntu-latest
63+
permissions:
64+
id-token: write
65+
contents: read
66+
timeout-minutes: 2
67+
steps:
68+
- name: "Checkout code"
69+
uses: actions/checkout@v6
70+
- name: "Count lines of code"
71+
uses: ./.github/actions/create-lines-of-code-report
72+
with:
73+
build_datetime: "${{ inputs.build_datetime }}"
74+
build_timestamp: "${{ inputs.build_timestamp }}"

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ pre-commit 4.5.1
88

99
# docker/ghcr.io/gitleaks/gitleaks v8.30.0@sha256:691af3c7c5a48b16f187ce3446d5f194838f91238f27270ed36eef6359a574d9 # SEE: https://github.com/gitleaks/gitleaks/pkgs/container/gitleaks
1010
# docker/ghcr.io/igorshubovych/markdownlint-cli v0.47.0@sha256:9f06c8c9a75aa08b87b235b66d618f7df351f09f08faf703177f670e38ee6511 # SEE: https://github.com/igorshubovych/markdownlint-cli/pkgs/container/markdownlint-cli
11+
# docker/ghcr.io/make-ops-tools/gocloc latest@sha256:6888e62e9ae693c4ebcfed9f1d86c70fd083868acb8815fe44b561b9a73b5032 # SEE: https://github.com/make-ops-tools/gocloc/pkgs/container/gocloc
1112
# docker/lycheeverse/lychee 0.22.0@sha256:ea5b34df7f5b1ea32a029027234e158af77ba1e3ae96e7554c3942d56de7cc13 # SEE: https://hub.docker.com/r/lycheeverse/lychee
1213
# docker/mstruebing/editorconfig-checker 3.6.0@sha256:3fee6fe35c9ec21367b92ca50b3bea856ffc8a7a186044c329ac8bb23b33284a # SEE: https://hub.docker.com/r/mstruebing/editorconfig-checker
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/bin/bash
2+
3+
# WARNING: Please, DO NOT edit this file! It is maintained in the Repository Template (https://github.com/nhs-england-tools/repository-template). Raise a PR instead.
4+
5+
set -euo pipefail
6+
7+
# Count lines of code of this repository. This is a gocloc command wrapper. It
8+
# will run gocloc natively if it is installed, otherwise it will run it in a
9+
# Docker container.
10+
#
11+
# Usage:
12+
# $ [options] ./create-lines-of-code-report.sh
13+
#
14+
# Options:
15+
# BUILD_DATETIME=%Y-%m-%dT%H:%M:%S%z # Build datetime, default is `date -u +'%Y-%m-%dT%H:%M:%S%z'`
16+
# FORCE_USE_DOCKER=true # If set to true the command is run in a Docker container, default is 'false'
17+
# VERBOSE=true # Show all the executed commands, default is `false`
18+
19+
# ==============================================================================
20+
21+
function main() {
22+
23+
cd "$(git rev-parse --show-toplevel)"
24+
25+
create-report
26+
enrich-report
27+
}
28+
29+
function create-report() {
30+
31+
if command -v gocloc > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
32+
run-gocloc-natively
33+
else
34+
run-gocloc-in-docker
35+
fi
36+
# shellcheck disable=SC2002
37+
cat lines-of-code-report.tmp.json \
38+
| jq -r '["Language","files","blank","comment","code"],["--------"],(.languages[]|[.name,.files,.blank,.comment,.code]),["-----"],(.total|["TOTAL",.files,.blank,.comment,.code])|@tsv' \
39+
| sed 's/Plain Text/Plaintext/g' \
40+
| column -t
41+
}
42+
43+
function run-gocloc-natively() {
44+
45+
gocloc --output-type=json . > lines-of-code-report.tmp.json
46+
}
47+
48+
function run-gocloc-in-docker() {
49+
50+
# shellcheck disable=SC1091
51+
source ./scripts/docker/docker.lib.sh
52+
53+
# shellcheck disable=SC2155
54+
local image=$(name=ghcr.io/make-ops-tools/gocloc docker-get-image-version-and-pull)
55+
docker run --rm --platform linux/amd64 \
56+
--volume "$PWD":/workdir \
57+
"$image" \
58+
--output-type=json \
59+
. \
60+
> lines-of-code-report.tmp.json
61+
}
62+
63+
function enrich-report() {
64+
65+
build_datetime=${BUILD_DATETIME:-$(date -u +'%Y-%m-%dT%H:%M:%S%z')}
66+
git_url=$(git config --get remote.origin.url)
67+
git_branch=$(git rev-parse --abbrev-ref HEAD)
68+
git_commit_hash=$(git rev-parse HEAD)
69+
git_tags=$(echo \""$(git tag | tr '\n' ',' | sed 's/,$//' | sed 's/,/","/g')"\" | sed 's/""//g')
70+
pipeline_run_id=${GITHUB_RUN_ID:-0}
71+
pipeline_run_number=${GITHUB_RUN_NUMBER:-0}
72+
pipeline_run_attempt=${GITHUB_RUN_ATTEMPT:-0}
73+
74+
# shellcheck disable=SC2086
75+
jq \
76+
'.creationInfo |= . + {"created":"'${build_datetime}'","repository":{"url":"'${git_url}'","branch":"'${git_branch}'","tags":['${git_tags}'],"commitHash":"'${git_commit_hash}'"},"pipeline":{"id":'${pipeline_run_id}',"number":'${pipeline_run_number}',"attempt":'${pipeline_run_attempt}'}}' \
77+
lines-of-code-report.tmp.json \
78+
> lines-of-code-report.json
79+
rm -f lines-of-code-report.tmp.json
80+
}
81+
82+
# ==============================================================================
83+
84+
function is-arg-true() {
85+
86+
if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
87+
return 0
88+
else
89+
return 1
90+
fi
91+
}
92+
93+
# ==============================================================================
94+
95+
is-arg-true "${VERBOSE:-false}" && set -x
96+
97+
main "$@"
98+
99+
exit 0

0 commit comments

Comments
 (0)