1. Generate an SSH public key and save the public key to gitLab

(1): Check whether the computer has a public key and private key. Run the following command in Powershell or Terminal (Mac). If the following information is displayed, the SSH public key and private key are available

$ cd ~/.ssh
$ ls
authorized_keys2  id_dsa       known_hosts
config            id_dsa.pub
Copy the code

(2): Ignore this step if the above file exists. If the above content is not displayed, enter ssh-keygen -o in Powershell or terminal (Mac) and press Enter

$ ssh-keygen -o
Generating public/private rsa key pair.
Enter file in which to save the key (/home/schacon/.ssh/id_rsa):
Created directory '/home/schacon/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/schacon/.ssh/id_rsa.
Your public key has been saved in /home/schacon/.ssh/id_rsa.pub.
The key fingerprint is:
d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 [email protected]

Copy the code

(3): Run the cat ~/.ssh/id_rsa.pub command to copy the public key

$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU
GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3
Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA
t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En
mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx
NrRFi9wrf+M7Q== [email protected]
Copy the code

(4): Open your GitLab warehouse address and click your profile picture -> Settings in the upper right corner, as shown below

(5): Paste the public key into Gitlab and save (Add key) as shown below:

(6): copy code repository address

(7): On the local command line, enter git close + the location of the replication in step (6)

git close [email protected]:xxx/xxx.git
Copy the code

If the code can be copied, then step 1: generate SSH public key and save the public key to GitLab

Success is shown below:

Reference address git-scm.com/book/zh/v2/…

2. Configure Jenkins credentials to obtain the code of the warehouse in GitLab

(1) Find a project to be configured and click

(2) Open configuration

The source management Repository URL changes to your Gitlab address, and the Credentials click add Credentials

(4) Pop up the add credential window

(5) Add the configuration credential field

  1. Type: SSH Username with private key

  2. Description: Fill in any name you can remember

  3. Private Key: Selected.

  4. Copy the local private key into the input box (how to obtain the private key, see the following figure)

  5. And I’m gonna hit Add

To obtain a private key, perform the following steps:

(6): Select the credential you just added, whose name is described in step 5

  1. If the credentials are correct, then nothing is wrong, right

  1. If the credential is incorrect, show it as follows, report an error, and check that the above steps are correct
Unable to connect to warehouse: Command"git ls-remote -h -- [email protected]:XXX/im.git HEAD" returned status code 128:
stdout:
stderr: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
Copy the code

(7) Click Save

3. Build pipeline, click “Build pipeline now”