-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yaml
More file actions
51 lines (41 loc) · 1.43 KB
/
action.yaml
File metadata and controls
51 lines (41 loc) · 1.43 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
name: Regex Extract
description: 'Reusable action to run a command and extract substrings for an arbitrary list of regex patterns.'
inputs:
command:
description: 'The shell command to execute.'
required: true
regex-inputs:
description: |
New line **or** comma separated list of regex patterns.
required: true
outputs:
matches:
value: ${{ steps.extract.outputs.matches }}
description: 'A JSON array (single line) containing the extracted substrings in the same order the patterns were supplied.'
runs:
using: composite
steps:
- name: Run command and extract substrings
id: extract
shell: bash
run: |
set -euo pipefail
OUTPUT=$(eval "${{ inputs.command }}")
echo "Command output: $OUTPUT"
mapfile -t PATTERNS < <(printf '%s\n' "${{ inputs.regex-inputs }}" | tr ',' '\n' | sed '/^[[:space:]]*$/d')
if [[ ${#PATTERNS[@]} -eq 0 ]]; then
echo "No regex patterns provided." >&2
exit 1
fi
declare -a MATCHES=()
for PAT in "${PATTERNS[@]}"; do
if [[ $OUTPUT =~ $PAT ]]; then
MATCHES+=("${BASH_REMATCH[1]}")
else
echo "Pattern did not match: $PAT" >&2
exit 1
fi
done
JSON_MATCHES=$(printf '%s\n' "${MATCHES[@]}" | jq -R . | jq -cs .)
echo "Extracted matches: $JSON_MATCHES"
echo "matches=$JSON_MATCHES" >> "$GITHUB_OUTPUT"