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

    推荐阅读