在这个例子中,我们将使用SSH来整合Jenkins和GitHub。作为一个例子,我们将创建一个Pipeline项目并运行一个存储在GitHub仓库中的假Jenkins文件。
前提条件
首先,你需要确保你的Jenkins服务器能够通过SSH与GitHub对话。请按照用SSH连接GitHub页面中的步骤操作。
仓库
这是我们的假GitHub项目,它位于https://github.com/inanzzz/game 。
.
├── ci
│ └── pipeline
│ └── branch
│ ├── develop
│ │ └── Jenkinsfile
│ ├── feature
│ │ └── Jenkinsfile
│ └── master
│ └── Jenkinsfile
├── composer.json
├── composer.lock
├── src
│ └── Football.php
├── tests
│ └── FootballTest.php
└── vendor
登录Jenkins用户界面,进行以下操作。
凭证
-
点击左边的 "Credentials "链接。
-
点击 "Jenkins/Global",然后点击 "Add credentials "链接。
-
从 "种类 "选项中选择 "SSH用户名和私钥"。
-
为 "用户名 "选项设置一个用户名(例如:
GitHub-inanzzz)。 -
勾选 "直接输入 "单选按钮并将
~/.ssh/id_rsa.pub文件的内容粘贴到文本框中。 -
保存并退出。
管线
在Jenkins的主页上,点击 "New Item "链接,创建一个名为 "Test "的新项目,然后在下面的页面中选择 "Pipeline "选项:
-
在 "Pipeline "部分选择 "Pipeline script from SCM "选项。
-
选择 "Git "作为SCM。
-
在 "Repository URL "中使用
https://github.com/inanzzz/game。 -
从 "Credentials "中选择
GitHub-inanzzz。 -
也可以把
*/master改为*/develop。 -
在 "经文路径 "中添加
ci/pipeline/branch/develop/Jenkinsfile。 -
保存并退出。
如果你点击 "Build Now "链接,它应该成功地运行第一次构建,控制台的输出应该如下所示:
Started by user admin
Obtained ci/pipeline/branch/develop/Jenkinsfile from git https://github.com/inanzzz/game
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/Game
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential 7a41495c
Cloning the remote Git repository
Cloning repository https://github.com/inanzzz/game
> git init /var/lib/jenkins/workspace/Game # timeout=10
Fetching upstream changes from https://github.com/inanzzz/game
> git --version # timeout=10
using GIT_SSH to set credentials
> git fetch --tags --progress https://github.com/inanzzz/game +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url https://github.com/inanzzz/game # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url https://github.com/inanzzz/game # timeout=10
Fetching upstream changes from https://github.com/inanzzz/game
using GIT_SSH to set credentials
> git fetch --tags --progress https://github.com/inanzzz/game +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/develop^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/develop^{commit} # timeout=10
Checking out Revision f63bf36b (refs/remotes/origin/develop)
> git config core.sparsecheckout # timeout=10
> git checkout -f f63bf36b
Commit message: "Very first commit"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] timeout
Timeout set to expire in 1 min 0 sec
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Code Pull)
[Pipeline] echo
Pulling...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Building...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Testing...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Image Build)
[Pipeline] echo
Building...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Image Push)
[Pipeline] echo
Pushing...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] echo
Deploying...
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timeout
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS