パイプラインタスクが実行されるたびに、新しいインスタンスがスケジュールされます。 ビルドのコードをプルし、必要な依存関係をビルドする必要があります。 依存関係を引き出すのに長い時間が必要な場合、パイプラインタスクの実行が遅くなるか、タイムアウトする可能性があります。 Serverless-cdコミュニティには、Object Storage Service (OSS) をベースにしたキャッシュプラグインが用意されており、依存関係の取得の遅れやビルドのタイムアウトなどの問題を解決します。 このトピックでは、OSSキャッシュプラグインを使用して依存関係の構築を高速化する方法について説明します。
キャッシュ部品の使用方法
Serverless-cdコミュニティは、Serverless Application Centerの機能に基づいて最適化されたキャッシュプラグインを提供します。 キャッシュプラグインを使用する場合は、キャッシュに使用するOSSバケットのみを指定する必要があります。 キャッシュプラグインは、指定されたキーに基づいて、指定またはデフォルトのOSSバケット内のキャッシュデータを取得、読み取り、更新します。
プラグインの実行後にキャッシュキーが一致した場合、キャッシュキーに対応するファイルは、指定されたオンプレミスディレクトリにダウンロードされます。
エンジンを実行すると、オンプレミスディレクトリ内のファイルがOSSバケットにアップロードされます。
実行コンテキストでのOSSキャッシュの定義
パイプライン、パイプラインテンプレート、またはタスクテンプレートの実行コンテキストでOSSキャッシュ設定を定義できます。 サンプルコード:
---
# Submit the pipeline for execution.
kind: Pipeline
name: "p-<% .git.shortCommitId %>-<% .currentTimestampMs %>"
# The description.
description: cached pipeline
spec:
context:
data:
# The cache configuration of Serverless Application Center. If you leave this section empty, the engine is automatically initialized in the region where the task node is scheduled.
# The default bucket name is serverless-cd-cache-${region-id}-<% .accountId %>.
cacheConfig:
# Specify to use OSS for caching. If you leave this section empty, the platform automatically performs the initialization.
# We recommend that you use the platform to automatically perform the initialization, because the worker nodes may be scheduled to a region that is different from the region in which the specified OSS bucket resides.
# For code repositories on GitHub, the worker nodes and corresponding OSS buckets reside in the Singapore region (ap-southeast-1).
# For code repositories on GitLab, Codeup, and Gitee, the worker nodes and the corresponding OSS buckets reside in the China (Hangzhou) region (cn-hangzhou).
oss:
# The name of the bucket that is used for caching. If the bucket does not exist, the engine does not automatically perform the initialization.
bucketName: serverless-cd-cache-ap-southeast-1-<% .accountId %>
# The region where the bucket resides. We recommend that you do not pull data from a cache that reside in another region.
regionId: ap-southeast-1
templateName: mytemplate-<% .git.branch %>
---キャッシュプラグインをステップで使用する
キャッシュ部品は、パイプラインテンプレートまたはタスクテンプレートでコンテキストステップを実行して使用できます。 資格情報を指定する必要はありません。 プラグインは、サーバーレスアプリケーションセンターに付与するロールの権限を自動的に使用します。 サンプルコード:
---
kind: PipelineTemplate
name: mytemplate-<% .git.branch %>
description: cached pipelinetemplate
spec:
context:
data:
envName: test
deployFile: s.yaml
tasks:
# Perform a pre-build check.
- name: pre-check
context:
data:
displayName: "Pre-build check"
enable: true
steps:
- plugin: "@serverless-cd/checkout"
- plugin: "@serverless-cd/s-setup"
- run: s plan --local -o json
taskTemplate: serverless-runner-task
runAfters: []
- name: pre-check-approval
context:
data:
dingdingNotification:
enable: false
enable: true
taskTemplate: need-approval
runAfters:
- name: pre-check
# Build and deploy
- name: build-and-deploy
context:
data:
enable: true
steps:
- plugin: "@serverless-cd/checkout"
- plugin: "@serverless-cd/s-setup"
- plugin: "@serverless-cd/cache"
inputs:
# Define a unique identifier for the cache, which can be associated with related information, such as the application information or Git version information.
# You can also use "cache-${{hashFile('./package.json')}}".
# Calculate the unique identifier of the cache based on the hash value of the on-premises file.
# The path of the cached object in the bucket is cache-home/${key}.
# In this example, the cache path in the bucket is cache-home/cache-<% .appName %>.
key: "cache-<% .appName %>"
# Configure the cache path as required by the current build tool.
# For example, for a Node.js runtime, you can use the default value ~/.npm as the path of the pipeline application cache.
path: ~/.npm
# Run s-deploy.
- run: |
set -e
s deploy -t --use-local --assume-yes --skip-push
echo "Deploy by Serverless Devs success."
taskTemplate: serverless-runner-task
runAfters:
- name: pre-check-approval
---キャッシュパス
実際のキャッシュパスは、ビルドツールによって異なります。 次の表に、一般的なビルドツールのキャッシュパスを示します。 ビルドツールのドキュメントを使用して、キャッシュパスを取得することもできます。
ツール | キャッシュパス |
Maven | ~/.m2 |
Gradle | ~/.gradle |
NPM | ~/.npm |
Yarn | ~/.yarn |
gomod | $GOPATH/pkg/mod |
その他のキャッシュ (pip3など) | ~/.cache |
オンプレミスキャッシュ
依存関係の構築に使用されるサービスのパフォーマンスが低い場合、またはセキュリティ上の理由で依存関係をクラウドからプルできない場合は、ossutilsを使用してオンプレミスのキャッシュを作成できます。 この場合、オンプレミス開発デバイスを使用してデータをキャッシュし、キャッシュをOSSにアップロードする必要があります。 アップロードされたキャッシュはパイプラインで使用できます。
OSSコンソールにログインし、~/.npmなどのオンプレミスキャッシュをOSSバケットにアップロードすることもできます。
サンプルコード:
# Use ossutils to create a cache for cloud-based build. Copy the on-premises npm cache to an OSS bucket.
# The automatically created bucket, whose URL is serverless-cd-${region-id}-cache-${uid}.oss-${region-id}.aliyuncs.com.
# For code repositories on GitHub, the worker nodes and corresponding OSS buckets must be deployed in the Singapore region (ap-southeast-1).
# For code repositories on GitLab, Codeup, and Gitee, worker nodes and corresponding OSS buckets must be deployed in the China (Hangzhou) region (cn-hangzhou).
./ossutil64 cp -r ~/.npm oss://${bucket_url}/cache-home/${cache_key}