|
| 1 | +- Assuming you followed [manual-remote-access.md](./manual-remote-access.md), you should now have remote access to your minikube cluster set up |
| 2 | + |
| 3 | +# Steps |
| 4 | + |
| 5 | +1. Add secrets for the server address and JWT token, and other variables |
| 6 | + |
| 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 | + - e.g. `kubectl create-token remote-dev --duration=12h` for a token valid for 12 hours |
| 9 | + - We probably don't want to use these in production, your kubernetes provider (e.g. EKS) may offer a better means of authentication |
| 10 | + |
| 11 | + - On the webpage for your repo: |
| 12 | + - Settings -> Secrets and Variables -> Actions -> New Repository Secret |
| 13 | + - Set the name to `KUBE_JWT_AUTH_TOKEN` |
| 14 | + - Set the value to the JWT token you generated |
| 15 | + - Add another secret called `KUBE_API_SERVER_ADDR` with the value of your public-facing API server address |
| 16 | + |
| 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 |
| 26 | + |
| 27 | + - Github actions can access repository secrets using the syntax `${{ secrets.<secret> }}` and variables with `${{ vars.<variable> }}` |
| 28 | + - We'll create a step in our action that sets the correct kubeconfig |
| 29 | + |
| 30 | + ```yaml |
| 31 | + # Other steps... # |
| 32 | + |
| 33 | + - name: Set kubeconfig with kubectl |
| 34 | + run: | |
| 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 }}" |
| 39 | +
|
| 40 | + # kubectl command steps ... # |
| 41 | + ``` |
| 42 | + |
| 43 | + - Using these variables and secrets makes it easier to update them in the future, without modifying the workflow file directly |
| 44 | + |
| 45 | +3. Create the full workflow |
| 46 | + |
| 47 | + - So we need to: |
| 48 | + 1. Make sure the `kubectl` binary is available |
| 49 | + 2. Checkout the repo |
| 50 | + 3. Configure authentication with kubectl |
| 51 | + 4. Run `kubectl` commands against the remote API |
| 52 | + |
| 53 | + ```yaml |
| 54 | + # File: .github/workflows/kubectl.yaml |
| 55 | + |
| 56 | + name: Run kubectl against remote cluster |
| 57 | + on: |
| 58 | + workflow_dispatch: # Allows manual start of workflows |
| 59 | + push: |
| 60 | + branches: |
| 61 | + - "main" |
| 62 | + jobs: |
| 63 | + deploy: |
| 64 | + runs-on: ubuntu-latest |
| 65 | + steps: |
| 66 | + - name: Install kubectl |
| 67 | + run: | |
| 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" |
| 70 | + chmod +x $HOME/bin/kubectl |
| 71 | + echo "$HOME/bin" >> $GITHUB_PATH |
| 72 | +
|
| 73 | + - name: Check kubectl is available on PATH |
| 74 | + run: kubectl version --client |
| 75 | + |
| 76 | + - name: Checkout repo |
| 77 | + uses: actions/checkout@v4 |
| 78 | + |
| 79 | + - name: Set kubeconfig with kubectl |
| 80 | + run: | |
| 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 }}" |
| 85 | +
|
| 86 | + - name: Run kubectl command against remote API |
| 87 | + run: kubectl get namespaces |
| 88 | + ``` |
| 89 | +
|
| 90 | + - With this, we have remote access to the API in according with the RBAC rules we created earlier |
| 91 | + - If you wanted to `kubectl apply -f` in this action, you could do so like below: |
| 92 | + |
| 93 | + ```yaml |
| 94 | + # previous setup steps # |
| 95 | +
|
| 96 | + - name: kubectl apply with a file |
| 97 | + run: kubectl apply -f "${GITHUB_WORKSPACE}/manifests/nginx-test.yml" |
| 98 | + ``` |
0 commit comments