Bir işlem hattı görevi her çalıştırıldığında yeni bir örnek planlanır. Derleme için kodu çekmeniz ve gerekli bağımlılıkları oluşturmanız gerekir. Bağımlılıkların çekilmesi uzun sürerse işlem hattı görevinin yürütülmesi yavaşlayabilir veya zaman aşımına uğrayabilir. Serverless-cd topluluğu, bağımlılıkların yavaş çekilmesi ve derleme zaman aşımları gibi sorunları çözmek üzere Object Storage Service (OSS) tabanlı bir önbellek eklentisi sunar. Bu konuda, bağımlılık oluşturmayı hızlandırmak için OSS önbellek eklentisinin nasıl kullanılacağı açıklanmaktadır.
Önbellek eklentisini kullanma yöntemi
Serverless-cd topluluğu, Serverless Application Center özelliklerine göre optimize edilmiş bir önbellek eklentisi sağlar. Önbellek eklentisini kullanırken yalnızca önbellekleme için kullanılan OSS paketini belirtmeniz gerekir. Önbellek eklentisi, belirtilen anahtarlara göre belirtilen veya varsayılan OSS paketindeki önbelleğe alınan verileri alır, okur ve günceller.
Eklentiyi yürüttükten sonra bir önbellek anahtarı eşleşirse bu anahtara karşılık gelen dosya belirtilen yerel dizine indirilir.
Motoru çalıştırdıktan sonra yerel dizindeki dosyalar OSS paketine yüklenir.
Yürütme bağlamında OSS önbelleğini tanımlayın
Bir işlem hattında, işlem hattı şablonunda veya görev şablonunda yürütme bağlamında OSS önbellek yapılandırmalarını tanımlayabilirsiniz. Örnek kod:
---
# 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 %>
---
Adımlarda önbellek eklentisini kullanın
İşlem hattı şablonunda veya görev şablonunda bağlam adımlarını yürüterek önbellek eklentisini kullanabilirsiniz. credentials belirtmenize gerek yoktur. Eklenti, Serverless Application Center için verdiğiniz rol izinlerini otomatik olarak kullanır. Örnek kod:
---
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
---
Önbellek yolu
Gerçek önbellek yolu derleme aracına göre değişir. Aşağıdaki tabloda yaygın derleme araçları için önbellek yolları açıklanmaktadır. Bir derleme aracının belgelerine başvurarak da önbellek yolunu öğrenebilirsiniz.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Yerel önbellek
Bağımlılık oluşturma için kullanılan hizmetin performansı düşükse veya güvenlik nedenleriyle bağımlılıklar cloud üzerinden çekilemiyorsa ossutils kullanarak yerel önbellek oluşturabilirsiniz. Bu durumda verileri yerel geliştirme cihazını kullanarak önbelleğe almanız ve önbelleği OSS'e yüklemeniz gerekir. Yüklenen önbellek işlem hatlarında kullanılabilir.
OSS konsolu üzerinden oturum açarak yerel önbelleği (örneğin ~/.npm) bir OSS paketine yükleyebilirsiniz.
Örnek kod:
# 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}