-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
85 lines (79 loc) · 3.25 KB
/
action.yml
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
73
74
75
76
77
78
79
80
81
82
83
84
85
---
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions
name: Kubernetes port-forward
author: '@vbem'
description: Forward local ports to a workload in Kubernetes
branding:
icon: truck
color: green
inputs:
workload:
description: 'Kubernetes workload `type/name` such as `deploy/mydeploy`, `svc/mysvc` or `po/mypod`'
required: true
mappings:
description: 'Ports mappings `[LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]` (default as `8080:80`)'
required: false
default: '8080:80'
options:
description: 'Other command-line options, such as `--namespace=myns`'
required: false
default: ''
sleep:
description: 'Seconds to wait before action finished (default as `3`)'
required: false
default: '3'
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#outputs-for-composite-actions
outputs:
pid:
description: 'Process ID of port-forward daemon'
value: ${{ steps.main.outputs.pid }}
log:
description: 'Path to port-forward log file'
value: ${{ steps.main.outputs.log }}
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-actions
runs:
using: composite
steps:
- id: main
shell: bash
run: |
# implementation details
# Group stdin to stderr with title
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines
# $1: group title
# stdin: logs
# stderr: grouped logs
# $?: 0 if successful and non-zero otherwise
function kit::wf::group {
echo "::group::$1" >&2
echo "$(< /dev/stdin)" >&2
echo '::endgroup::' >&2
}
# Set stdin as value to output of current step with given name
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter
# https://renehernandez.io/snippets/multiline-strings-as-a-job-output-in-github-actions/
# $1: output name
# $2: masked value in logs
# stdin: output value
# stderr: grouped logs
# $?: 0 if successful and non-zero otherwise
function kit::wf::output {
local val
val="$(< /dev/stdin)"
{ # https://www.gnu.org/software/bash/manual/bash.html#Command-Grouping
echo "$1<<__GITHUB_OUTPUT__"
echo "$val"
echo '__GITHUB_OUTPUT__'
} >> "$GITHUB_OUTPUT"
kit::wf::group "🖨️ step output '$1' has been set" <<< "${2:-$val}"
}
PORT_FORWARD_LOG="${{ runner.temp }}/k8s-pod-forward.$RANDOM.log"
kit::wf::group '🚢 Run backgroup port-forward daemon' \
<<<"kubectl port-forward '${{ inputs.workload }}' ${{ inputs.mappings }} ${{ inputs.options }}"
nohup kubectl port-forward '${{ inputs.workload }}' ${{ inputs.mappings }} ${{ inputs.options }} \
< /dev/null &> "$PORT_FORWARD_LOG" &
kit::wf::output 'pid' <<< "$!"
kit::wf::output 'log' <<< "$PORT_FORWARD_LOG"
sleep '${{ inputs.sleep }}'
kit::wf::group '🚢 Current port-forward log' < "$PORT_FORWARD_LOG"
...