Describe a pipeline using a YAML file

更新时间:
复制 MD 格式

You can specify a YAML file in your pipeline configuration. This file describes how Serverless App Center executes the pipeline to build or publish a Serverless application. This topic describes how to configure the YAML file and the predefined fields it contains.

Background information

A YAML configuration file describes the data structures of the pipeline object and the pipeline template object that are required to execute a pipeline. When your code repository changes, a webhook triggers Serverless App Center, which then executes pipeline actions as defined in the configuration file.

Describing pipeline execution in a configuration file is a recommended DevOps practice for Serverless App Center. You can specify or create the YAML file when you create an application or in an environment. For more information, see Manage pipelines.

Prerequisites

  • You are familiar with the concepts of pipelines and pipeline templates. For more information, see Pipelines and Pipeline templates.

  • You have a Git repository that is supported by Serverless App Center and for which you have administrative permissions. Alternatively, you can use the Git repository that Serverless App Center creates and initializes when you create a new application.

  • You have created a YAML configuration file in the root directory of the Git repository. This topic uses a file named cicd.yaml as an example.

Configure the YAML file

You can use a YAML file to describe a pipeline by referencing a template or by directly defining the pipeline's execution steps. Directly defining the steps is recommended. The following code shows an example.

---
# Submit a pipeline for execution
kind: Pipeline
# The name of the pipeline execution. The name must be globally unique. Use the commit ID and a timestamp to construct a unique name.
name: "p-<% .git.shortCommitId %>-<% .currentTimestampMs %>"
# Optional descriptive information
description: 'demo pipeline'
spec:
  context:
    data:
      ## Global DingTalk notification configuration. This overwrites the configuration in the template.
      #dingTalkNotification:
      #  # Disables global DingTalk notifications. You can enable notifications for specific tasks as needed.
      #  enable: false
      #  # Skips notifications when a task succeeds.
      #  skipOnSuccess: false
      #  # The webhook for DingTalk notifications.
      #  webhook: https://oapi.dingtalk.com/robot/send?access_token=xxx
      #  # The signature key for DingTalk notifications.
      #  secret: xxx
      #  # The content of the DingTalk notification. Do not configure a universal text message unless you intend to unify the notification content.
      #  message:
      #    at:
      #      isAtAll: false
      #      atUserIds: ["admin"]
      #      atMobiles: ["188xxxx8888"]
      #    msgtype: text
      #    # The text field supports template syntax. The template can retrieve the status and exception information of the current execution.
      #    text: |
      #      task finished.
      #      TaskName: {{ .currentTask.name }}
      #      Success: {{ .success }}
      #      Status: {{ .currentTask.status.phase }}
      # Deploys using Serverless Devs. Specify the path to the s.yaml file. The path is automatically filled upon triggering.
      # deployFile: s.yaml
      # The name of the application in Serverless App Center. The name is automatically filled upon triggering.
      appName: <% .appName %>
      # The path to the s.yaml file in the repository. This refers to the s.yaml file in the root directory.
      deployFile: s.yaml
  # The description of the pipeline template used for this execution. Serverless App Center now supports built-in template descriptions in pipelines.
  templateSpec:
    tasks:
    - name: pre-check
      context:
        data:
          displayName: "Pre-check"
          enable: true
          steps:
            - plugin: "@serverless-cd/checkout"
            - plugin: "@serverless-cd/s-setup"
            - run: s plan -t "${{ ctx.data.deployFile }}"
      taskTemplate: serverless-runner-task
      runAfters: []
    - name: pre-check-approval
      context:
        data:
          displayName: "Manual Review"
          enable: true
      taskTemplate: need-approval
      runAfters:
      - name: pre-check
    - name: build-and-deploy
      context:
        data:
          displayName: "Build and Deploy"
          enable: true
          steps:
            - plugin: "@serverless-cd/checkout"
            - plugin: "@serverless-cd/s-setup"
            - run: s deploy -t "${{ ctx.data.deployFile }}" --use-local --assume-yes --skip-push
      taskTemplate: serverless-runner-task
      runAfters: 
      - name: pre-check-approval
        
---

In the preceding example, when a qualifying Git event occurs, a pipeline named p-<% .git.shortCommitId %>-<% .currentTimestampMs %> is created. The pipeline then sequentially executes three tasks: pre-check, manual review, and build and deploy. For each task, pipeline plugins and shell commands describe the specific execution steps.

Predefined variables in the YAML file

Serverless App Center provides the following predefined variables. When a trigger occurs, Serverless App Center replaces the placeholders in the YAML file with their corresponding variable values before submitting the pipeline and task objects described in the file.

Name

Description

Example

<% .appName %>

The application name.

headless-ffmpeg-wcha

<% .accountId %>

The ID of the Alibaba Cloud account.

143199913651****

<% .releaseVersion %>

The deployment version of the current pipeline execution.

1676913428****

<% .envName %>

The environment where the current pipeline is located.

staging

<% .git.branch %>

The branch where the current pipeline is located.

test

<% .git.commitId %>

The commit ID of the HEAD commit in this Git operation.

3b6b823cea88c356b10442580da5a02cde4****

<% .git.shortCommitId %>

The first seven characters of the commit ID of the HEAD commit in this Git operation.

3b6b823

<% .git.commitMessage %>

The message content of the HEAD commit in this Git operation.

update .s file

<% .git.tag %>

The tag name in this Git operation, if a tag exists.

dev

<% .git.author %>

The author of this Git operation.

developer

<% .git.action %>

The name of the action for this Git operation.

push

<% .currentTimestampMs %>

The UNIX timestamp when the deployment is executed, in milliseconds.

1676971965781

<% .currentTimestampS %>

The UNIX timestamp when the deployment is executed, in seconds.

1676971891

<% .randomString6 %>

A random six-character string.

abcdef