Search code examples
jenkinsjenkins-pluginsjenkins-pipelinejenkins-cli

Jenkins pipeline job gets triggered as anonymous but not as an user/Admin


Jenkins Pipeline job doesn't trigger pipeline job using jenkins cli. When i run jenkins as anaonymous this works, but when i create a user/admin it fails.

I have a job A which has parameters and passes the same to Pipeline Job. This is a Master-slave setup. This is how i run:

sudo java -jar /home/user/jenkins-cli.jar -s $JENKINS_URL build pipeline_job -p parameter_Name="$parameter_Name" -p parameter_Name2="$parameter2_Name"

1.) I tried using options, "-auth" , "-username -password" but doesn't work.

errors: No such command: -auth No such command: -ssh

2.) Another option is paste the public key in SSH section http://jenkin_url/me/configure , but still it fails

error:

java.io.IOException: Invalid PEM structure, '-----BEGIN...' missing

Is there i am missing anything ?


Solution

  • I Found the solution,

    1.) used SSH CLI.

    In my case i was using master-slave environment, connection was made using SSH keys vice-versa. In order to trigger the build using Jenkins CLI, place the SSH keys both public & private and place them in http://jenkinsURL/user/username/configure

    Here username= the one used to connect the nodes.

    Trigger the job as below:

    java -jar /home/username/jenkins-cli.jar -s $JENKINS_URL -i /home/username/.ssh/id_rsa build JOBNAME
    

    Note: This is one way but cloudbees doesn't encourage this approach.

    2.) There is new approach i.e., using API token authentication.

    • Go to http://jenkinsURL/user/username/configure
    • Copy the API token
    • trigger the build as below:

      java -jar /home/username/jenkins-cli.jar -s $JENKINS_URL -auth username:apitoken /home/username/.ssh/id_rsa build JOBNAME

    Note: For using API token option, download the latest jar file