Skip to content

Commit ac4e5c8

Browse files
committed
Initial workflow test
1 parent c13a914 commit ac4e5c8

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

.github/workflows/kubectl.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Run kubectl against remote cluster
2+
on:
3+
workflow_dispatch:
4+
push:
5+
branches:
6+
- "1-github-runner-manages-remote"
7+
jobs:
8+
deploy:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Install kubectl
12+
run: |
13+
mkdir $HOME/bin
14+
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
15+
chmod +x $HOME/bin/kubectl
16+
echo "$HOME/bin" >> $GITHUB_PATH
17+
18+
- name: Check kubectl is available on PATH
19+
run: kubectl version --client
20+
21+
- name: Checkout repo
22+
uses: actions/checkout@v4
23+
24+
- name: Set kubeconfig with kubectl
25+
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"
30+
31+
- name: Run kubectl command against remote API
32+
run: kubectl get namespaces

kubectl-gh-actions.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Steps
44

5-
1. Make the JWT token accessible to the GitHub runner
5+
1. Add secrets for the server address and JWT token
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
@@ -12,3 +12,66 @@
1212
- Settings -> Secrets and Variables -> Actions -> New Repository Secret
1313
- Set the name to `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
16+
17+
2. Access the secret in the action
18+
19+
- Github actions can access repository secrets using the syntax `${{ secrets.<secret> }}`
20+
- We'll create a step in our action that sets the correct kubeconfig
21+
22+
```yaml
23+
# Other steps... #
24+
25+
- name: Set kubeconfig with kubectl
26+
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"
31+
32+
# kubectl command steps ... #
33+
```
34+
35+
3. Create the full workflow
36+
37+
- So we need to:
38+
1. Make sure the `kubectl` binary is available
39+
2. Checkout the repo
40+
3. Configure authentication with kubectl
41+
4. Run `kubectl` commands against the remote API
42+
43+
```yaml
44+
# File: .github/workflows/kubectl.yaml
45+
46+
name: Run kubectl against remote cluster
47+
on:
48+
push:
49+
branches:
50+
- "1-github-runner-manages-remote"
51+
jobs:
52+
deploy:
53+
runs-on: ubuntu-latest
54+
steps:
55+
- name: Install kubectl
56+
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
59+
chmod +x $HOME/bin/kubectl
60+
echo "$HOME/bin" >> $GITHUB_PATH
61+
62+
- name: Check kubectl is available on PATH
63+
run: kubectl version --client
64+
65+
- name: Checkout repo
66+
uses: actions/checkout@v4
67+
68+
- name: Set kubeconfig with kubectl
69+
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"
74+
75+
- name: Run kubectl command against remote API
76+
run: kubectl get namespaces
77+
```

0 commit comments

Comments
 (0)