|
2 | 2 |
|
3 | 3 | # Steps |
4 | 4 |
|
5 | | -1. Make the JWT token accessible to the GitHub runner |
| 5 | +1. Add secrets for the server address and JWT token |
6 | 6 |
|
7 | 7 | - Note that generated JWT tokens are relatively short-lived, but you can extend their validity by passing `--duration=<timespan>` to `kubectl create token` |
8 | 8 | - e.g. `kubectl create-token remote-dev --duration=12h` for a token valid for 12 hours |
|
12 | 12 | - Settings -> Secrets and Variables -> Actions -> New Repository Secret |
13 | 13 | - Set the name to `JWT_AUTH_TOKEN` |
14 | 14 | - 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