forked from padok-team/github-actions-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-kubeconfig.sh
More file actions
executable file
·42 lines (33 loc) · 1.81 KB
/
Copy pathgenerate-kubeconfig.sh
File metadata and controls
executable file
·42 lines (33 loc) · 1.81 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
#!/usr/bin/env bash
set -e
### These are the parameters you can set when calling this script:
NAMESPACE="${NAMESPACE:-default}"
###
echo "⏳ Fetching service account credentials..."
SA_SECRET_NAME=$(kubectl get serviceaccount github-actions --namespace "${NAMESPACE}" --output go-template='{{ (index .secrets 0).name }}')
echo "✅ Service account credentials fetched."
echo
echo "⏳ Adding Kubernetes API server to kubectl configuration..."
KUBECONFIG_SERVER=$(kubectl config view --minify --output go-template='{{ (index .clusters 0).cluster.server }}')
kubectl get secret $SA_SECRET_NAME --namespace "${NAMESPACE}" --output go-template='{{ index .data "ca.crt" }}' | base64 --decode > /tmp/kubeconfig-ca.crt
kubectl --kubeconfig /tmp/kubeconfig.yml config set-cluster production --server=$KUBECONFIG_SERVER --certificate-authority /tmp/kubeconfig-ca.crt --embed-certs=true
rm /tmp/kubeconfig-ca.crt
echo "✅ Kubernetes API server added."
echo
echo "⏳ Adding authentication token to kubectl configuration..."
KUBECONFIG_TOKEN=$(kubectl get secret $SA_SECRET_NAME --namespace "${NAMESPACE}" --output go-template='{{ .data.token }}' | base64 --decode)
kubectl --kubeconfig /tmp/kubeconfig.yml config set-credentials github-actions --token $KUBECONFIG_TOKEN
kubectl --kubeconfig /tmp/kubeconfig.yml config set-context github-actions-production --cluster production --user github-actions --namespace "${NAMESPACE}"
kubectl --kubeconfig /tmp/kubeconfig.yml config use-context github-actions-production
echo "✅ Authentication token added."
echo
echo "⏳ Converting configuration to base64..."
KUBECONFIG_B64="$(base64 --input /tmp/kubeconfig.yml)"
rm /tmp/kubeconfig.yml
echo "✅ Configuration converted."
echo
echo "👌 Configuration file ready!"
echo "👇 Use the following value for your GitHub secret:"
echo
echo "${KUBECONFIG_B64}"
echo