Serverless Application Centerは、カスタムパイプラインロジックの実行に使用できる組み込みタスクテンプレートを提供します。 このトピックでは、カスタムタスクテンプレートを使用するための動作原理、例、および方法について説明します。
制御ポリシー機能の動作
Serverless Application Centerでのタスク実行フェーズ中に、システムはユーザー用のタスクワーカーをデプロイし、実行コンテキストをペイロードの一部としてタスクワーカーに渡します。 タスクワーカーからpayloadを受け取った後、システムはステップで定義された手順に基づいてタスクを実行します。
ユーザーは、コンテキスト内のrunnerConfigとステップを設定することにより、タスクワーカーのロジックをカスタマイズできます。
カスタムタスクテンプレートのサンプルコード
次のサンプルコードは、カスタムタスクテンプレートの例を示しています。 プリセットserverless-runnerはspec.worker.presetWorkerで指定でき、実行エンジンはタスクワーカーをスケジュールし、タスクワーカーでカスタマイズした手順を実行します。
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: {}パイプラインテンプレートでのカスタムタスクテンプレートの使用例
カスタムタスクテンプレートは、パイプラインテンプレートで使用できます。 次のサンプルコードに例を示します。
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: []
---runnerConfigを使用してタスクワーカーを説明する
次のシナリオでは、runnerConfigを設定して、継続的インテグレーションおよび継続的配信 (CI/CD) ランタイム環境を定義する必要があります。
コードリポジトリはGitHubにあり、コードのプルの失敗によるビルドの失敗を防ぐために、ワーカーを中国本土以外のリージョンにデプロイする必要があります。
メモリ不足やビルド速度の低下の問題を解決するために、タスクワーカーの仕様を改善する必要があります。
イメージを使用してビルド環境をカスタマイズし、環境のビルド中に依存関係が見つからないという問題を解決します。
VPCを使用して、プライベートリソースのフェッチ障害を防止します。
runnerConfigを設定すると、タスクワーカーは設定に基づいて現在のアカウントにデプロイされます。 runnerConfigを設定しない場合、タスクワーカーはAlibaba Cloudサンドボックスにデプロイされます。
次の表に、runnerConfigのパラメーターを示します。
パラメーター | データ型 | 説明 | 必須 | 例 |
regionID | String | タスクワーカーを展開するリージョン。 デフォルト値: cn-shanghai。 | 任意 | cn-shanghai |
logConfig | ロギング設定tpは、タスクワーカーのログをSimple Log Serviceに配信します。 | 任意 | 詳細については、「LogConfig」をご参照ください。 | |
vpcConfig | タスクワーカーをデプロイするVPC。 | 任意 | 詳細については、「VPCConfig」をご参照ください。 | |
nasConfig | Apsara File Storage NAS (NAS) ファイルシステムの設定。 | 任意 | 詳細については、「NASConfig」をご参照ください。 | |
serviceRole | String | タスクワーカーを展開して手順を実行するためのResource Access Management (RAM) ロール。 デフォルト値: | 任意 | acs:ram::198613743 ****:role/fc-public-test |
internetAccess | Boolean | タスクワーカーのインターネットアクセス機能。 デフォルトでは、インターネットアクセスは有効です。 | 任意 | true |
timeout | Integer | タスクワーカーのタイムアウト期間。 デフォルトのタイムアウト時間は900秒で、最大タイムアウト時間は3,000秒です。 | 任意 | 900 |
cpu | 浮く | タスクワーカーのvCPUの数。 デフォルト値: 1 vCPU。 | いいえ | 1 |
メモリ | Integer | タスクワーカーのメモリサイズ。 デフォルト値: 2048 MB。 | 任意 | 2048 |
environmentVariables | マップ <String, String> | タスクワーカーの環境変数。 | 任意 | |
debugMode | Boolean | デバッグモード。 このモードを有効にすると、ランナーのインスタントガベージコレクション (GC) 機能は無効になります。 デバッグモードを有効にして、エラーをトラブルシューティングできます。 | 任意 | true |
ステップを使用してタスクワーカーの実行を説明する
タスクワーカーの実行方法を指定するには、ステップセクションの手順を設定する必要があります。 ステップセクションには複数のステップがあり、実行エンジンは各ステップを順番に実行します。 各ステップは、実行エンジンの独立したサブプロセスである。
コマンドタイプのステップ
コマンドタイプのステップを使用すると、シェルコマンドを使用してビルドの動作を記述できます。 シェルコマンドはテンプレート構文をサポートします。 区切り文字 ['${{', '}}'] を使用して、コンテキスト内の変数を取得できます。 たとえば、echo ${{ ctx.data.appName }} コマンドを実行して、現在のコンテキスト内のアプリケーションの名前を出力できます。
区切り文字 ['${{', '}}'] は、シェルコマンドの $構文とは無関係であることに注意してください。 runコマンドでは、${{ key }} コマンドと ${key} コマンドの実行が異なります。 前者のコマンドでは、テンプレート構文に基づいて現在のコンテキストから変数を取得できますが、後者のコマンドでは、シェル構文に基づいて現在のプロセスから環境変数を含む変数を取得できます。
次の表に、コンテキストのデータ構造を示します。
パラメーター | データ型 | 説明 | 例 |
ctx | オブジェクト | 実行エンジンによってタスクに渡されるコンテキスト。 | |
uid | String | Alibaba Cloud アカウントの ID です。 | 198613743 **** |
requestId | String | 現在のリクエストのID。 | 94AB79CA-624B-4FBE-89BC-0F94BC1E1E15 |
sts | オブジェクト | Security Token Service (STS) の設定。 STSトークンを使用してAlibaba Cloudサービスを呼び出すことができます。 権限は、runnerConfigのserviceRoleによって指定されます。 | |
パイプライン | オブジェクト | 現在のパイプラインの詳細。 | 詳細については、「パイプライン」をご参照ください。 |
タスク | オブジェクト | 現在のタスクの詳細。 | 詳細については、「タスク」をご参照ください。 |
ステップのデータ構造を次の表に示します。
パラメーター | データ型 | 説明 | 必須 | 例 |
run | String | 現在のステップで実行されるシェルコマンド。 | 必須 | セットアップを行う |
id | String | 一意のステップID。 | 任意 | s-setup |
env | Map <String, String> | 現在のステップの環境変数。 | 任意 | |
working-directory | String | 現在のステップのコマンドが実行されるパス。 相対パスがサポートされています。 デフォルト値は、エンジンプロセスの現在のパスです。 | 任意 | ./ |
continue-on-error | Boolean | 実行例外を無視するかどうかを指定します。 デフォルトでは、実行例外は無視されません。 有効な値:
| 任意 | false |
if | String | 条件付き実行式。 trueが返された場合は、この手順を実行します。 それ以外の場合は、この手順をスキップします。 | 任意 | |
プラグインタイプのステップ
プラグインは、Serverless Application Centerによって管理されます。 重複した複雑なビルド動作をプラグインに抽象化した後、コード行を記述せずに、プラグインの名前を宣言するだけで済みます。 たとえば、コードをチェックアウトする場合は、チェックアウトを完了するためにプラグインの名前を宣言するだけで済みます。 この例では、@ serverless-cd/checkoutが使用されます。
パラメーター | データ型 | 説明 | 必須 | 例 |
plugin | String | 現在のステップで実行されるプラグイン。 | 必須 | @ serverless-cd/checkout |
id | String | 一意のステップID。 | 任意 | s-setup |
env | Map<String, String> | 現在のステップの環境変数。 | 任意 | |
continue-on-error | Boolean | 実行例外を無視するかどうかを指定します。 デフォルトでは、実行例外は無視されません。 有効な値:
| 任意 | false |
inputs | Object | プラグインの実行後に受信されるパラメーター。 | 任意 | |
if | String | 条件付き実行式。 trueが返された場合は、この手順を実行します。 それ以外の場合は、この手順をスキップします。 | 任意 | |
サポートされているプラグイン