name: k3s on: push: branches: [ "main" ] workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Install kubectl env: K3S_TOKEN: ${{ secrets.K3S_TOKEN }} K3S_CERT: ${{ secrets.K3S_CERT }} shell: bash run: | # Fetch the latest stable version of kubectl KUBECTL_VERSION=$(curl -s https://dl.k8s.io/release/stable.txt) if [ -z "$KUBECTL_VERSION" ]; then echo "Failed to fetch the latest kubectl version. Exiting." exit 1 fi # Download kubectl binary curl -LO "https://dl.k8s.io/release/v1.31.0/bin/linux/amd64/kubectl" if [ $? -ne 0 ]; then echo "Failed to download kubectl binary. Exiting." exit 1 fi # Make kubectl executable and move to PATH chmod +x kubectl sudo mv kubectl /usr/local/bin/ # Verify kubectl installation kubectl version --client # Install kubelogin curl -fsSLO https://github.com/int128/kubelogin/releases/download/v1.25.4/kubelogin_linux_amd64.zip unzip kubelogin_linux_amd64.zip kubelogin -d kubelogin mv kubelogin/kubelogin kubelogin/kubectl-oidc_login export PATH=$PATH:$(realpath kubelogin) # Create the kubeconfig file with the K3S_TOKEN and K3S_CERT mkdir -p ~/.kube # Base64 decode the K3S_CERT and write it to a file echo "$K3S_CERT" | base64 --decode > ~/.kube/ca.crt # Create the kubeconfig file cat < ~/.kube/config apiVersion: v1 kind: Config clusters: - name: k3s-cluster cluster: certificate-authority: ~/.kube/ca.crt server: https://kubernetes.default.svc:6443 users: - name: k3s-user user: token: $K3S_TOKEN contexts: - name: k3s-context context: cluster: k3s-cluster user: k3s-user current-context: k3s-context EOF # Test the kubeconfig setup by printing the cluster info kubectl cluster-info # Apply Kubernetes configuration kubectl delete -f kubernetes.yaml kubectl apply -f kubernetes.yaml