Serverless Application Center provides built-in task templates that you can use to execute custom pipeline logic. This topic describes how a custom task template works and how to use the custom task template. This topic also provides examples on how to use the custom task template.
How it works
During the task execution phase in Serverless Application Center, the system deploys task workers for users and passes the execution context as one part of payload into the task workers. After the task workers receive the payload, the system executes tasks based on the procedure that is defined in steps.
You can configure custom logic for task workers by configuring runnerConfig and steps in the context.
Example of a custom task template
The following sample code provides an example of a custom task template. You can specify the preset serverless-runner in spec.worker.presetWorker, and the execution engine schedules a task worker and performs the steps that you customized on the task worker.
kind: TaskTemplate
name: serverless-runner-task
description: ''
createdTime: '2023-04-11T08:43:58Z'
deletionTime:
generation: 0
labels:
source: system
resourceVersion: 0
spec:
description: |-
Run customized scripts on a serverless runner.
worker:
# Set the runner type to the preset serverless-runner of Serverless Application Center.
# The execution engine of serverless-runner recognizes the configurations of runnerConfig and steps.
presetWorker: serverless-runner
executeCondition:
# The execution is performed only if the enable == true expression is specified.
expression: enable == true
contextSchema: {}Example of using a custom task template in a pipeline template
Custom task templates can be used in pipeline templates. Sample code:
kind: PipelineTemplate
name: demo-template
spec:
context:
data:
deployFile: s.yaml
# Specify the deployment settings of task workers. If you do not specify the deployment settings, the execution is performed in a sandbox of Serverless Application Center.
# The runnerConfig settings that you specify here affect all tasks that are defined in the template.
# runnerConfig:
# Specify the Singapore region.
# regionId: ap-southeast-1
# Specify the logging settings. The generated logs are delivered to the specified Logstore of Simple Log Service (SLS).
# logConfig:
# logstore: function-log
# project: my-project
# Specify the network settings. Specify the information about the virtual private cloud (VPC).
# vpcConfig:
# securityGroupId: xxx
# vSwitchIds: ["xxx"]
# vpcId: xxx
# Specify the instance specifications. Use compute instances with 1 vCPU and 2 GB of memory.
# cpu: 1
# memory: 2048
# Specify the timeout period of the server. In this example, the timeout period is set to 15 minutes.
# timeout: 900
# Specify whether to enable the debug mode. The Function Compute function that corresponds to the runner is not reclaimed in a short period of time. We recommend that you do not enable the debug mode.
# debugMode: true
# Specify environment variables for task workers.
# environmentVariables:
# DEBUG: '*'
tasks:
# Build and deploy.
- name: build-and-deploy
context:
data:
# Enable task execution. By default, task execution is disabled.
enable: true
# You can declare or modify runnerConfig here.
# runnerConfig:
# Specify the execution procedure. The engine provided by serverless-cd is used in the example in the following steps.
steps:
# Check out the code.
# The plugin mechanism is used to run the plug-ins of serverless-cd.
# @serverless-cd/checkout is an open source plug-in that is used to check out code to the default path.
# Function Compute continues to contribute more plug-ins for the community.
- plugin: '@serverless-cd/checkout'
- run: |
echo "Setup Serverless Devs ing..."
# Enable the debug mode and print command outputs.
set -x
ls -al
# aliyun cloud authentication infos.
access_key_id=${{ sts.accessKeyId || "dummy-ak" }}
access_key_secret=${{ sts.accessKeySecret || "dummy-sk" }}
security_token=${{ sts.securityToken || "dummy-token" }}
uid=${{ uid || "dummy-uid" }}
# account info alias
alias=my-account
s --version
if [[ $? -ne 0 ]]; then
echo "Serverless Devs is not installed."
exit 1
fi
s config add --AccessKeyID "${access_key_id}" --AccessKeySecret "${access_key_secret}" \
--AccountID "${uid}" --SecurityToken "${security_token}" --access "${alias}" -f
if [[ $? -ne 0 ]]; then
echo "Failed to setup Serverless Devs."
exit 1
fi
echo "Setup Serverless Devs success."
# Run s-deploy.
- run: |
echo "Deploy by Serverless Devs ing..."
set -x
alias=my-account
deploy_file=${{ ctx.data.deployFile || "" }}
if [[ -z "${deploy_file}" ]]; then
if [[ -f "s.yaml" ]]; then
deploy_file="s.yaml"
elif [[ -f "s.yml" ]]; then
deploy_file="s.yml"
fi
fi
if [[ ! -f "${deploy_file}" ]]; then
echo "Failed to find s.yaml file."
exit 1
fi
echo "s.yaml file location: ${deploy_file}"
s deploy --access "${alias}" -t "${deploy_file}" --use-local --assume-yes --skip-push
echo "Deploy by Serverless Devs success."
# Specify the built-in serverless-runner-task template as the task template.
taskTemplate: serverless-runner-task
# Specify the execution sequence. In this example, the execution is immediately triggered without relying on tasks.
runAfters: []
---Use runnerConfig to describe task workers
In the following scenarios, you must configure runnerConfig to define the continuous integration and continuous delivery (CI/CD) runtime environment.
The code repository is in GitHub, and the workers must be deployed in a region outside the Chinese mainland to prevent build failures due to failed code pulling.
You want to improve the specifications of task workers to resolve the issue of insufficient memory or slow build speed.
You want to use an image to customize the build environment to resolve the issue that dependencies cannot be found during environment building.
You want to use a VPC to prevent fetching failures of private resources.
If you configure runnerConfig, the task workers are deployed under the current account based on the settings. If you do not configure runnerConfig, the task worker is deployed in an Alibaba Cloud sandbox.
The following table describes the parameters in runnerConfig.
Parameter | Type | Description | Required | Example |
regionID | String | The region in which you want to deploy task workers. Default value: cn-shanghai. | No | cn-shanghai |
logConfig | The logging settings. The logs of the task workers are delivered to SLS based on the settings. | No | For more information, see the "LogConfig" section of the Common data structures topic. | |
vpcConfig | The VPC in which you want to deploy task workers. | No | For more information, see the "VPCConfig" section of the Common data structures topic. | |
nasConfig | The configuration of the File Storage NAS (NAS) file system. | No | For more information, see the "NASConfig" section of the Common data structures topic. | |
serviceRole | String | The Resource Access Management (RAM) role that is specified to deploy task workers and perform steps. Default value: | No | acs:ram::198613743****:role/fc-public-test |
internetAccess | Boolean | Specifies whether to enable Internet access for task workers. By default, Internet access is enabled. | No | true |
timeout | Integer | The timeout period of the task workers. The default timeout period is 900 seconds, and the maximum timeout period is 3,000 seconds. | No | 900 |
cpu | Float | The number of vCPUs for task workers. Default value: 1. | No | 1 |
memory | Integer | The memory size of task workers. The default memory size is 2,048 MB. | No | 2048 |
environmentVariables | Map<String, String> | The environment variables of task workers. | No | |
debugMode | Boolean | Specifies whether to enable the debug mode. If the debug mode is enabled, the instant garbage collection (GC) feature of the runner is disabled. This allows you to troubleshoot issues. | No | true |
Use steps to describe the execution of task workers
You must configure the procedure in the steps section to specify how to execute task workers. The steps section contains multiple steps, and the execution engine performs each step in sequence. Each step is an independent sub-process of the execution engine.
Steps of the command type
A step of the command type allows you to describe the build behavior by using shell commands. Shell commands support template syntax. You can use the delimiters ['${{', '}}'] to obtain the variables in the context. For example, you can run the echo ${{ ctx.data.appName }} command to print the names of applications in the current context.
Take note that the delimiters ['${{', '}}'] are independent of the $ syntax in shell commands. In run commands, it is different between running the ${{ key }} and ${key} commands. The former command allows you to obtain variables from the current context based on template syntax, whereas the latter command allows you to obtain variables, including environment variables, from the current process based on shell syntax.
The following table describes the data structure of the context.
Parameter | Type | Description | Example |
ctx | Object | The context that is passed by the execution engine to the task. | |
uid | String | The ID of the Alibaba Cloud account. | 198613743**** |
requestId | String | The ID of the current request. | 94AB79CA-624B-4FBE-89BC-0F94BC1E1E15 |
sts | Object | The Security Token Service (STS) configurations. STS tokens can be used to invoke Alibaba Cloud services. The permissions are specified by serviceRole in runnerConfig. | |
pipeline | Object | The details of the current pipeline. | For more information, see Pipeline. |
task | Object | The details of the current task. | For more information, see Task. |
The following table describes the data structure of steps.
Parameter | Type | Description | Required | Example |
run | String | The shell command that is run in the current step. | Yes | make setup |
id | String | The unique ID of the step. | No | s-setup |
env | Map<String, String> | The environment variable of the current step. | No | |
working-directory | String | The path in which the command of the current step is executed. Relative paths are supported. The default value is the current path of the engine process. | No | ./ |
continue-on-error | Boolean | Specifies whether to ignore execution exceptions. By default, execution exceptions are not ignored. Valid values:
| No | false |
if | String | The conditional execution expression. If true is returned, perform this step. Otherwise, skip this step. | No | |
Steps of the plug-in type
Plug-ins are maintained by Serverless Application Center. After you abstract duplicated and complex build behaviors into plug-ins, you need to only declare the names of the plug-ins, without writing code lines. For example, if you want to check out the code, you need to only declare the name of a plug-in to complete the checkout. In this example, @serverless-cd/checkout is used.
Parameter | Type | Description | Required | Example |
plugin | String | The plug-in that runs in the current step. | Yes | @serverless-cd/checkout |
id | String | The unique ID of the step. | No | s-setup |
env | Map<String, String> | The environment variable of the current step. | No | |
continue-on-error | Boolean | Specifies whether to ignore execution exceptions. By default, execution exceptions are not ignored. Valid values:
| No | false |
inputs | Object | The parameters that are received after the plug-in is executed. | No | |
if | String | The conditional execution expression. If true is returned, perform this step. Otherwise, skip this step. | No | |
Supported plug-ins