forked from craftyc0der/github-action-eks-tkn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
110 lines (92 loc) · 2.93 KB
/
entrypoint.sh
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
#!/bin/sh
set -e
if [ -z "$INPUT_KUBECONFIG" ]; then
echo "INPUT_KUBECONFIG is not set. EKS will not be called."
else
if [ -z "$INPUT_AWS_ACCESS_KEY_ID" ]; then
echo "INPUT_AWS_ACCESS_KEY_ID is not set. Quitting."
exit 1
else
INPUT_AWS_ACCESS_KEY_ID=$(echo "${INPUT_AWS_ACCESS_KEY_ID}" | xargs)
fi
if [ -z "$INPUT_AWS_SECRET_ACCESS_KEY" ]; then
echo "INPUT_AWS_SECRET_ACCESS_KEY is not set. Quitting."
exit 1
else
INPUT_AWS_SECRET_ACCESS_KEY=$(echo "${INPUT_AWS_SECRET_ACCESS_KEY}" | xargs)
fi
# Default to us-east-1 if AWS_REGION not set.
if [ -z "$INPUT_AWS_REGION" ]; then
AWS_REGION="us-east-2"
fi
if [[ ! $INPUT_NAMESPACE =~ ^[-_a-zA-Z0-9]*$ ]]; then
echo "No special characters allowed in namespace"
exit 1
fi
if [ -z "$INPUT_SERVICEACCOUNT" ]; then
SAARG=""
else
if [[ ! $INPUT_SERVICEACCOUNT =~ ^[-_a-zA-Z0-9]*$ ]]; then
echo "No special characters allowed in serviceaccount"
exit 1
fi
SAARG="--serviceaccount ${INPUT_SERVICEACCOUNT}"
fi
if [ ! -z "$INPUT_TASK" ]
then
if [[ ! $INPUT_TASK =~ ^[-_a-zA-Z0-9]*$ ]]; then
echo "No special characters allowed in task name"
exit 1
fi
TASKTYPE="task"
else
if [[ ! $INPUT_CLUSTER_TASK =~ ^[-_a-zA-Z0-9]*$ ]]; then
echo "No special characters allowed in clustertask name"
exit 1
fi
if [ ! -z "$INPUT_CLUSTER_TASK" ]
then
TASKTYPE="clustertask"
INPUT_TASK=$INPUT_CLUSTER_TASK
else
echo "No task defined"
exit 1
fi
fi
if [[ ! $INPUT_ARGS =~ ^[-\.=[:space:]\:/a-zA-Z0-9]*$ ]]; then
echo "No special characters allowed in task arguments"
exit 1
fi
if [ -z "$INPUT_POD_TEMPLATE" ]; then
PTARG=""
else
echo "${INPUT_POD_TEMPLATE}" > /workdir/pod_template.yaml
PTARG="--pod-template /workdir/pod_template.yaml"
fi
# Create a dedicated profile for this action to avoid conflicts
# with past/future actions.
aws configure --profile github_user <<-EOF > /dev/null 2>&1
${INPUT_AWS_ACCESS_KEY_ID}
${INPUT_AWS_SECRET_ACCESS_KEY}
${INPUT_AWS_REGION}
text
EOF
echo -e "\033[36mSetting up kubectl configuration\033[0m"
mkdir -p ~/.kube/
echo "${INPUT_KUBECONFIG}" > ~/.kube/config
fi
echo -e "\033[36mExecuting tkn\033[0m"
TASKRUN_NAME=$(tkn ${TASKTYPE} start ${PTARG} ${SAARG} -n ${INPUT_NAMESPACE} ${INPUT_TASK} $INPUT_ARGS --output json | jq -r ".metadata | .name")
tkn taskrun logs -f ${TASKRUN_NAME} -n ${INPUT_NAMESPACE}
# confrirm that the run succeeded: https://tekton.dev/docs/pipelines/taskruns/
TASK_STATUS=$(kubectl get tr ${TASKRUN_NAME} -n ${INPUT_NAMESPACE} -o json | jq -r ".status | .conditions | .[] | .status")
TASK_REASON=$(kubectl get tr ${TASKRUN_NAME} -n ${INPUT_NAMESPACE} -o json | jq -r ".status | .conditions | .[] | .reason")
if [[ $TASK_STATUS != "True" ]] || [[ $TASK_REASON != "Succeeded" ]]; then
echo "Tekton Build Failed"
exit 1
fi
echo -e "\033[36mCleaning up: \033[0m"
rm ./run.sh -Rf
echo -e "\033[36m - exec ✅ \033[0m"
rm ~/.kube/config -Rf
echo -e "\033[36m - kubeconfig ✅ \033[0m"