-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
128 lines (113 loc) · 4.26 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
name: OpenShift CLI (oc) Login and Runner
description: Action for running oc commands
branding:
icon: package
color: blue
inputs:
### Required
oc_namespace:
description: OpenShift namespace; e.g. abc123-dev
required: true
pattern: '^[a-z0-9]{6}-(dev|test|prod|tools)$'
oc_server:
description: OpenShift server; e.g. https://api.silver.devops.gov.bc.ca:6443
required: true
pattern: '^https:\/\/.*:6443$'
oc_token:
description: OpenShift access token
required: true
minLength: 32
triggers:
description: Paths used to trigger this action; e.g. ('./backend/' './frontend/)
### Typical / recommended
commands:
description: Command to run, generally oc commands; e.g. oc whoami
cronjob:
description: Cronjob to run, watch and report on
### Usually a bad idea / not recommended
cronjob_tail:
description: Number of cronjob log lines to tail; use -1 for all
default: 0
cronjob_timeout:
description: Timeout for cronjob to run; e.g. 5m
default: 5m
pattern: '^[0-9]+[mhs]$'
diff_branch:
description: Branch to diff against
default: ${{ github.event.repository.default_branch }}
oc_version:
description: Override oc version, >= 4.0; e.g. 4.14
default: ''
pattern: '^4\.[0-9]+$'
repository:
description: Optionally, specify a different repo to clone
default: ${{ github.repository }}
outputs:
triggered:
description: Whether the action was triggered
value: ${{ steps.diff.outputs.triggered }}
permissions: {}
runs:
using: composite
steps:
# Send triggers to diff action
- id: diff
uses: bcgov/[email protected]
with:
triggers: ${{ inputs.triggers }}
diff_branch: ${{ inputs.diff_branch }}
- if: steps.diff.outputs.triggered == 'true'
env:
OC: ${{ inputs.oc_version || '4.14' }}
shell: bash
working-directory: /usr/local/bin
run: |
# Install CLI Tool and Login
if ! command -v oc &> /dev/null; then
URL="https://mirror.openshift.com/pub/openshift-v4/clients/ocp/stable-${OC}/openshift-client-linux.tar.gz"
(wget "${URL}" -qcO - | tar -xzvf - oc) || !! || { echo "Failed to download oc client"; exit 1; }
fi
# OpenShift login
oc login --server=${{ inputs.oc_server }} --token=$( curl -ksX POST \
${{ inputs.oc_server }}/api/v1/namespaces/${{ inputs.oc_namespace }}/serviceaccounts/pipeline/token \
--header "Authorization: Bearer ${{ inputs.oc_token }}" \
--header "Content-Type: application/json; charset=utf-8" \
--data '{"spec": {"expirationSeconds": 600}}' \
| jq -r '.status.token' \
) || { echo "Failed to obtain service account token"; exit 1; }
# Verify namespace
if [ "$( oc project -q )" != "${{ inputs.oc_namespace }}" ]; then
echo "Project and token do not match!"
exit 1
fi
- if: steps.diff.outputs.triggered == 'true'
uses: actions/checkout@v4
with:
repository: ${{ inputs.repository }}
- if: steps.diff.outputs.triggered == 'true' && inputs.commands
shell: bash
run: |
# Run command(s)
${{ inputs.commands }}
- if: steps.diff.outputs.triggered == 'true' && inputs.cronjob
shell: bash
run: |
JOB_NAME=${{ inputs.cronjob }}--$(date +"%Y-%m-%d--%H-%M-%S")
echo "JOB_NAME=${JOB_NAME}" >> $GITHUB_ENV
oc create job ${JOB_NAME} --from=cronjob/${{ inputs.cronjob }}
oc wait --for jsonpath='{.status.phase}'=Succeeded pod --selector=job-name=${JOB_NAME} \
--timeout=${{ inputs.cronjob_timeout }}
echo "Job successful!"
# Provide logs if requested, even on fail
- if: always() && steps.diff.outputs.triggered == 'true' && inputs.cronjob
shell: bash
run: |
if [ "${{ inputs.cronjob_logs }}" != "false" ]; then
echo -e "\n\n--- Logs ---\n\n"
oc logs --tail=${{ inputs.cronjob_tail }} --selector=job-name=${JOB_NAME}
echo -e "\n\n---\n\n"
fi
# Action repo needs to be present for cleanup/tests
- if: steps.diff.outputs.triggered == 'true' && github.repository != inputs.repository
name: Checkout local repo to make sure action.yml is present
uses: actions/checkout@v4