Jenkins 流水线构建项目 配置
点击配置凭据
添加凭据
设置凭据信息
填写 Username, Private Key 2个字段即可,
Username随便写
私钥查看Jenkins服务器私钥文件获取
macname@MacdeMacBook-Pro ~ % cat ~/.ssh/id_rsa -----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEA1KVg42Up2pYFI5x9WBEYyRWGXpAOQ82g2Tsf8WHI+X8LOm6O XpVUMWfndVzG79S1XuojV6sp7D1xiQTjTP1dfz7/X3jpfnT5tS3LTVrQk0YxPQ48 csvbIRUfPqUszVOJIz6ZyErOm9LjLG9ii9Su8pE7LrnTv/5EpPynEPc6rSGv0Ee9 UgpwvxvkiHmNSjdGltIo7WORx/f7HFZDWUcGpZ9xMkXVM8CNrUYc/rvr7N5/oING CHAwAUtQLUaeAQSG9F47DsRdZWQEuMsOQsiwKTIOyfHArRYBElvzS3rnKYNPEAzb hTvVuyUcEndqYZY/Z6hBxllaXa6OuFvkGFQx2wIDAQABAoIBAH8c/PXxmztXQmsv OJ5UWgSaIWCVrHFLkkNI3KdmVMUP9J5RSDmx+kouWmYFt8XV2TWLsOTPJJhbgmrt yRJUwnc2KWHg3XMCaeKsJUNwbg4bf4OH5wqYYb086xh/lHCI5wlyemsIjMkJH1OI odgEoy/NMRuVacbzMAWQIi46G5Ex4e+JFNO1MEEf94B77txNPT4DwJtLDbdu20aG xl/INIMBj3vC1d3usfH5cU/Zg/vT1Y123vJWSYdO+JKoByNRgnDSlJF+kZLEpOIc Qb5mj1UO2Z+kJW2dJ5zQJ3dFxNdly2o4T3hHNLSZawzjJ+7mEcQMhyCtBwcXnSh5 IsH1DEECgYEA/n4hdk/R7ApCAOe2C1aQ3eD2XuJy20spJS3lugdEALxHRw+m0qgY XMUgayjuVxzLOZeg8YF/bnCOu47EBGZbBNhMscLRsFd2ovRtZwChxe0Pz5Q/Fu2Y PcCvx/P+qGYA9+6u0kj7V/zNNRjF38mCg4OgjRz4HDL2jL1H/hXJeicCgYEA1efM b+kCmFJBZLJby2Y21wuw052a+NzqUMyaYNCwItoXlhrDspvwBTLu8HRXJijyfKfU SojJyNj65TgzSMMex6zKWismW5km/krQiGXNiUo1SMlKSQY0fSaLrWkRhWiHtNzc OLLt3HHFHGnW1elhdyH9SCcu5vEruJVbp09JHy0CgYAQjNdUQGCgfxJHyMnX9us4 4rZi+UsK49prJo74CBzuB1CDWIs6y9+5uMAg9+p4E9FLj4bR7eABqKQ/oksxFg75 Q6/JJ6hHVYjLlCXBOxNKMd6DW1q2/QCXk35a/I0lGm0ZspX/6usdj70wpMnJge2etQ 7jB0vNuNyeqqwGx8I6KUpQKBgQCXVwktpI5RduENfaO0pPzn6T2Se0/KA4NE533S QdRc1DEb4pPTKsEG7KJhPe7q8M65t2fpJ5unZkmoV3iD4E5WdPNMlN4uE7LP/B2dehjujgQ SPbCy94nQUNtpxY79amdZ1keKnbZX+nvs6jW7yemkruNXTG4ODQbeJ2Wh8uHLSVL mq0BgQKBgQCZo2YQTrGCUx7YwIGjy40f3QxqdaWg8AIJkWEhW8Ks5H9T7Hy41rN/ DdFdMotfTkI7z6WFyZDpu43kufLo/pK/44GuXy6jnm6WWVs1L7Ewgsy1RZkly+aWB2XhX rfwp4d3Kwaqs91Uw6+oE6RNKCElGVu2+H8xsxaNdt456gZF6IFbVYiQk8wakw== -----END RSA PRIVATE KEY----- macname@MacdeMacBook-Pro ~ %
同时,需要将公钥添加到git上
macname@MacdeMacBook-Pro ~ % cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDUpWDjZSnalgUjnH1YERjJFYZekA5DzaDZOx/xYcj5fws6bo5elVQxZ+d1XMbv1LVe6iNXqynsPXGJBONM/V1/Pv9feOl+dPm1LctNWtCTdegRjE9Djxyy9shFR8+p764SzNU4kjPpnISs6b0uMsb2KL1K7ykTsuudO//kSk/KcQ9zqtIa/QR71SCnC/G+SIeY1KN0aW0ijtY5HH9/scVkNZRwaln3EyRdUzwI2tRhz+u+vs3n+gg0YIcDABS1AtRp4BBIb0swdefrXjsOxF1lZAS4yw5CyLApMgg7J8cCtFgESW/NLeucp0i9jupg08QDNuFO9W7JRwSd2phlj9nqEHGWVpdro6g4W+QYVDHb macname@bogon
github , Settings->SSH and GPG keys->Add new
到这里凭据添加完成了。
新建一个流水线job
编写pipeline脚本
加入以下代码
node{ stage('Prepare') { echo ">>>>>>>>>> Stage 1. Prepare" } stage('Clone Code') { echo ">>>>>>>>>> Stage 2. Clone Code" git branch: 'main',credentialsId: 'ddiof58925df-1f1c-4870-b1fd-c5473ec6c02e', url: 'git@github.com:xxx/test_jenkins.git' } stage('Exec Test Suit') { echo ">>>>>>>>>> Stage 3. Exec Test Suit for " sh "chmod +x test.sh" SHELL_RESULT = sh(script: "./test.sh", returnStatus: true) } stage('Generate Report') { echo ">>>>>>>>>> Stage 4. Generate Report for , please check http://www.baidu.com/ for more information" if (SHELL_RESULT != 0){ echo 'Ops! auto-test failed, pls check more information from http://www.baidu.com' } } }
其中credentialsId从Jenkins凭据获取, Manage Jenkins -> Manage Credentials
GitHub地址,换成自己的
代码内容
macname@MacdeMacBook-Pro test_jenkins % ls README.md test.sh macname@MacdeMacBook-Pro test_jenkins % macname@MacdeMacBook-Pro test_jenkins % cat test.sh echo hello world exit $? macname@MacdeMacBook-Pro test_jenkins % macname@MacdeMacBook-Pro test_jenkins %
配置完成后保存。
然后开始构建
构建成功。