Pipelines as Code 體驗(yàn)

pipelines-as-code.png

?? 目錄

  • ?? Pipelines as Code 是什么?
  • ??? 準(zhǔn)備環(huán)境
  • ?? 快速安裝
  • ? 能力體驗(yàn)
  • ?? 一些疑問(wèn)
  • ?? 參考

?? Pipelines as Code 是什么?

是一套基于 OpenShift Pipeline/Tekton 自用的 CI 工具,可以幫助你快速構(gòu)建部署你的代碼

它使用位于源代碼控制管理 (SCM) 系統(tǒng)(例如 GitHub )的文件中的 Tekton PipelineRuns 和任務(wù)來(lái)定義 CI/CD,通過(guò)將 Pipeline 定義存儲(chǔ)在代碼倉(cāng)庫(kù)中,可以更輕松地通過(guò)代碼更改對(duì)管道更改進(jìn)行版本控制、審查和協(xié)作

它不是一個(gè)構(gòu)建引擎,本身不負(fù)責(zé)構(gòu)建工作,只是負(fù)責(zé)構(gòu)建的定義和一些資源狀態(tài)的同步,他會(huì)將構(gòu)建工作全部委托給 Tekton 來(lái)做,自己做了一些外圍的工作,比如定義了代碼倉(cāng)庫(kù)地址,在什么條件下可以觸發(fā)構(gòu)建,誰(shuí)會(huì)有權(quán)限來(lái)觸發(fā)這些構(gòu)建等等

??? 準(zhǔn)備環(huán)境

Getting started with Pipelines-as-Code 中詳細(xì)的描述另外如何安裝 Pipelines as Code,這里描述下我的安裝過(guò)程,給大家一個(gè)大致的地圖

正如前面提到,Pipelines as Code 會(huì)將構(gòu)建工作交給 Tekton 來(lái)做,所以需要安裝 Tekton Pipeline 保證構(gòu)建引擎可用

另外安裝 Pipelines as Code 相關(guān)的組件,安裝可以通過(guò)直接 apply yaml 文件到集群,也可以通過(guò)一個(gè)命令行工具來(lái)完成,這個(gè)工具可以幫你完成 Pipelines as Code 的安裝和一些使用的初始化工作,我使用的就是命令行工具

當(dāng)然這一切都需要先有一個(gè) k8s 集群

k8s 集群

我使用的是 macOs,k8s 我使用的工具是 OrbStack,他可以提供 Docker 和 k8s 環(huán)境,整體使用感覺(jué)好,能耗和啟動(dòng)速度方面都很好,推薦大家使用

orbStask.png

Tekton Pipeline

我本地有 tektoncd/pipeline 源碼用于開發(fā),可以通過(guò)源碼直接進(jìn)行構(gòu)建部署,通過(guò) ko apply --local -R -f config/ 就可以將 Tekton Pipeline 所需的組件部署起來(lái)(簡(jiǎn)便的部署方式,對(duì)吸引社區(qū)貢獻(xiàn)者作用很大)

如果沒(méi)有相關(guān)開發(fā)環(huán)境,也可以通過(guò)在集群 apply pipeline-release.yaml 進(jìn)行安裝,這會(huì)將相關(guān)的 Namespace Deployment 等資源安裝好

命令行工具

Pipelines as Code 提供了一個(gè)命令行工具,這個(gè)工具是作為 tkn 的一個(gè)插件存在的,tkn 是 Tekton 提供的一個(gè)命令行工具,通過(guò)如下命令安裝這兩個(gè)命令行工具

brew install tektoncd-cli

brew install openshift-pipelines/pipelines-as-code/tektoncd-pac

執(zhí)行完成后,執(zhí)行如下命令行,能顯示版本號(hào)說(shuō)明安裝正常

{21:44}/ ? tkn pac version
0.27.1

?? 快速開始

在這里,我們會(huì)安裝 Pipelines as Code 組件,配置 gosmee 以便本地接收 Github 的 webhook 事件信息,創(chuàng)建 Github Application 來(lái)同步流水線執(zhí)行狀態(tài)和結(jié)果到 Github,并且創(chuàng)建一個(gè) Github 代碼倉(cāng)庫(kù)來(lái)檢驗(yàn)我們 CI 流水線能否生效

上面步驟中安裝的 tkn pac 提供了很多功能,可以讓我們的操作簡(jiǎn)便快捷,基于這一個(gè)命令行工具,我們就可以完成安裝工作,提供的參數(shù)大致如下

{21:44}/ ? tkn pac
tkn plugin to use Pipelines as Code as a CLI

Usage:
  tkn-pac [command]

Available Commands:
  bootstrap   Bootstrap Pipelines as Code.
  completion  Prints shell completion scripts
  create      Create Pipelines as Code resources
  delete      Delete Pipelines as Code resources
  describe    Describe a repository
  generate    Generate PipelineRun
  help        Help about any command
  info        Show installation information
  list        List Pipelines as Code Repository
  logs        Display the PipelineRun logs from a Repository
  resolve     Resolve PipelineRun the same way its run on CI
  version     Print tkn pac version
  webhook     Update webhook secret

Flags:
  -h, --help                help for tkn-pac
  -k, --kubeconfig string   Path to the kubeconfig file to use for CLI requests (default: /Users/sda/.kube/config) (default "/Users/sda/.kube/config")
  -n, --namespace string    If present, the namespace scope for this CLI request

Use "tkn-pac [command] --help" for more information about a command.

tkn pac bootstrap 可以幫助我們快速安裝 Pipelines as Code 以及設(shè)置 Github Application

安裝 Pipelines as Code 組件

執(zhí)行 tkn pac bootstrap 安裝 Pipelines as Code,此時(shí)會(huì)先檢查是否已經(jīng)安裝了該工具,如果沒(méi)有安裝,則會(huì)詢問(wèn)是否安裝,我們?cè)谶@里選擇安裝

{21:49}/ ? tkn pac bootstrap
=> Checking if Pipelines-as-Code is installed.
??? Pipelines-as-Code doesn't seem to be installed in the pipelines-as-code namespace.
? Do you want me to install Pipelines-as-Code v0.19.2? (Y/n)

安裝 gosmee

繼續(xù)安裝時(shí),會(huì)詢問(wèn)你是否要安裝 gosmee,這是一個(gè) webhook 轉(zhuǎn)發(fā)器,可以實(shí)現(xiàn)將 Github 上的 webhook 事件信息轉(zhuǎn)發(fā)到我們的本地環(huán)境,這很重要,我們需要選擇安裝

gosmee.png

需要注意的是,這里會(huì)生成一個(gè) gosmee forward URL,我們需要保留一下,后續(xù)設(shè)置 Github Application 的時(shí)候會(huì)到,比如這里生成的 https://hook.pipelinesascode.com/zZVuUUOkCzPD,官方文檔沒(méi)有提到這一步,導(dǎo)致我在這里就踩了坑,導(dǎo)致排查了很久 ??

Pipelines-as-Code does not install an Ingress object to allow the controller to be accessed from the internet.
We can install a webhook forwarder called gosmee (https://github.com/chmouel/gosmee) using a https://hook.pipelinesascode.com URL.
This will let your git platform provider (e.g., GitHub) reach the controller without requiring public access.
? Do you want me to install the gosmee forwarder? (Y/n)
?? Your gosmee forward URL has been generated: https://hook.pipelinesascode.com/zZVuUUOkCzPD

指定 Tekton Dashboard

前面提到過(guò),Pipelines as Code 主要是將工作委托給了 Tekton 來(lái)完成,Tekton 有自己的 UI 界面的組件 Tekton Dashboard,他會(huì)在界面上展示 PipelineRun TaskRun 的執(zhí)行狀態(tài)及相關(guān)的日志

這里指定 Tekton Dashboard 地址的步驟,就是為了支持在查看某個(gè)構(gòu)建狀態(tài)和詳細(xì)日志的場(chǎng)景下,跳轉(zhuǎn)到相關(guān)界面查看相關(guān)信息

因?yàn)槲覜](méi)有安裝,因此跳過(guò)了這個(gè)步驟,這里是官方文檔提供的 demo,它會(huì)先自動(dòng)檢測(cè)是否存在對(duì)應(yīng)的地址,如果存在直接使用,用戶也可以自行指定地址

?? We have detected a tekton dashboard install on http://dashboard.paac-127-0-0-1.nip.io
? Do you want me to use it? Yes

創(chuàng)建 Github Application

隨后會(huì)讓你創(chuàng)建一個(gè) Github Applicaiton,這里我們需要為它指定一個(gè)名稱,名稱沒(méi)有什么要求,只要不與已經(jīng)存在的 Application 沖突即可

? Enter the name of your GitHub application: My PAAC Application

你會(huì)被導(dǎo)向一個(gè)鏈接,通過(guò)鏈接可以創(chuàng)建 Github Application


github-application.png
?? Secret pipelines-as-code-secret has been created in the pipelines-as-code namespace
?? You can now add your newly created application to your repository by going to this URL:

https://github.com/apps/my-paac-application

?? Don't forget to run "tkn pac create repository" to create a new Repository CR on your cluster.

此時(shí)安裝過(guò)程就結(jié)束了,但是還是需要對(duì)剛才創(chuàng)建的 Github Application 做一些改動(dòng),保證后續(xù)工作順利

我們需要修改下 Application 的 Webhook URL 保證倉(cāng)庫(kù)事件信息可以發(fā)送到 gosmee 地址中,訪問(wèn) https://github.com/settings/apps/my-paac-application 找到設(shè)置,將 Webhook URL 修改成上面提到的需要保留的地址

application-webhook.png

? 能力體驗(yàn)

這一步驟中,我們會(huì)創(chuàng)建一個(gè) Github 倉(cāng)庫(kù)用于體驗(yàn) Pipelines as Code 提供的強(qiáng)大能力

點(diǎn)擊 https://github.com/openshift-pipelines/pac-demo/generate 跳轉(zhuǎn)到 Github 創(chuàng)建一個(gè) demo 倉(cāng)庫(kù),這是一個(gè) Golang 的代碼倉(cāng)庫(kù),里面有一些用于演示的簡(jiǎn)單的代碼

訪問(wèn) Github Application https://github.com/apps/my-paac-application 并安裝到剛才創(chuàng)建的 demo 倉(cāng)庫(kù)

install-application.png

clone 剛才創(chuàng)建的 demo 倉(cāng)庫(kù)到本地,并且進(jìn)入到相關(guān)目錄

git clone https://github.com/$yourusername/pac-demo
cd pac-demo

執(zhí)行如下命令為代碼倉(cāng)庫(kù)創(chuàng)建 CI 配置

tkn pac create repository

? Enter the Git repository url (default: https://github.com/chmouel/pac-demo):
? Please enter the namespace where the pipeline should run (default: pac-demo-pipelines):
! Namespace pac-demo-pipelines is not found
? Would you like me to create the namespace pac-demo-pipelines? (Y/n)
? Directory .tekton has been created.
? We have detected your repository using the programming language Go.
? A basic template has been created in /tmp/pac-demo/.tekton/pipelinerun.yaml, feel free to customize it.
? You can test your pipeline by pushing the generated template to your git repository

此時(shí)會(huì)在集群中創(chuàng)建一個(gè) repository 資源,并且在代碼倉(cāng)庫(kù)根目錄創(chuàng)建一個(gè) .tekton 的目錄,我們重點(diǎn)關(guān)注下這個(gè)自動(dòng)創(chuàng)建的文件夾

在該文件夾中會(huì)存在一個(gè) pipelinerun.yaml 文件,這個(gè)文件定義了一個(gè) CI 流程,里面包含了 clonegolang-ci-lint 兩個(gè)步驟,看下文件內(nèi)容了解一下

apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: pac-demo
  annotations:
    # The event we are targeting as seen from the webhook payload
    # this can be an array too, i.e: [pull_request, push]
    pipelinesascode.tekton.dev/on-event: "[pull_request, push]"

    # The branch or tag we are targeting (ie: main, refs/tags/*)
    pipelinesascode.tekton.dev/on-target-branch: "[main]"

    # Fetch the git-clone task from hub, we are able to reference later on it
    # with taskRef and it will automatically be embedded into our pipeline.
    pipelinesascode.tekton.dev/task: "git-clone"

    # Use golangci-lint from the hub to test our Golang project
    pipelinesascode.tekton.dev/task-1: "golangci-lint"

    # You can add more tasks by increasing the suffix number, you can specify
    # them as array to have multiple of them.
    # browse the tasks you want to include from hub on https://hub.tekton.dev/
    #
    # pipelinesascode.tekton.dev/task-2: "[curl, buildah]"

    # how many runs we want to keep attached to this event
    pipelinesascode.tekton.dev/max-keep-runs: "5"
spec:
  params:
    # The variable with brackets are special to Pipelines as Code
    # They will automatically be expanded with the events from Github.
    - name: repo_url
      value: "{{ repo_url }}"
    - name: revision
      value: "{{ revision }}"
  pipelineSpec:
    params:
      - name: repo_url
      - name: revision
    workspaces:
      - name: source
      - name: basic-auth
    tasks:
      - name: fetch-repository
        taskRef:
          name: git-clone
        workspaces:
          - name: output
            workspace: source
          - name: basic-auth
            workspace: basic-auth
        params:
          - name: url
            value: $(params.repo_url)
          - name: revision
            value: $(params.revision)
      - name: golangci-lint
        taskRef:
          name: golangci-lint
        runAfter:
          - fetch-repository
        params:
          - name: package
            value: .
        workspaces:
          - name: source
            workspace: source

  workspaces:
    - name: source
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
    # This workspace will inject secret to help the git-clone task to be able to
    # checkout the private repositories
    - name: basic-auth
      secret:
        secretName: "{{ git_auth_secret }}"

這個(gè)就是一個(gè)標(biāo)準(zhǔn)的 Tekton PipelineRun 資源的定義,不同的是 annotations 中有很多 Pipelines as Code 自定義的值,用來(lái)指示不同場(chǎng)景下的配置

可以看到,spec.pipelineSpec 中接受 repo_urlrevision 這兩個(gè)參數(shù)來(lái)進(jìn)行代碼克隆,并且這里包含 clone 和 golangci-lint 兩個(gè)步驟

此時(shí)我們提交代碼到代碼倉(cāng)庫(kù),并且發(fā)起從當(dāng)前分支到 main 分支的 PR,在 PR 的界面,應(yīng)該可以看到該 PR 的 checks 會(huì)處于執(zhí)行狀態(tài)

checks.png

點(diǎn)擊 Details 詳情可以看到 checks 相關(guān)詳情信息,通過(guò)這里我們可以看到,這個(gè) PR 的 checks 沒(méi)有通過(guò),原因是 golangci-lint 的一些校驗(yàn)沒(méi)有通過(guò)

details.png

代碼里存在一些不合規(guī)范的內(nèi)容

check-failure.png

我們可以修改 main.go 代碼以更正 checks 中提到的問(wèn)題,再次提交代碼

package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Fprintf(os.Stdout, "Hello world")
}

再去查看 PR 時(shí)會(huì)發(fā)現(xiàn),PR 的 checks 已經(jīng)成功通過(guò)了

check-succeed.png

這些就是 Pipeline as Code 的能力體驗(yàn),雖然只是一個(gè)簡(jiǎn)單的 Demo,但是他也滿足你在 Github 上做開發(fā)時(shí) CI 的基本需求,在代碼變動(dòng)時(shí)進(jìn)行代碼構(gòu)建,并且將流水線結(jié)果同步到 Github 上

?? 一些疑問(wèn)

這一切都很神奇,也許你想問(wèn),這些 check 是在哪里做的呢,是在我本地嗎?如果是的話,本地是如何接收到需要構(gòu)建的消息又是怎么將結(jié)果同步到 Github 上的呢

對(duì)于第一個(gè)問(wèn)題,你可以執(zhí)行 tkn pac logs -n pac-demo-pipelines -L 來(lái)查看 pac-demo-pipelines 命名空間最新執(zhí)行的 pipelinerun,這個(gè)對(duì)應(yīng)的就是 Github 上的那個(gè)檢查步驟,克隆代碼,隨后執(zhí)行 golangci-lint 校驗(yàn)代碼中存在的問(wèn)題

第二個(gè)問(wèn)題就是上面提到的 gosmee,它將 Github webhook 的事件信息轉(zhuǎn)發(fā)到了本地

第三個(gè)問(wèn)題是通過(guò)我們注冊(cè)的 Github Application 完成的構(gòu)建信息的同步,pipelines-as-code-controller 會(huì)將 pipelineRun 的執(zhí)行狀態(tài)和結(jié)果通過(guò) Github Application 同步到 Github 代碼倉(cāng)庫(kù)中

?? 參考

Pipelines-as-Code
Getting started with Pipelines-as-Code
Github Pipelines as Code
gosmee

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容