最基本的連續(xù)交付流程將至少具有三個階段,這些階段應(yīng)在以下內(nèi)容中定義Jenkinsfile:構(gòu)建,測試和部署。
對于本節(jié),我們將主要關(guān)注部署階段,但應(yīng)該注意的是,穩(wěn)定的構(gòu)建和測試階段是任何部署活動的重要前身。
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building'
}
}
stage('Test') {
steps {
echo 'Testing'
}
}
stage('Deploy') {
steps {
echo 'Deploying'
}
}
}
}
Toggle Scripted Pipeline (Advanced)
Jenkinsfile (Scripted Pipeline)
node {
stage('Build') {
echo 'Building'
}
stage('Test') {
echo 'Testing'
}
stage('Deploy') {
echo 'Deploying'
}
}
一個常見的模式是擴展級別以捕獲額外的部署環(huán)境,如“分段”或“生產(chǎn)”,如下面的代碼段所示。
stage('Deploy - Staging') {
steps {
sh './deploy staging'
sh './run-smoke-tests'
}
}
stage('Deploy - Production') {
steps {
sh './deploy production'
}
}
在這個例子中,我們假設(shè)我們的./run-smoke-tests腳本運行的任何“煙霧測試” 都足以將釋放資格或驗證到生產(chǎn)環(huán)境。自動將代碼自動部署到生產(chǎn)的這種Pipeline可以被認(rèn)為是“持續(xù)部署”的實現(xiàn)。雖然這是一個崇高的理想,但對許多人來說,連續(xù)部署可能不實際的原因很好,但仍然可以享受持續(xù)交付的好處。 Jenkins Pipeline很容易支撐兩者。
通常在階段之間,特別是環(huán)境階段之間,您可能需要人為的輸入才能繼續(xù)。例如,判斷應(yīng)用程序是否處于“促進”到生產(chǎn)環(huán)境的狀態(tài)。這可以通過input
步驟完成。在下面的示例中,“真實檢查”階段實際上阻止輸入,如果沒有人確認(rèn)進度,則不會繼續(xù)進行。
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
/* "Build" and "Test" stages omitted */
stage('Deploy - Staging') {
steps {
sh './deploy staging'
sh './run-smoke-tests'
}
}
stage('Sanity check') {
steps {
input "Does the staging environment look ok?"
}
}
stage('Deploy - Production') {
steps {
sh './deploy production'
}
}
}
}
Toggle Scripted Pipeline (Advanced)
Jenkinsfile (Scripted Pipeline)
node {
/* "Build" and "Test" stages omitted */
stage('Deploy - Staging') {
sh './deploy staging'
sh './run-smoke-tests'
}
stage('Sanity check') {
input "Does the staging environment look ok?"
}
stage('Deploy - Production') {
sh './deploy production'
}
}
這個導(dǎo)讀旨在向您介紹使用Jenkins和Jenkins Pipeline的基礎(chǔ)知識。因為它是非??蓴U展的,Jenkins可以進行修改和配置,以處理幾乎任何方面的自動化。要了解更多關(guān)于Jenkins可以做什么的信息,請查看用戶手冊或 Jenkins博客,了解最新的活動,教程和更新。
更多建議: