How do I leverage pipeline as code in
Automation Pipelines

As a DevOps administrator or developer, you might want to create a pipeline in
Automation Pipelines
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.
  • 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.
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.
  1. In your code editor, create a file.
  2. Copy and paste the sample pipeline code, and update it to reflect your specific pipeline needs.
  3. To include an endpoint to your pipeline code, copy and paste the example endpoint code, and update it to reflect your endpoint.
    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.
    For more information about configuring the workspace, see Configuring the Pipeline Workspace.
  4. Save the code.
  5. To store and version your pipeline code, check the code into your source control repository.
  6. When you create a continuous integration and delivery pipeline, you must import the Kubernetes YAML file.
    To import the Kubernetes YAML file, select it in the Continuous Delivery area of the smart pipeline template, and click
    Process
    . Or, use the API.
By using the code examples, you created the YAML code that represents your pipeline and endpoints.
Example YAML code for a pipeline and endpoints
This example YAML code includes sections that represent the workspace for the
Automation Pipelines
native build, stages, tasks, notifications, and more in a pipeline.
For examples of code for supported plug-ins, see Connecting Automation Pipelines 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 ---
Run your pipeline, and make any adjustments as needed. See How do I run a pipeline and see results.