Jenkins2 學習系列10 -- 多分支pipeline構建

Updated: 2019-08-15git

若是但願經過Webhook觸發multibranch pipeline項目須要安裝 multibranch-scan-webhook-trigger-plugin 插件 安裝完以後,配置界面多出一個Scan by webhook選項github

image.png

實際中一個項目的代碼倉庫可能會有不少分支,好比develop,master等。Jenkins 支持建立多分支pipeline的任務。web

建立多分支項目

新建 "Item" 直接選擇 "Multibranch Pipeline" 便可 Tab中有不少配置項,好比 General,Branch Sources,Build Configuration等正則表達式

  • Scan Multibranch Pipeline Triggers 觸發 掃描分支頻率,最低是1分鐘

image.png

  • Orphaned Item 孤兒任務,所謂孤兒任務即代碼倉庫中該分支被刪除,可是Jenkins分支中還保留着。

image.png

image.png

  • Health metric 健康指標 我也不清楚有什麼用,望指教

配置完成後,Jenkins就會自動執行首次構建,首先掃描全部的分支,若是根據配置的路徑去找Jenkinsfile,找到後就當即執行。express

根據發現的分支數量,好比這裏3個就自動建立了3個pipeline項目,點進去後能夠像pipeline任務同樣進行詳細配置。bash

image.png

使用 when 指令判斷多分支

咱們須要判斷針對不一樣分支作不一樣事情,使用 if else 比較low,不夠優雅ui

stage("deploy to test") {
  steps {
      script {
          if (env.GIT_NAME == 'testing') {
            echo 'deploy to test'
          }
     }
   }
}
複製代碼

能夠使用when指令this

stage("deploy to test") {
  when {
    branch 'testing'
    steps {
      echo 'deploy to test'
    }
  }
}

stage("deploy to prod") {
  when {
    branch 'production'
    steps {
      echo 'deploy to prod'
    }
  }
}
複製代碼

when指令的用法

when指令容許pipeline根據給定的條件,決定是否執行階段內的步驟。when指令必須至少包含一個條件。when指令除了支持branch判斷條件,還支持多種判斷條件。spa

  • changelog:若是版本控制庫的changelog符合正則表達式,則執行
  • changeset:若是版本控制庫的變動集合中包含一個或多個文件符合給定的Ant風格路徑表達式,則執行
when {
  changeset "**/*.js"
}
複製代碼
  • environment:若是環境變量的值與給定的值相同,則執行
when {
  environment name: 'DEPLOY_TO', value: 'production'
}
複製代碼
  • equals:若是指望值與給定的值相同,則執行
when {
  equals expected: 2, actual: currentBuild.number
}
複製代碼
  • expression:若是Groovy表達式返回的是true,則執行 當表達式返回的是字符串時,它必須轉換成布爾類型或null;不然,全部的字符串都被看成true處理。
when {
  expression {
    return env.BRANCH_NAME != 'master'
  }
}
複製代碼
  • building Tag:若是pipeline所執行的代碼被打 了tag,則執行
  • tag:若是pipeline所執行的代碼被打了tag,且tag名稱符合規則,則執行 若是tag的參數爲空,即tag (),則表示不論tag名稱是什麼都執行,與buildingTag的效果相同。
when {
  tag "release-*"
}
複製代碼

tag 條件支持comparator參數,支持的值以下: -- EQUALS:簡單的文本比較。插件

when {
  tag "release-3.1", comparator: "EQUALS"
}
複製代碼

-- GLOB (默認值):Ant風格路徑表達式。因爲是默認值,因此使用時通常省略。完整寫法以下:

when {
  tag "release-*", comparator: "GLOB"
}
複製代碼

-- REGEXP:正則表達式。使用方法以下:

when {
  tag "release-\\d+", comparator: "REGEXP"
}
複製代碼

tag條件塊很是適合根據tag進行發佈的發佈模式。

以上介紹的都是單條件判斷,when指令還能夠進行多條件組合判斷。

  • allOf:全部條件都必須符合。下例表示當分支爲master且環境變量DEPLOY TO的值爲production時,才符合條件。
allOf {
  branch "master";
  environment name: 'DEPLOY_TO', value: 'production'
}
複製代碼

注意,多條件之間使用分號分隔。

  • anyOf:其中一個條件爲true, 就符合。下例表示master分支或staging分支都符合條件。
anyOf {
  branch "master";
  branch "staging";
}
複製代碼

Generic Webhook Trigger 插件在多分支pipeline場景下的應用

Generic Webhook Trigger 在以前已經介紹過,能夠這麼傳參

triggers {
        GenericTrigger(
            genericVariables: [
              [key: 'ref', value: '$. ref']
            ],
            token: env.JOB_NAME ,
            regexpFilterText: '$ref',
            regexpFilterExpression: 'refs/heads/' + env.BRANCH_NAME,
        )
    }
複製代碼

env.BRANCH_NAME 爲當前 pipeline 的分支名

多分支項目中特有的環境變量

有些全局環境變量是多分支項目中特有的,能夠直接在pipeline中引用,如 ${env.BRANCH_NAME} 下面的就不翻譯了

  • BRANCH_NAME

For a multibranch project, this will be set to the name of the branch being built, for example in case you wish to deploy to production from master but not from feature branches; if corresponding to some kind of change request, the name is generally arbitrary (refer to CHANGE_ID and CHANGE_TARGET).

  • CHANGE_ID

For a multibranch project corresponding to some kind of change request, this will be set to the change ID, such as a pull request number, if supported; else unset.

  • CHANGE_URL

For a multibranch project corresponding to some kind of change request, this will be set to the change URL, if supported; else unset.

  • CHANGE_TITLE

For a multibranch project corresponding to some kind of change request, this will be set to the title of the change, if supported; else unset.

  • CHANGE_AUTHOR

For a multibranch project corresponding to some kind of change request, this will be set to the username of the author of the proposed change, if supported; else unset.

  • CHANGE_AUTHOR_DISPLAY_NAME

For a multibranch project corresponding to some kind of change request, this will be set to the human name of the author, if supported; else unset.

  • CHANGE_AUTHOR_EMAIL

For a multibranch project corresponding to some kind of change request, this will be set to the email address of the author, if supported; else unset.

  • CHANGE_TARGET

For a multibranch project corresponding to some kind of change request, this will be set to the target or base branch to which the change could be merged, if supported; else unset.

問題

Multibranch Pipeline Events 的做用是什麼

image.png

參考

converting-conditional-to-pipeline/

相關文章
相關標籤/搜索