Jenkins从GitLab中拉取代码
1.在Jenkins主机上生成ssh密钥
[root@jenkins gitrepo]# ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/root/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_ed25519
Your public key has been saved in /root/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:OQNieyyrapJQqjjg6+yeQmqV/E6lAB6WGTlQ+TNqBBM root@jenkins
The key's randomart image is:
+--[ED25519 256]--+
|E+o. |
|oo= |
| O..o . |
|o =.++ . . |
| =.o+oo.S |
|+.o+.+o o |
|Oo. oo |
|X+.... |
|XXo .. |
+----[SHA256]-----+
[root@jenkins gitrepo]# ls -al ~/.ssh/
total 12
drwx------. 2 root root 46 Dec 15 22:23 .
dr-xr-x---. 7 root root 4096 Dec 15 21:49 ..
-rw------- 1 root root 399 Dec 15 22:23 id_ed25519
-rw-r--r-- 1 root root 94 Dec 15 22:23 id_ed25519.pub
[root@jenkins gitrepo]# cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDU5uZvN6i7fuEjWNST02lISBuVloc6y+sIYSycs0GUq root@jenkins
2.在GitLab中配置公钥
点击“头像”,选择“编辑个人资料”
点击“SSH密钥”
复制jenkins主机上生成的公钥
[root@jenkins gitrepo]# cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDU5uZvN6i7fuEjWNST02lISBuVloc6y+sIYSycs0GUq root@jenkins
[root@jenkins gitrepo]# ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDU5uZvN6i7fuEjWNST02lISBuVloc6y+sIYSycs0GUq root@jenkinszzzhan
粘贴SSH密钥
4. kenkins添加凭证
点击用户名=》点击“凭证”
复制私钥
[root@jenkins gitrepo]# cat ~/.ssh/id_ed25519
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACA1Obmbzeou37hI1jUk9NpSEgblZaHOsvrCGEsnLNBlKgAAAJBFzLPDRcyz
wwAAAAtzc2gtZWQyNTUxOQAAACA1Obmbzeou37hI1jUk9NpSEgblZaHOsvrCGEsnLNBlKg
AAAEA24KdkDfuk5oxflDyXmK9cI2tdOR5g8UZZHEj+CXvHEzU5uZvN6i7fuEjWNST02lIS
BuVloc6y+sIYSycs0GUqAAAADHJvb3RAamVua2lucwE=
-----END OPENSSH PRIVATE KEY-----
点击“Create”
往下滑,找到“Git Host Key Verification Configuration”选择“Accept first connection”,必须选择否则构建会报错
点击项目的“配置”
粘贴仓库的地址,然后选择刚刚配置的凭证
构建项目,显示构建成功
Established TCP socket on 33209
<===[JENKINS REMOTING CAPACITY]===>channel started
Executing Maven: -N -B -f /var/lib/jenkins/workspace/maven-docker/pom.xml clean package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for springmvc-maven:easy-springmvc-maven:war:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-war-plugin is missing. @ line 22, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ----------------< springmvc-maven:easy-springmvc-maven >----------------
[INFO] Building springmvc-maven 0.0.1-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[WARNING] Parameter 'version' is unknown for plugin 'maven-war-plugin:3.4.0:war (default-war)'
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ easy-springmvc-maven ---
[INFO] Deleting /var/lib/jenkins/workspace/maven-docker/target
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ easy-springmvc-maven ---
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/maven-docker/src/main/resources
[INFO]
[INFO] --- compiler:3.1:compile (default-compile) @ easy-springmvc-maven ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /var/lib/jenkins/workspace/maven-docker/target/classes
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ easy-springmvc-maven ---
[INFO] Not copying test resources
[INFO]
[INFO] --- compiler:3.1:testCompile (default-testCompile) @ easy-springmvc-maven ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ easy-springmvc-maven ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- war:3.4.0:war (default-war) @ easy-springmvc-maven ---
[INFO] Packaging webapp
[INFO] Assembling webapp [easy-springmvc-maven] in [/var/lib/jenkins/workspace/maven-docker/target/easy-springmvc-maven]
[INFO] Processing war project
[INFO] Copying webapp resources [/var/lib/jenkins/workspace/maven-docker/src/main/webapp]
[INFO] Building war: /var/lib/jenkins/workspace/maven-docker/target/easy-springmvc-maven.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.918 s
[INFO] Finished at: 2024-12-15T23:05:12-05:00
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving /var/lib/jenkins/workspace/maven-docker/pom.xml to springmvc-maven/easy-springmvc-maven/0.0.1-SNAPSHOT/easy-springmvc-maven-0.0.1-SNAPSHOT.pom
[JENKINS] Archiving /var/lib/jenkins/workspace/maven-docker/target/easy-springmvc-maven.war to springmvc-maven/easy-springmvc-maven/0.0.1-SNAPSHOT/easy-springmvc-maven-0.0.1-SNAPSHOT.war
Finished: SUCCESS
5. 其他
5.1 报错“stderr: No ED25519 host key is known for 192.168.100.31 and you have requested strict checking”
Started by user roy li
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/maven-docker
The recommended git tool is: NONE
using credential 3> git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/maven-docker/.git # timeout=10
Fetching changes from the remote Git repository> git config remote.origin.url git@192.168.100.31:plat-group/easy-springmvc-maven.git # timeout=10
Fetching upstream changes from git@192.168.100.31:plat-group/easy-springmvc-maven.git> git --version # timeout=10> git --version # 'git version 2.43.5'
using GIT_SSH to set credentials Roy's GitLab credentials
[INFO] SELinux is present on the host and we could not confirm that it does not apply actively: will try to relabel temporary files now; this may complain if context labeling not applicable after all> /usr/bin/chcon --type=ssh_home_t /var/lib/jenkins/workspace/maven-docker@tmp/jenkins-gitclient-ssh556435468242475846.key
Verifying host key using known hosts file
You're using 'Known hosts file' strategy to verify ssh host keys, but your known_hosts file does not exist, please go to 'Manage Jenkins' -> 'Security' -> 'Git Host Key Verification Configuration' and configure host key verification.> git fetch --tags --force --progress -- git@192.168.100.31:plat-group/easy-springmvc-maven.git +refs/heads/*:refs/remotes/origin/* # timeout=10
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from git@192.168.100.31:plat-group/easy-springmvc-maven.gitat PluginClassLoader for git//hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:997)at PluginClassLoader for git//hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1239)at PluginClassLoader for git//hudson.plugins.git.GitSCM._checkout(GitSCM.java:1310)at PluginClassLoader for git//hudson.plugins.git.GitSCM.checkout(GitSCM.java:1277)at hudson.scm.SCM.checkout(SCM.java:540)at hudson.model.AbstractProject.checkout(AbstractProject.java:1250)at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:649)at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:85)at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:522)at hudson.model.Run.execute(Run.java:1833)at PluginClassLoader for maven-plugin//hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543)at hudson.model.ResourceController.execute(ResourceController.java:101)at hudson.model.Executor.run(Executor.java:446)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --force --progress -- git@192.168.100.31:plat-group/easy-springmvc-maven.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout:
stderr: No ED25519 host key is known for 192.168.100.31 and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.Please make sure you have the correct access rights
and the repository exists.at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2848)at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2184)at PluginClassLoader for git-client//org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:635)at PluginClassLoader for git//hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:995)... 12 more
ERROR: Error fetching remote repo 'origin'
Finished: FAILURE
解决,找到“系统管理”=》“全局安全设置”=》“Git Host Key Verification Configuration”=》选择“Accept first connection”