这篇文章是让读者能快速的建立Jenkins流水线。 流水线能从GitHub 取得source code,编译,测试。而且能在定时启动流水线。
Login to Jenkins webpage. I setup a Jenkins on my local machine, in this case, the url is 0.0.0.0:8080. Go to Manage Jenkins -> Manage Nodes and Clouds
The node name is “master”.
In Jenkins , create a new Pipeline. In the pipeline script, choose Pipeline script from SCM and set script path to Jenkinsfile.
In the github url, create a Jenkinsfile. For scripted pipeline, create the node master. Create three stages.
node (‘master’) {
stage(‘Source’) {
git ‘https://github.com/victoryeo/cppcode/'
}
stage(‘Build’) {
sh ‘make’
}
stage(‘Test’) {
echo “Test”
sh ‘./quotient’
}
}
In the Jenkins web page, click on the Build now option.
After build is completed, in the Build History pane, click on number, e.g. #10 to examine the build history.
Then, click on Console Output to inspect the build log.
For declarative pipeline, create the stages, three stage, and steps.
pipeline {
agent any
stages {
stage(‘Source’) {
steps {
git ‘https://github.com/victoryeo/cppcode/'
}
}
stage(‘Build’) {
steps {
sh ‘make clean’
sh ‘make’
}
}
stage(‘Test’) {
steps {
echo “Test”
sh ‘./quotient’
}
}
}
}
Additionally, in declarative pipeline, we can add triggers to Jenkinsfile to schedule periodic build.
pipeline {
agent any
triggers {
cron('H 9-16/2 * * 1-5')
# the field follows the syntax of cron (with minor differences).
# Specifically, each line consists of 5 fields
# MINUTE HOUR DOM MONTH DOW
# eg. once every two hours every weekday
}
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}
The Jenkinsfile and C++ code are available at https://github.com/victoryeo/cppcode/