作为 DevOps 管理员或开发人员,您可能希望在 Automation Pipelines 中使用 YAML 代码(而不是用户界面)创建管道。创建代码形式的管道时,可以使用任何编辑器并在管道代码中插入注释。
在管道代码中,可以引用外部配置,如环境变量和安全凭据。如果更新在管道代码中使用的变量,则在更新时可以不必更新管道代码。
可以将管道 YAML 代码用作模板以克隆和创建其他管道,以及与其他管道共享模板。
可以将管道代码模板存储在源代码管理存储库中,以便对其进行版本控制并跟踪更新。通过使用源代码管理系统,可以轻松备份管道代码,并根据需要进行还原。
前提条件
- 确认您具有代码编辑器。
- 如果计划将管道代码存储在源代码管理存储库中,请确认您可以访问工作实例。
过程
结果
通过使用代码示例,您创建了表示管道和端点的 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:
- [email protected]
- [email protected]
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 ---
下一步做什么
运行管道,并根据需要进行任何调整。请参见如何运行管道和查看结果。