GitLab + Jenkins + Harbor 工具鏈快速落地指南( 二 )


  • DevStream 期望 Pipeline 配置通過 Jenkinsfile 來定義,這個 Jenkinsfile 也是通過模板的方式保存,可以靈活渲染 。比如官網示例中 Jenkinsfile 模板保存在這里;DevStream 執行的時候會下載這個 Jenkinsfile 模板(當然,這個模板也支持自定義 , 支持放到 GitLab 或者其他任何 web 服務器上),下載后渲染用戶自定義變量,然后將其寫入剛才創建的項目腳手架對應的代碼庫里;
  • 接著 DevStream 就可以調用 Jenkins api , 完成 Pipeline 創建了 。沒錯,創建 Pipeline 的時候,需要的 Jenkinsfile、項目地址等信息都有了,所以這里的 Pipeline 配置很輕量;
  • 最后 DevStream 還需要調用 GitLab api 完成 webhook 的創建,這樣 SCM(GitHub 或者 GitLab)上的事件(push、merge 等)才能順利通知到 Jenkins,從而觸發 Pipeline 執行 。
  • 到這里 DevStream 基本就打完收工了,這時候如果你往這個代碼庫里的主分支 push 了一個 commit,GitLab 就會直接觸發 Jenkins 上流水線運行;進而 Jenkins 上的流水線執行狀態也會直接回顯到 GitLab 上;當然,Jenkins 里構建的產物,比如 Docker container image(s) 也會被 push 到 Harbor(每錯,這個過程是定義在 Jenkinsfile 里的,你可以靈活修改;同時 Harbor 也不一定非得是 Harbor,你可以直接改成其他鏡像倉庫的地址,從而讓 Jenkins 對接到云廠商提供的鏡像倉庫服務里也完全 OK) 。
    四、開干吧!考慮到插件的依賴順序,外加 Jenkins、GitLab、Harbor 等工具的部署屬于"基礎設施",幾乎只需要執行一次 ,  而 Repo Scaffolding 和 Jenkins Pipeline 的創建屬于"配置"過程,可能要執行多次(比如不斷新增 Repo 和 Pipeline 等),所以我們分2步來完成這條工具鏈的搭建過程 。
    4.1、工具鏈部署先下載一個 DevStream 的 CLI,參考這個文檔 。有了 dtm 之后,我們就該著手準備配置文件了(下面配置保存到 config.yaml 里):
    ---varFile: "" # If not empty, use the specified external variables config filetoolFile: "" # If not empty, use the specified external tools config filepluginDir: "" # If empty, use the default value: ~/.devstream/plugins, or use -d flag to specify a directorystate: # state config, backend can be local, s3 or k8s  backend: local  options:    stateFile: devstream-1.state---tools:- name: gitlab-ce-docker  instanceID: default  dependsOn: [ ]  options:    hostname: gitlab.example.com    gitlabHome: /srv/gitlab    sshPort: 30022    httpPort: 30080    httpsPort: 30443    rmDataAfterDelete: false    imageTag: "rc"- name: jenkins  instanceID: default  dependsOn: [ ]  options:    repo:      name: jenkins      url: https://charts.jenkins.io    chart:      chartPath: ""      chartName: jenkins/jenkins      namespace: jenkins      wait: true      timeout: 5m      upgradeCRDs: true      valuesYaml: |        serviceAccount:          create: true          name: jenkins        controller:          adminUser: "admin"          adminPassword: "changeme"          ingress:            enabled: true            hostName: jenkins.example.com          installPlugins:            - kubernetes:3600.v144b_cd192ca_a_            - workflow-aggregator:581.v0c46fa_697ffd            - git:4.11.3            - configuration-as-code:1512.vb_79d418d5fc8          additionalPlugins:            # install "GitHub Pull Request Builder" plugin, see https://plugins.jenkins.io/ghprb/ for more details            - ghprb            # install "OWASP Markup Formatter" plugin, see https://plugins.jenkins.io/antisamy-markup-formatter/ for more details            - antisamy-markup-formatter        # Enable HTML parsing using OWASP Markup Formatter Plugin (antisamy-markup-formatter), useful with ghprb plugin.        enableRawHtmlMarkupFormatter: true        # Jenkins Configuraction as Code, refer to https://plugins.jenkins.io/configuration-as-code/ for more details        # notice: All configuration files that are discovered MUST be supplementary. They cannot overwrite each other's configuration values. This creates a conflict and raises a ConfiguratorException.        JCasC:          defaultConfig: true- name: harbor  instanceID: default  dependsOn: [ ]  options:    chart:      valuesYaml: |        externalURL: http://harbor.example.com        expose:          type: ingress          tls:            enabled: false          ingress:            hosts:              core: harbor.example.com        chartmuseum:          enabled: false        notary:          enabled: false        trivy:          enabled: false        persistence:          persistentVolumeClaim:            registry:              storageClass: ""              accessMode: ReadWriteOnce              size: 5Gi            jobservice:              storageClass: ""              accessMode: ReadWriteOnce              size: 1Gi            database:              storageClass: ""              accessMode: ReadWriteOnce              size: 1Gi            redis:              storageClass: ""              accessMode: ReadWriteOnce              size: 1Gi

    推薦閱讀