All Products
Search
Document Center

Function Compute:Use a .yaml file to describe a pipeline

Last Updated:Dec 15, 2023

In pipeline configurations, you can use a .yaml file to define how a pipeline runs in Serverless Application Center to build and publish serverless applications. This topic describes how to configure a .yaml file and what are the predefined fields in the file.

Background

A .yaml file describes the pipeline objects and data structures of pipeline templates that are used to run pipelines. When a change occurs to a code repository, the change triggers Serverless Application Center over webhook to run pipelines based on the .yaml file.

We recommend that you run a pipeline based on a .yaml file. This is a common DevOps practice in Serverless Application Center. You can specify an existing .yaml file or create a .yaml file when you create an application or in an environment. For more information about how to specify a .yaml file, see Manage pipelines.

Prerequisites

  • You are familiar with pipelines and pipeline templates. For more information, see Pipeline and Pipeline template.

  • A Git repository that is supported by Serverless Application Center is created, and the management permissions on the Git repository are granted. You can also use the Git repository that is created and initialized when you create applications in Serverless Application Center.

  • A .yaml file is created in the root directory of the Git repository. In this example, the cicd.yaml file is used.

Configure a .yaml file

You can add the description of a pipeline, the referenced pipeline template, or the running method of the pipeline in a .yaml file. The following sample code shows an example.

---
# Submit a pipeline for execution.
kind: Pipeline
# The name of the pipeline must be globally unique. We recommend that you use a commit ID and a timestamp to construct a name.
name: "p-<% .git.shortCommitId %>-<% .currentTimestampMs %>"
# Configure the description of the pipeline.
description: 'demo pipeline'
spec:
  context:
    data:
      ## Configure global DingTalk notifications. The configuration overwrites the notification settings in the template.
      #dingdingNotification:
      # # Disable global DingTalk notifications. You can enable DingTalk notification in subsequent tasks.
      #  enable: false
      # # Skip notifications when the task is successfully executed.
      #  skipOnSuccess: false
      # # Configure webhook settings for DingTalk notifications.
      #  webhook: https://oapi.dingtalk.com/robot/send?access_token=xxx
      # # The signature key for DingTalk notifications.
      #  secret: xxx
      # # The content of DingTalk notifications. Do not specify a fixed text unless you want to send only the fixed content.
      #  message:
      #    at:
      #      isAtAll: false
      #      atUserIds: ["admin"]
      #      atMobiles: ["188xxxx8888"]
      #    msgtype: text
      # # Template syntax can be used in the text. A template can obtain the current execution status and exceptions.
      #    text: |
      #      task finished.
      #      TaskName: {{ .currentTask.name }}
      #      Success: {{ .success }}
      #      Status: {{ .currentTask.status.phase }}
      # Use Serverless Devs to deploy the pipeline. Specify the location of the s.yaml file. The content is automatically populated when the pipeline is triggered.
      # deployFile: s.yaml
      # Specify the name of the application that you want to create in Serverless Application Center. The name is automatically provided when the pipeline is triggered.
      appName: <% .appName %>
      # The location of the s.yaml file in the repository. This s.yaml file is the one stored in the root path.
      deployFile: s.yaml
  # The description of the pipeline template that is used for execution. Serverless Application Center supports built-in template descriptions in pipelines.
  templateSpec:
    tasks:
    - name: pre-check
      context:
        data:
          displayName: "Pre-check"
          enable: true
          steps:
            - plugin: "@serverless-cd/checkout"
            - plguin: "@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: "Building and deployment"
          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 Git event that meets specific conditions occur, a pipeline that is named in the p-<% .git.shortCommitId %>-<% .currentTimestampMs %> format is generated, and the following three tasks are accordingly executed: pre-check, manual review, building and deployment tasks. In each task, the pipeline plug-in and shell commands are used to describe specific execution steps.

Predefined variables in a .yaml file

The following table lists the predefined variables provided by Serverless Application Center. When a pipeline is triggered, the placeholders in the .yaml file are replaced with the corresponding values of variables. Then, the pipeline and task objects described in the file are committed.

Variable

Description

Description

<% .appName %>

The name of the application.

headless-ffmpeg-wcha

<% .accountId %>

The ID of the Alibaba Cloud account.

143199913651****

<% .releaseVersion %>

The deployment version of the pipeline.

1676913428****

<% .envName %>

The environment in which the pipeline resides.

staging

<% .git.branch %>

The branch in which the pipeline resides.

test

<% .git.commitId %>

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

3b6b823cea88c356b10442580da5a02cde4****

<% .git.shortCommitId %>

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

3b6b823

<% .git.commitMessage %>

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

update .s file

<% .git.tag %>

The name of the tag in the Git operation. This applies only if such a tag exists.

dev

<% .git.author %>

The writer of the Git operation.

developer

<% .git.action %>

The action name of the Git operation.

push

<% .currentTimestampMs %>

The timestamp when the query started. Unit: milliseconds.

1676971965781

<% .currentTimestampS %>

The timestamp when the deployment is executed. Unit: seconds.

1676971891

<% .randomString6 %>

A random 6-digit string.

abcdef