[Super Comprehensive] Jenkins pulls Gitee code through SSH credentials (including error solutions)

[Super comprehensive] Jenkins pulls Gitee code through SSH certificate (including error resolution)

  • Jenkins pulls Gitee code through SSH credentials
    • 1. Generate a key
      • 1. Before generating the key, check whether the key has been generated, and delete the key if it exists:
      • 2. Use the command `ssh-keygen -t rsa` in the /root/.ssh directory to generate the public key and private key, and press Enter after entering the command
      • 3. The generated public key and private key files are as follows:
    • 2. Add the public key to your Gitee account
      • 1. View the generated public key
      • 2. Log in to Gitee, enter the background by clicking the setting option in the avatar, and configure the contents of the public key file just generated.
    • 3. Add the private key to Jenkins’ SSH credentials and create a project to use SSH credentials
      • 1. View the generated private key
      • 2. Create a new SSH credential in Jenkins. We just used the root user to generate the key, so the user name is filled with root, the ID can be left blank and will be automatically generated after saving, and the private key is filled with the content in the private key file we generated.
      • 3. In this way, we can configure SSH credentials in the project in Jenkins. Note that the SSH address of the project needs to be used at this time, as shown below:
        • ① Create a project
        • ② Configure git information, and then click Apply/Save below
        • ③The following error may be reported, and if an error is reported, click Apply/Save below
    • 3. Solve the error and create an association with the remote git warehouse in the local git warehouse
      • ①Enter the git warehouse directory
      • ② Use `git remote -v` to view your current remote url
      • ③Use `git remote set-url` to modify your remote url, instead of https protocol, use git protocol instead
      • ④Use `git remote -v` again to view the remote url
      • ⑤ Run the command `/usr/bin/git ls-remote -h git address`
      • ⑥Successfully solved, click configuration view in the project, you will find that the error just now has been solved
    • 4. Build the project
      • ① When we build the project, we can see the location of the downloaded git source code and the SSH credentials we just configured in the console output.
      • ②Finally, in Linux, enter the location of the downloaded git source code to check whether the file is pulled successfully!

jenkins pulls Gitee code through SSH credentials

To use the SSH key method to pull the code on Gitee, we only need to use the root user on the server to generate the public key and private key, configure the public key to Gitee, and then configure the private key to Jenkins. In this way, Jenkins can pull the code from Gitee without password.

1. Generate key

1. Before generating the key, check whether the key has been generated, and delete the key if it exists:

Deleting the existing key is to prevent some unknown errors following this process

root@bpvank-virtual-machine:~# cd /root/.ssh
root@bpvank-virtual-machine:~/.ssh# ls
id_rsa id_rsa.pub
root@bpvank-virtual-machine:~/.ssh# rm id_rsa
root@bpvank-virtual-machine:~/.ssh# rm id_rsa.pub
root@bpvank-virtual-machine:~/.ssh# ls

2. Use the command ssh-keygen -t rsa in the /root/.ssh directory to generate public and private keys, and press Enter after entering the command

root@bpvank-virtual-machine:~/.ssh# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:DrGdLSo+dVV9Q0doMHWw6FINU9rdH3psE0iol6FqVvw root@bpvank-virtual-machine
The key's randomart image is:
 + ---[RSA 2048]----+
| **== + |
| ooO== + |
| . . o.*o + + + |
| + B. = o + |
| o S.= .. =.|
| .B.. E o .|
| .. + .. |
| ... |
| .. |
 + ----[SHA256]----- +

3. The generated public key and private key files are as follows:

root@bpvank-virtual-machine:~/.ssh# ls
id_rsa id_rsa.pub

2. Add the public key to your Gitee account

1. Check the generated public key

root@bpvank-virtual-machine:~/.ssh# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDbXXDH792Z1Z382EQingiZWHjXWuds++v16HIT1uzA7dmpR41dGgmg9trGlJ/sbk8DPU83aAZZSy5kp9TaLUnFfxGOQU3NNMe78kRt2eOTaAnzo qhA8ZwyuXIgQVu7234N5HyLhmv0umzH/N + 2Sdg0H9orBm + 55BcrVzbsKSzPatZrlgNyOc2O/CLak0nn4/ivAglBgZJeODwEaTQAEOcdfdQpTvsJO + EdjOlRcfWWa4ylQl3zR BzSLklzfONo4y4AnCvDNuCTLzbSWnnG9Ti0v0b0aYwvqliFqGp67/EpQyjKfctoPgkGXMm5rfbhgo9Nmhi0++6Wi2kzLGu4Fh root@bpvank-virtual-machine

2. Log in to Gitee, click the setting option in the avatar to enter the background, and configure the contents of the public key file just generated.

Configure public key

3. Add the private key to Jenkins’ SSH credentials and create a project to use SSH credentials

1. Check the generated private key

root@bpvank-virtual-machine:~/.ssh# cat id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA211wx + /dmdWd/NhEIp4ImVh411rnbPvr9ehyE9bswO3ZqUeN
XRoJoPbaxpSf7G5PAz1PN2gGWUsuZKfU2i1JxX8RjkFNzTTHu/JEbdnjk2gJ86Ko
?…
LyKnjUsIBjC4uk98sJ29KwKBgDR8bjDXBwo3BusyZnWBGr33iZ0AC8GBMF1gV0ce
E8zAcEmOapDMQ12ap65b3lg4Rxj8gxKo8W4KBrbRwFaQ1iukNx4qg/m5MDd4IwnQ
0J2wXpZyN + at4ez5QJFn6a2JiKIVHmnNNuMFiB82jkrvi84B7HsH
-----END RSA PRIVATE KEY-----

2. Create a new SSH credential in Jenkins. We just used the root user to generate the key, so the user name is filled with root, the ID can be left blank and it will be automatically generated after saving, and the private key is filled with the content in the private key file we generated .

Note: When copying the private key, the head and tail also need to be copied and added! ! !

—–BEGIN RSA PRIVATE KEY—–

—–END RSA PRIVATE KEY—–

3. In this way, we can configure the SSH certificate in the project in Jenkins. Note that the SSH address of the project needs to be used at this time, as shown below:

①Create project

②Configure git information, then click Apply/Save below

③The following error may be reported, and if an error is reported, click Apply/Save below

3. Solve the error and create an association with the remote git repository in the local git repository

①Enter the git warehouse directory

# 1. View the git warehouse directory
root@bpvank-virtual-machine:~/.ssh# find ~ -name ".git" -type d
/root/.jenkins/workspace/java-project-01/.git
/root/.jenkins/workspace/java-project-02/.git
# 2. Enter the git warehouse directory of the project just created
root@bpvank-virtual-machine:~/.ssh# cd /root/.jenkins/workspace/java-project-02/.git
# 3, ls view
root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# ls
branches config description FETCH_HEAD HEAD hooks info objects refs

②Use git remote -v to view your current remote url

root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# git remote -v
origin https://github.com/test1/test1.git (fetch)
origin https://github.com/test1/test1.git (push)

③Use git remote set-url to modify your remote url, instead of https protocol, use git protocol instead

git remote set-url origin [email protected]:bpvank/jenkins-testing.git

④ Use git remote -v again to view the remote url

root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# git remote -v
origin [email protected]:bpvank/jenkins-testing.git (fetch)
origin [email protected]:bpvank/jenkins-testing.git (push)

⑤ Run the command again /usr/bin/git ls-remote -h git address

root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# /usr/bin/git ls-remote -h [email protected]:bpvank/jenkins-testing. git
The authenticity of host 'gitee.com (212.64.63.215)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp + KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no)?
Warning: Permanently added 'gitee.com,212.64.63.215' (ECDSA) to the list of known hosts.
3b671ae7df7898d9ad4315bd1354a5f00ab8c158 refs/heads/master

Explanation: This command is usually used to create an association with a remote git repository in a local git repository, or to update the latest status information of a remote reference in a local git repository.

⑥Successfully resolved, click configuration view in the project, you will find that the error just now has been resolved

View configuration
Error resolution

4. Build the project

①When we build the project, we can see the location of the downloaded git source code and the SSH credentials we just configured in the console output.

success

②Finally, in Linux, enter the location of the downloaded git source code to check whether the file is pulled successfully!

root@bpvank-virtual-machine:~# cd /root/.jenkins/workspace/java-project-02
root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02# ls
pom.xml README.md springmvctest.iml src