做為 DevOps 管理員或開發人員,您可能想要使用 YAML 程式碼 (而非使用者介面) 在 Code Stream 中建立管線。當您建立程式碼形式的管線時,可以使用任何編輯器,並在管線程式碼中插入註解。
在管線程式碼中,您可以參考外部組態,例如環境變數和安全性認證。當您更新在管線程式碼中使用的變數時,無需更新管線程式碼即可更新這些變數。
您可以將管線 YAML 程式碼用作範本來複製和建立其他管線,並與其他人共用範本。
您可以將管線程式碼範本儲存在原始檔控制存放庫中,從而對其進行版本設定並追蹤更新。透過使用原始檔控制系統,您可以輕鬆備份管線程式碼,並視需要進行還原。
必要條件
- 確認您具有代碼編輯器。
- 如果您打算將管線程式碼儲存在原始檔控制存放庫中,請確認您可以存取工作執行個體。
程序
結果
透過使用程式碼範例,您已建立表示管線和端點的 YAML 程式碼。
範例: 管線和端點的範例 YAML 程式碼
此範例 YAML 程式碼包括一些區段,用於表示管線中的 Code Stream 原生建置、階段、工作、通知等的工作區。
如需支援外掛程式的程式碼範例,請參閱將 Code Stream 連線至端點
---
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 ---
下一步
執行管線,並視需要進行任何調整。請參閱如何執行管線和查看結果。