Kubernetes fails inside jenkins pipeline - jenkins

I'm trying to run kubectl commands inside jenkins pipeline but they are failing. Outside in powershell window they work fine but in the pipeline, they show this when doing:
kubectl cluster-info --v=99
I've tried adding --token $TOKEN (jwt generated) following some other thread's recommendation but didn't work. Anyone know why this is happening and any way to bypass it? All these commands work fine when ran outside the jenkins pipeline.

The problem was jenkins actually uses a different home directory and so even if your kubectl work in command line, it won't run if jenkins runs it from the pipeline as it doesn't have access to the credentials from the user directory.
So find your .kube config folder, usually in C:/users/ and then copy and paste that folder in the $JENKINS_HOME directory. The jenkins home directory can vary depending on how you installed it (for windows installers, it gets put in an obscure location inside System32). Once done, then jenkins will have access to the same certificates you use natively to run kubectl commands and it will have full access.

Related

Pass binary from Jenkins host to agent

Can you pass a binary from a Jenkins host to an agent?
I've got Jenkins running in Kubernetes, and the terraform plugin installed on my Jenkins master with the binary located at /var/jenkins_home/tools/org.jenkinsci.plugins.terraform.TerraformInstallation/terraform/terraform
I would like to pass this to my Jenkins agent by configuring my pod template and mounting the host volume path /var/jenkins_home/tools/org.jenkinsci.plugins.terraform.TerraformInstallation/terraform/terraform to the agent's path /usr/bin/terraform
But this doesn't seem to work as expected
When I exec into the agent and run a terraform version I get the error bash: terraform: command not found indicating that it doesn't have the binary.
I can see a terraform directory mounted in /usr/bin but without the binary. What I expect is for terraform to be installed on the agent. But my thinking might be incorrect here.
Is it possible to do this, has anyone has any experience with this?
As a #David Maze mentioned binary from Jenkins needs to be manually installed on every node, which can be a difficult to manage. However you can set Jenkins to run pipeline steps inside a container where the image contains the tools you need, which simplifies such case.
Read more: execution-env-jenkins.
One alternative is to use the slaves setup plugin. We use it to install and configure internal tools (and end) on nodes bases on labels. A log less hassle than #Malgorata's (and our previous) manually copy approach
Not sure how well it works with Kubernetes as not in our configuration.

Running a shell script in a container inside OpenShift after the container is up and running

I have a minishift setup running in my local and now I brought a tomcat container up with few war files in it. Now I have a script in /usr/local/tomcat/bin folder. I need to run the script and I am posting a curl request to fetch something from the war deployed.
Issue1: I am not able to use post deployment lifecycle hook as it just creates a copy of the pod and the container is still not started, so I cannot make use of the war deployed to access the app service.
Let me know if there is a way to execute the script inside the container.
I am trying with tasks and cronjob, let me know if that will work.
Thanks in advance.

Problem in using ansible plugin in jenkins

I installed Ansible plugin in Jenkins and I configured.
I created ansible.cfg, hosts and the playbook files.
I pushed those files with directory in Bitbucket.
The issue is when I start a build in Jenkins it says "skipping: no hosts matched" or I have already tested the directory manually and it works.
This is jenkins configuration:
This the error message:
When i execute the ansible-playbook command directly from the folder in Desktop it works.
This is when i executed the command in the jenkins directory
I think you need to provide a credentials owner. When you run it manually you are identified (with an attached ssh key) but Jenkins needs some permissions.

Run ansible using Jenkins

I created a job in jenkins and I want to build the project using ansible. I want to run my command on several host (that's why I use ansible). When I try to run the project it fails with some permission error:
/home/ubuntu/install.sh -s -U ubuntu -f 5
FATAL: command execution failed
java.io.IOException: Cannot run program "/usr/bin/ansible" (in directory "/var/lib/jenkins/jobs/Standard Demo/workspace"): error=13, Permission denied
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at hudson.Proc$LocalProc.<init>(Proc.java:244)
at hudson.Proc$LocalProc.<init>(Proc.java:216)
Do you know what the problem is? I am logged into the jenkins server as admin user.
This is not an Ansible problem, it is a configuration issue in Jenkins. As others have noted, by default Jenkins will run as a "normal user" (typically jenkins). That is the user that jobs and steps (including shell scripts like the one you're calling) will run as. In your case, this user does not have sufficient permissions to run Ansible.
I don't recommend changing this default user because a. there are good security reasons for this setup, and b. it can actually be complex to do right, because you would have to address permissions issues for all of Jenkins to match the new user. However, it's quite easy to do things like run sudo from within a Script build step. Just use that tool (and a properly configured /etc/sudoers) to gain the permissions you need during the build.

Jenkins user has root as home directory

I'm running jenkins in a virtual machine on google cloud platform. The VM came from bitnami.
My problem is that if I run a job which executes a bash script and from that script I try to access ~ it returns /
The user my jobs are run as is tomcat. Tomcats home directory is /home/tomcat as you would expect. If I login as tomcat and cd to ~ it will take me to the correct place /home/tomcat.
Why is the home directory of tomcat different when running a job from jenkins than it is if I just login normally?
Have you tried executing
whoami
in a job?
Perhaps Jenkins is run by a different functional user than tomcat.
Have you checked the root dir for anything suspicious that would indicate that it is somehow used? Finally, how do you exactly check the home dir in a job?

Resources