-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaction.sh
More file actions
executable file
·72 lines (63 loc) · 2.05 KB
/
action.sh
File metadata and controls
executable file
·72 lines (63 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bash
set -Eeuo pipefail
# Detect platform
if [[ "$(uname -s)" != "Linux" ]]; then
echo "This action currently supports Ubuntu-linux runners only."; exit 1
fi
# Install terrafetch
echo "::group::Install terrafetch"
ver="${INPUT_TERRAFETCH_VERSION}"
if [[ "$ver" == "latest" ]]; then ver=""; fi
curl -1sLf https://dl.cloudsmith.io/public/rosesecurity/terrafetch/setup.deb.sh \
| sudo -E bash
sudo apt-get -qq update
sudo apt-get -qq install -y "terrafetch${ver:+=$ver}"
echo "::endgroup::"
# Run terrafetch
echo "::group::Run terrafetch"
set +e
TERRAFETCH_OUTPUT="$(terrafetch -d "$INPUT_TERRAFORM_DIRECTORY")"
ret=$?
set -e
echo "terrafetch finished with exit code $ret"
echo "terrafetch-return-code=$ret" >>"$GITHUB_OUTPUT"
[[ $ret -ne 0 ]] && exit "$ret" # fail the step/job on error
echo "::endgroup::"
# Inject output into README
outfile="$INPUT_OUTPUT_FILE"
tmp=$(mktemp)
# Ensure the input is lowercase
COLLAPSE=$(printf '%s' "$INPUT_COLLAPSE_OUTPUT" | tr '[:upper:]' '[:lower:]')
if [[ "$COLLAPSE" = true ]]; then
block_md=$(printf '%s\n' \
'<details><summary>Terrafetch</summary>' \
'' \
'```console' \
"$TERRAFETCH_OUTPUT" \
'```' \
'</details>')
else
block_md=$(printf '%s\n' \
'```console' \
"$TERRAFETCH_OUTPUT" \
'```')
fi
awk -v block="$block_md" '
BEGIN {inside = 0}
/<!-- TERRAFETCH:START -->/ {print; print block; inside = 1; next}
/<!-- TERRAFETCH:END -->/ {inside = 0}
!inside {print}
' "$outfile" >"$tmp"
mv "$tmp" "$outfile"
# Commit when file changes
echo "::group::Commit & push (if needed)"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
if git diff --quiet "$outfile"; then
echo "No changes detected – skipping commit."
else
git add "$outfile"
git commit -m "chore: update Terrafetch section"
git push "https://${INPUT_GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" HEAD:$(git rev-parse --abbrev-ref HEAD)
fi
echo "::endgroup::"