2

I'm running Jenkins locally and this is the last stage of my Jenkinsfile (after following this tutorial):

stage('Deploy to K8s') {
    steps {
        sshagent(credentials: ['k8s_key']) {
            sh 'scp -r -o StrictHostKeyChecking=no localPath/deployment-remes-be.yaml <user>@<ip_address>:/opt/kubernetes-system/backend'
            script {
                try {
                    sh 'ssh <user>@<ip_address> kubectl apply -f /opt/kubernetes-system/backend/deployment-remes-be.yaml --kubeconfig=~/.kube/config'
                }
                catch(error) {

                }
            }
        }
    }
}

When I run the pipeline it completes without any blocking errors, but when I check the logs I can see this: enter image description here

The copy before the apply command is working. I have microk8s installed on the Debian server I'm trying to deploy to and if I run the apply command manually then it's working fine. I've created the .kube/config file as shown here but using the --kubeconfig file doesn't make any difference. It also doesn't matter if I use microk8s.kubectl, I always get this message.

I have these plugins installed:

enter image description here

What can I do here to make the apply work from the pipeline?

cs.kali
  • 198
  • 12
  • 1
    The error states that `kubectl` is not in the user's path, and so the first troubleshooting step would be to provide the absolute path to the executable. – Matthew Schuchard Jan 10 '23 at 13:33
  • You're right, I needed it like this `/snap/bin/microk8s.kubectl apply`. Pls write your comment as answer so I can accept it. Thanks – cs.kali Jan 10 '23 at 13:50

1 Answers1

2

In this situation where the error thrown is that the executable command is not found in the path, then the absolute path should be attempted as a first step. The shell step method can be updated accordingly:

sh 'ssh <user>@<ip_address> /path/to/kubectl apply -f /opt/kubernetes-system/backend/deployment-remes-be.yaml --kubeconfig=~/.kube/config'
Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67