Kubectl on jenkinsfile

Posted June 14, 2020 1.7k views

Hi everyone,

i’m able to execute kubectl over command line in linux, but i get error bellow when running the same command in jenkinsfile. Can anybody help me, please?

Error: Unable to initialize DigitalOcean API client: access token is required. (hint: run ‘doctl auth init’)

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

Hi there @carlosbenicio79,

It is quite possible that your Jenkins service is running as a different user so it does not have access to the Kubernetes config file of your SSH user.

What you could do is add an extra step in your Jenkinsfile to first authenticate with doctl, it would look something like this:

doctl auth init --access-token "your_digitalocean_token"

I would recommend adding your DigitalOcean access token as a Jenkins secret rather than hardcoding it in your Jenkinsfile.

And then add another step to save the Kubernetes config:

doctl kubernetes cluster kubeconfig save use_your_cluster_name

Hope that this helps!

Hi @bobbyiliev

Thank you for answering me.

I solved it changing the approaching by using kubernetes plugin.