-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.yaml
170 lines (162 loc) · 5.9 KB
/
action.yaml
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# !!!!!!!!!!!!!! This repository is public !!!!!!!!!!!!!!!!!!!!!!!!
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# repo is public until Github Action supports cloning private repos
# https://github.com/github/roadmap/issues/74
name: 'Run Kubernetes Job'
description: 'Create a K8s Job and tails its logs until it fails or complete'
inputs:
namespace: # id of input
description: 'namespace to use (can be also created and/or deleted after use)'
required: true
name:
description: 'job name to create in namespace'
required: true
image:
description: 'image to run the job on'
required: true
command:
description: command the job will run, can be "cmd arg" or "['cmd', 'arg']"
required: false
default: "[]"
createNamespace:
description: '[true/false] whether to create the namespace, default to false'
required: false
default: 'false'
deleteNamespace:
description: '[true/false] whether to delete the namespace if the job complete in time'
required: false
default: 'false'
timeoutMinuteStartContainer:
description: 'in minutes, how long to wait for pulling image and starting container, does not apply once the container is running. Use action timeout_minute to timeout the overall Job run'
required: false
default: "10"
backoffLimit:
description: 'how many times to retry running the Job'
required: false
default: "1"
requestsCPU:
description: 'how much CPU resources request from system to run this job'
required: true
default: "1"
requestsMEM:
description: 'how much MEM resources request from system to run this job'
required: true
default: 1Gi
limitCPU:
description: 'how much CPU resources this job can consume'
required: true
default: "2"
limitMEM:
description: 'how much MEM resources this job can consume'
required: true
default: 2Gi
runs:
using: "composite"
steps:
- name: "Check inputs"
shell: bash
run: |
echo " Check input:"
echo "namespace: ${{ inputs.namespace }}"
echo "name: ${{ inputs.name }}"
echo "image: ${{ inputs.image }}"
echo "command: ${{ inputs.command }}"
echo "createNamespace: ${{ inputs.createNamespace }}"
echo "deleteNamespace: ${{ inputs.cleanNamespace }}"
- name: "Enable Problem Matcher"
shell: bash
run: |
echo "::add-matcher::${{ github.action_path }}/.github/problem_matchers/eslint.json"
echo "::add-matcher::${{ github.action_path }}/.github/problem_matchers/stylelint.json"
- name: "Create Namespace"
shell: bash
run: |
[[ "${{ inputs.createNamespace }}" != "true" ]] && exit 0
echo " Re-create namespace"
kubectl delete namespace ${{ inputs.namespace }} || true
kubectl create namespace ${{ inputs.namespace }}
- name: "Create Job"
shell: bash
run: |
cat <<EOF > job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: ${{ inputs.name }}
namespace: ${{ inputs.namespace }}
spec:
backoffLimit: ${{ inputs.backoffLimit }}
completions: 1
parallelism: 1
template:
spec:
# don't restart Pod, prefer Job backofflimit (easier to keep track off and wont end in an endless restart loop)
restartPolicy: Never
tolerations:
- key: "ci-workloads-only"
operator: "Exists"
nodeSelector:
reserved-for: ci-workloads
volumes:
- name: v1
emptyDir: {}
containers:
- name: extractor
image: bash:5.0.18
command: [ "/usr/local/bin/bash", "-c", "--" ]
args: [ "touch /tmp/runfile; while [ -f /tmp/runfile ]; do sleep 5; done;" ]
volumeMounts:
- mountPath: /job_outputs
name: v1
- name: ${{ inputs.name }}
image: ${{ inputs.image }}
imagePullPolicy: Always
tty: true
resources:
limits:
cpu: "${{ inputs.limitCPU }}"
memory: ${{ inputs.limitMEM }}
requests:
cpu: "${{ inputs.requestsCPU }}"
memory: ${{ inputs.requestsMEM }}
command: ${{ inputs.command }}
volumeMounts:
- mountPath: /job_outputs
name: v1
env:
- name: "TERM"
value: "xterm-256color"
- name: "CI"
value: "true"
EOF
for e in $(printenv); do
IFS='=' read -r -a key_val <<< "$e"
name=${key_val[0]}
[[ "${name}" != _* ]] && continue
name=${name:1}
[[ "${name}" == "" ]] && continue
# indent has to match the above heredoc
cat <<EOF >> job.yaml
- name: "${name}"
value: "${key_val[1]}"
EOF
done
echo " Job to be created:"
cat job.yaml
echo " Creating Job ${{ inputs.name }}"
kubectl apply -f job.yaml
- name: "Handle Job"
shell: bash
run: |
python -u ${{ github.action_path }}/handlejob.py
env:
JOB_NAME: ${{ inputs.name }}
NAMESPACE: ${{ inputs.namespace }}
TIMEOUT_MINUTE_START_CONTAINER: ${{ inputs.timeoutMinuteStartContainer }}
- name: "Clean Resources"
shell: bash
run: |
[[ "${{ inputs.deleteNamespace }}" != "true" ]] && exit 0
echo " Delete namespace"
kubectl delete namespace ${{ inputs.namespace }} || true