As a DevOps administrator or developer, you might want to create a pipeline in Code Stream by using YAML code, instead of using the user interface. When you create pipelines as code, you can use any editor and insert comments in the pipeline code.
In your pipeline code, you can refer to external configurations such as environment variables and security credentials. When you update variables that you use in your pipeline code, you can update them without having to update the pipeline code.
You can use the pipeline YAML code as a template to clone and create other pipelines, and share the templates with others.
You can store your pipeline code templates in a source control repository, which versions them and tracks updates. By using a source control system, you can easily back up your pipeline code, and restore it if needed.
Prerequisites
- Verify that you have a code editor.
- If you plan to store your pipeline code in a source control repository, verify that you can access a working instance.
Procedure
Results
By using the code examples, you created the YAML code that represents your pipeline and endpoints.
Example: Example YAML code for a pipeline and endpoints
This example YAML code includes sections that represent the workspace for the Code Stream native build, stages, tasks, notifications, and more in a pipeline.
For examples of code for supported plug-ins, see Connecting Code Stream to endpoints
---
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}
---
This YAML code represents an example Jenkins endpoint.
--- 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 ---
This YAML code represents an example Kubernetes endpoint.
--- name: my-k8s tags: [ ] kind: ENDPOINT properties: kubernetesURL: https://urlname.examplelocation.amazonaws.com userName: admin password: encryptedpassword description: '' type: kubernetes:KubernetesServer isLocked: false ---
What to do next
Run your pipeline, and make any adjustments as needed. See How do I run a pipeline and see results.