如何在
Automation Pipelines
中利用代码形式的管道

作为 DevOps 管理员或开发人员,您可能希望在
Automation Pipelines
中使用 YAML 代码(而不是用户界面)创建管道。创建代码形式的管道时,可以使用任何编辑器并在管道代码中插入注释。
  • 确认您具有代码编辑器。
  • 如果计划将管道代码存储在源代码管理存储库中,请确认您可以访问工作实例。
在管道代码中,可以引用外部配置,如环境变量和安全凭据。如果更新在管道代码中使用的变量,则在更新时可以不必更新管道代码。
可以将管道 YAML 代码用作模板以克隆和创建其他管道,以及与其他管道共享模板。
可以将管道代码模板存储在源代码管理存储库中,以便对其进行版本控制并跟踪更新。通过使用源代码管理系统,可以轻松备份管道代码,并根据需要进行还原。
  1. 在代码编辑器中,创建一个文件。
  2. 复制并粘贴示例管道代码,然后对其进行更新以反映您的特定管道需求。
  3. 要在管道代码中包含端点,请复制并粘贴示例端点代码,然后对其进行更新以反映您的端点。
    When using a Kubernetes API endpoint in the pipeline workspace,
    Automation Pipelines
    creates the necessary Kubernetes resources such as ConfigMap, Secret, and Pod to run the continuous integration (CI) task or custom task.
    Automation Pipelines
    communicates with the container by using the NodePort.
    The
    Automation Pipelines
    pipeline workspace supports Docker and Kubernetes for continuous integration tasks and custom tasks.
    有关配置工作区的详细信息,请参见配置管道工作区
  4. 保存代码。
  5. 要存储管道代码并进行版本控制,请将代码签入到源代码管理存储库中。
  6. 创建持续集成和交付管道时,必须导入 Kubernetes YAML 文件。
    要导入 Kubernetes YAML 文件,请在智能管道模板的“持续交付”区域中选择该文件,然后单击
    处理
    。或者,使用 API。
通过使用代码示例,您创建了表示管道和端点的 YAML 代码。
管道和端点的示例 YAML 代码
此示例 YAML 代码包含一些区域,用于表示管道中
Automation Pipelines
本地构建、阶段、任务、通知等的工作区。
有关支持插件的代码示例,请参见将 Automation Pipelines 连接到端点
--- kind: PIPELINE name: myPipelineName tags: - tag1 - tag2 # Ready for execution enabled: false #Max number of concurrent executions concurrency: 10 #Input Properties input: input1: '30' input2: 'Hello' #Output Properties output: BuildNo: '${Dev.task1.buildNo}' Image: '${Dev.task1.image}' #Workspace Definition ciWorkspace: image: docker:maven-latest path: /var/tmp endpoint: my-k8s cache: - ~/.m2 # Starred Properties starred: input: input1 output: output1 # Stages in order of execution stageOrder: - Dev - QA - Prod # Task Definition Section stages: Dev: taskOrder: - Task1, Task6 - Task2 Long, Task Long Long - Task5 tasks: Task1: type: jenkins ignoreFailure: false preCondition: '' endpoints: jenkinsServer: myJenkins input: job: Add Two Numbers parameters: number1: 10 number2: 20 Task2: type: blah # repeats like Task1 above QA: taskOrder: - TaskA - TaskB tasks: TaskA: type: ssh ignoreFailure: false preCondition: '' input: host: x.y.z.w username: abcd password: ${var.mypassword} script: > echo "Hello, remote server" TaskB: type: blah # repeats like TaskA above # Notificatons Section notifications: email: - stage: Dev #optional ; if not found - use pipeline scope task: Task1 #optional; if not found use stage scope event: SUCCESS endpoint: default to: - user@yourcompany.com - abc@yourcompany.com subject: 'Pipeline ${name} has completed successfully' body: 'Pipeline ${name} has completed successfully' jira: - stage: QA #optional ; if not found - use pipeline scope task: TaskA #optional; if not found use stage scope event: FAILURE endpoint: myJiraServer issuetype: Bug project: Test assignee: abc summary: 'Pipeline ${name} has failed' description: |- Pipeline ${name} has failed Reason - ${resultsText} webhook: - stage: QA #optional ; if not found - use pipeline scope task: TaskB #optional; if not found use stage scope event: FAILURE agent: my-remote-agent url: 'http://www.abc.com' headers: #requestHeaders: '{"build_no":"123","header2":"456"}' Content-Type: application/json Accept: application/json payload: |- Pipeline ${name} has failed Reason - ${resultsJson} ---
此 YAML 代码表示一个示例 Jenkins 端点。
--- name: My-Jenkins tags: - My-Jenkins - Jenkins kind: ENDPOINT properties: offline: true pollInterval: 15.0 retryWaitSeconds: 60.0 retryCount: 5.0 url: http://urlname.yourcompany.com:8080 description: Jenkins test server type: your.jenkins:JenkinsServer isLocked: false ---
此 YAML 代码表示一个示例 Kubernetes 端点。
--- name: my-k8s tags: [ ] kind: ENDPOINT properties: kubernetesURL: https://urlname.examplelocation.amazonaws.com userName: admin password: encryptedpassword description: '' type: kubernetes:KubernetesServer isLocked: false ---
运行管道,并根据需要进行任何调整。请参见如何运行管道和查看结果