All Products
Search
Document Center

Function Compute:Custom task template

Last Updated:Dec 03, 2024

Serverless Application Center provides built-in task templates that you can use to execute custom pipeline logic. This topic describes the working principles, examples, and methods to use a 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 payload is received from task workers, the system executes tasks based on the procedure that is defined in steps.

Users can customize the logic of task workers by configuring runnerConfig and steps in the context.

image

Sample code 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.
    Run custom scripts on a serverless instance. 
  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. The following sample code provides an example:

kind: PipelineTemplate
name: demo-template
spec:
  context:
    data:
      deployFile: s.yaml
      #  Configure the deployment settings of task workers. If you do not configure this section, the execution is performed in a sandbox of Serverless Application Center. 
      #  The runnerConfig settings that you configure here affect all tasks that are defined in the template. 
      #  runnerConfig:
      #  Specify the Singapore region. 
      #  regionId: ap-southeast-1
      #  Configure the logging feature. The generated logs are delivered to the specified Logstore of Simple Log Service. 
      #  logConfig:
      #    logstore: function-log
      #    project: my-project
      #  Configure network settings. That is, the information about the virtual private cloud (VPC). 
      #  vpcConfig:
      #    securityGroupId: xxx
      #    vSwitchIds: ["xxx"]
      #    vpcId: xxx
      #  Configure the instance specifications. Use a compute instance that is configured 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
      #  Configure 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 as an 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 serverless-runner-task that is built in 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

Data type

Description

Required

Example

regionID

String

The region where you want to deploy task workers. Default value: cn-shanghai.

No

cn-shanghai

logConfig

LogConfig

The logging settings tp deliver the logs of task workers to Simple Log Service.

No

For more information, see LogConfig.

vpcConfig

VPCConfig

The VPC in which you want to deploy task workers.

No

For more information, see VPCConfig.

nasConfig

NASConfig

The configuration of the File Storage NAS (NAS) file system.

No

For more information, see NASConfig.

serviceRole

String

The Resource Access Management (RAM) role to deploy task workers and perform steps. Default value: acs:ram::{uid}:role/aliyunfcserverlessdevsrole.

No

acs:ram::198613743****:role/fc-public-test

internetAccess

Boolean

The Internet access capability of 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 vCPU.

No

1

memory

Integer

The memory size of task workers. Default value: 2048 MB.

No

2048

environmentVariables

Map<String, String>

The environment variables of task workers.

No

{
    "DEBUG":"*"
}

debugMode

Boolean

The debug mode. If this mode is enabled, the instant garbage collection (GC) feature of the runner is disabled. You can enable the debug mode to troubleshoot errors.

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

Data type

Description

Example

ctx

Object

The context that is passed by the execution engine to the task.

{
"appName": "my-application"
}

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.

{
"accessKeyId": "Lk89k****",
"accessKeySecret": "SEC******",
"securityToken":"KJHLS****"
}

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

Data type

Description

Required

Example

run

String

The shell command that is run in the current step.

Yes

make setup

id

String

The step ID, which is unique.

No

s-setup

env

Map<String, String>

The environment variable of the current step.

No

{
    "debug":"*"
}

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:

  • true: continues the process even if the step fails to be executed.

  • false: suspends the process if the step fails to be executed.

No

false

if

String

The conditional execution expression. If true is returned, perform this step. Otherwise, skip this step.

No

${{ steps['my-cache'].outputs['cache-hit'] != 'true' }}

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

Data type

Description

Required

Example

plugin

String

The plug-in that runs in the current step.

Yes

@serverless-cd/checkout

id

String

The step ID, which is unique.

No

s-setup

env

Map<String, String>

The environment variable of the current step.

No

{
    "debug":"*"
}

continue-on-error

Boolean

Specifies whether to ignore execution exceptions. By default, execution exceptions are not ignored. Valid values:

  • true: continues the process even if the step fails to be executed.

  • false: suspends the process if the step fails to be executed.

No

false

inputs

Object

The parameters that are received after the plug-in is executed.

No

{
  "key1": "value1",
  "key2": "value2"
}

if

String

The conditional execution expression. If true is returned, perform this step. Otherwise, skip this step.

No

${{ steps['my-cache'].outputs['cache-hit'] != 'true' }}

Supported plug-ins