Skip to content

Commit ed99904

Browse files
committed
Factor out cluster info into repo variables
1 parent 7b009e8 commit ed99904

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

.github/workflows/kubectl.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Run kubectl against remote cluster
22
on:
3-
workflow_dispatch:
3+
workflow_dispatch: # Allows manual start of workflows
44
push:
55
branches:
66
- "1-github-runner-manages-remote"
@@ -23,14 +23,10 @@ jobs:
2323

2424
- name: Set kubeconfig with kubectl
2525
run: |
26-
kubectl config set-cluster "minikube" --server "${{ secrets.API_SERVER_ADDR }}"
27-
kubectl config set-credentials "remote-dev" --token "${{ secrets.JWT_AUTH_TOKEN }}"
28-
kubectl config set-context "remote-context" --cluster "minikube" --user "remote-dev"
29-
kubectl config use-context "remote-context"
26+
kubectl config set-cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --server "${{ secrets.KUBE_API_SERVER_ADDR }}"
27+
kubectl config set-credentials "${{ vars.KUBE_REMOTE_USER }}" --token "${{ secrets.KUBE_JWT_AUTH_TOKEN }}"
28+
kubectl config set-context "${{ vars.KUBE_REMOTE_CONTEXT }}" --cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --user "${{ vars.KUBE_REMOTE_USER }}"
29+
kubectl config use-context "${{ vars.KUBE_REMOTE_CONTEXT }}"
3030
31-
- name: Check kubectl has authenticated/authorized access to the remote API
31+
- name: Run kubectl command against remote API
3232
run: kubectl get namespaces
33-
34-
- name: kubectl apply with a file
35-
run: |
36-
kubectl apply -f "${GITHUB_WORKSPACE}/manifests/nginx-test.yml"

kubectl-gh-actions.md

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,46 @@
22

33
# Steps
44

5-
1. Add secrets for the server address and JWT token
5+
1. Add secrets for the server address and JWT token, and other variables
66

77
- Note that generated JWT tokens are relatively short-lived, but you can extend their validity by passing `--duration=<timespan>` to `kubectl create token`
88
- e.g. `kubectl create-token remote-dev --duration=12h` for a token valid for 12 hours
99
- We probably don't want to use these in production, your kubernetes provider (e.g. EKS) may offer a better means of authentication
1010

1111
- On the webpage for your repo:
1212
- Settings -> Secrets and Variables -> Actions -> New Repository Secret
13-
- Set the name to `JWT_AUTH_TOKEN`
13+
- Set the name to `KUBE_JWT_AUTH_TOKEN`
1414
- Set the value to the JWT token you generated
15-
- Add another secret called `API_SERVER_ADDR` with the value of your public-facing API server address
15+
- Add another secret called `KUBE_API_SERVER_ADDR` with the value of your public-facing API server address
1616

17-
2. Access the secret in the action
17+
- We'll also add some variables for the cluster, remote username, and remote context
18+
- On the webpage for your repo:
19+
- Settings -> Secrets and Variables -> Actions -> Variables -> New Repository Variable
20+
- Add three variables with the names and values:
21+
- KUBE_REMOTE_CLUSTER = minikube
22+
- KUBE_REMOTE_USER = remote-dev
23+
- KUBE_REMOTE_CONTEXT = remote-context
24+
25+
2. Access the secrets and variables in the action
1826

19-
- Github actions can access repository secrets using the syntax `${{ secrets.<secret> }}`
27+
- Github actions can access repository secrets using the syntax `${{ secrets.<secret> }}` and variables with `${{ vars.<variable> }}`
2028
- We'll create a step in our action that sets the correct kubeconfig
2129

2230
```yaml
2331
# Other steps... #
2432

2533
- name: Set kubeconfig with kubectl
2634
run: |
27-
kubectl config set-cluster "minikube" --server "${{ secrets.API_SERVER_ADDR }}"
28-
kubectl config set-credentials "remote-dev" --token "${{ secrets.JWT_AUTH_TOKEN }}"
29-
kubectl config set-context "remote-context" --cluster "minikube" --user "remote-dev"
30-
kubectl config use-context "remote-context"
35+
kubectl config set-cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --server "${{ secrets.KUBE_API_SERVER_ADDR }}"
36+
kubectl config set-credentials "${{ vars.KUBE_REMOTE_USER }}" --token "${{ secrets.KUBE_JWT_AUTH_TOKEN }}"
37+
kubectl config set-context "${{ vars.KUBE_REMOTE_CONTEXT }}" --cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --user "${{ vars.KUBE_REMOTE_USER }}"
38+
kubectl config use-context "${{ vars.KUBE_REMOTE_CONTEXT }}"
3139
3240
# kubectl command steps ... #
3341
```
3442

43+
- Using these variables and secrets makes it easier to update them in the future, without modifying the workflow file directly
44+
3545
3. Create the full workflow
3646

3747
- So we need to:
@@ -45,6 +55,7 @@
4555

4656
name: Run kubectl against remote cluster
4757
on:
58+
workflow_dispatch: # Allows manual start of workflows
4859
push:
4960
branches:
5061
- "main"
@@ -54,8 +65,8 @@
5465
steps:
5566
- name: Install kubectl
5667
run: |
57-
mkdir $HOME/bin
58-
curl -Lf 'https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl' -o $HOME/bin/kubectl
68+
mkdir "$HOME/bin"
69+
curl -Lf "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" -o "$HOME/bin/kubectl"
5970
chmod +x $HOME/bin/kubectl
6071
echo "$HOME/bin" >> $GITHUB_PATH
6172
@@ -67,10 +78,10 @@
6778

6879
- name: Set kubeconfig with kubectl
6980
run: |
70-
kubectl config set-cluster "minikube" --server "${{ secrets.API_SERVER_ADDR }}"
71-
kubectl config set-credentials "remote-dev" --token "${{ secrets.JWT_AUTH_TOKEN }}"
72-
kubectl config set-context "remote-context" --cluster "minikube" --user "remote-dev"
73-
kubectl config use-context "remote-context"
81+
kubectl config set-cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --server "${{ secrets.KUBE_API_SERVER_ADDR }}"
82+
kubectl config set-credentials "${{ vars.KUBE_REMOTE_USER }}" --token "${{ secrets.KUBE_JWT_AUTH_TOKEN }}"
83+
kubectl config set-context "${{ vars.KUBE_REMOTE_CONTEXT }}" --cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --user "${{ vars.KUBE_REMOTE_USER }}"
84+
kubectl config use-context "${{ vars.KUBE_REMOTE_CONTEXT }}"
7485
7586
- name: Run kubectl command against remote API
7687
run: kubectl get namespaces
@@ -83,6 +94,5 @@
8394
# previous setup steps #
8495
8596
- name: kubectl apply with a file
86-
run: |
87-
kubectl apply -f "${GITHUB_WORKSPACE}/manifests/nginx-test.yml"
97+
run: kubectl apply -f "${GITHUB_WORKSPACE}/manifests/nginx-test.yml"
8898
```

0 commit comments

Comments
 (0)