Get the App
SLTechnology News&Howtos  ›  Servers  › 

Jenkins, automatic deployment of scp transmission error pit

Shulou Source: shulou.com Published: 2022-06-02 20:43:49 10月04日 Update

Environment: centos7

Version: jenkins-2.176.3-1.1.noarch.rpm

Problem: after installing jenkins with a root account, configure automatic deployment, and the packaged project cannot be scp to the project server. (after the test server ssh port is changed from 22 to 6688 by default)

Host key verification failed. Lost connection

[JENKINS] Archiving / opt/jenkins/workspace/test-microservice/base-zuul/target/base-zuul-0.0.1-SNAPSHOT.jar to com.zjgf/base-zuul/0.0.1-SNAPSHOT/base-zuul-0.0.1-SNAPSHOT.jar

[test-microservice] $/ bin/bash / tmp/jenkins8821230735005579726.sh

Channel stopped

Host key verification failed.

Lost connection

Upload files fail

Solution:

1. The jenkins installed in the rpm package adds jenkins by default. The default shell for users is:

Jenkins:x:992:989:Jenkins Automation Server:/var/lib/jenkins:/bin/false

2. Modify the default shell

Jenkins:x:992:988:Jenkins Automation Server:/var/lib/jenkins:/bin/bash

3. Switch to jenkins user

[root@node60 ~] # su-jenkins

Bash-4.2 $

4. Solve the problem of variables

You can see that jenkins users have no variables because when jenkins is installed, jenkins only creates jenkins users, not home directories for them. So the system will not automatically copy the user environment variable files under the / etc/skel directory to the user's home directory when creating the user, which leads to the non-existence of these files and the problem of-bash-4.2#.

5. Modify jenkins user variable

The following command is executed after switching to the jenkins user (again, su-jenkins)! (only the user now displays-bash-4.2) Don't panic at this time, perform the following steps:

Vim / .bash_profile

Execute the above command, and even if there is no .bash _ profile file, linux will be created automatically.

And then add this sentence.

Export PS1=' [\ u@\ h\ W]\ $'

: wq (save exit)

Then refresh the .bash _ profile file to make it work

Source / .bash_profile

And then you can see that the current variable is in effect.

[jenkins@node60 root] $

You can now see that the shell variable for the jenkins user is normal.

6. Test again

Execute the jenkins task again and find or report an error. During the use of Jenkins, if the sudo command is used in the script, an error like the following may occur:

Sudo: no tty present and no askpass program specified

This is due to the incorrect context in which the Jenkins server executes the sudo command, resulting in an exception in the execution of the command.

7. Solution:

Switch to root or administrator account

[root@node60 ~] # visudo

Add a line to the file

Allows people in group wheel to run all commands

% wheel ALL= (ALL) ALL

Jenkins ALL= (ALL) NOPASSWD: ALL

Execute the jenkins task again and find that there is still an error. After troubleshooting, you need to add the key of the jenkins user on the test server.

Cat / var/lib/jenkins/.ssh/id_rsa.pub

The system prompts me -!

Cat: / var/lib/jenkins/.ssh/id_rsa.pub: there is no such file or directory

So

Create the jenkins user's public key file id_rsa.pub (su-jenkins the following command is executed in the jenkins user shell! )

[jenkins@node60 root] $ssh-keygen-t rsa

Go all the way back to the car and then check the generated jenkins key pair.

[jenkins@node60 root] $cat / var/lib/jenkins/.ssh/id_rsa.pub

Ssh-rsa AAAAB3NzaWGfcK3 (too long, silently delete part) WEDv64dQ+oG/IN2lDQa/ jenkins@node60

Then copy the public key produced by jenkins, log in to the test server, and add it to the ~ / .ssh/authorized_keys of the test server deployment account.

[jenkins@test1] $vim ~ / .ssh/authorized_keys

Ssh-rsa AAAAB3NzaW0Mqv0xwKGfcK3WEDv645lPuhXXReY3jGdQ+onGwqym9G/IN2lDQa/ jenkins@node60

Save exit

8. Test again

Edit jenkins task JOB* again {it should be noted that the specified port number parameters-P and-p used by scp and ssh are case-sensitive in jenkins.

Here are the test scripts I wrote during the test

! / bin/bash

Upload path

UPLOAD= "/ opt/MService/file/upload"

Scp-P 6688 / opt/jenkins/workspace/test-aaa-master/target/aaa-0.0.1-SNAPSHOT.jar jenkins@test1:$UPLOAD

Execute the test server update script

Ssh-t-p 6688 jenkins@test1 'sh/ home/jenkins/.sh/aaa.sh'

At this point, the problem is solved! Finish up

Tags: User test command file server service variable again directory problem task script switch public key just key time is in the environment the system Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Shulou Technology MySQL vpn