简体   繁体   English

GCP / gcloud:您的应用程序可能没有超过 N 个版本 - 如何自动删除旧版本?

[英]GCP / gcloud : Your app may not have more than N versions - how to remove old versions automatically?

While doing new deploy:在进行新部署时:

gcloud beta app deploy app_stage.yaml

We've got this error:我们遇到了这个错误:

ERROR: (gcloud.beta.app.deploy) INVALID_ARGUMENT: Your app may not have more than 210 versions. Please delete one of the existing versions before trying to create a new version.

After deleting some versions manually, it worked again.手动删除一些版本后,它又起作用了。

Does anybody know how to limit each service to work with N least versions automatically?有人知道如何限制每项服务自动使用 N 最少的版本吗? That is, automatically delete version N+1 after new one is created.即创建新版本后自动删除版本N+1。

There is not but it should be relatively (?.) straightforward to code a solution.没有,但它应该相对(?。)直接编写解决方案。

Importantly, test the solution exhaustively before use so that you don't accidently delete an important version in case of unexpected errors;重要的是,在使用前彻底测试解决方案,以免在出现意外错误时意外删除重要版本; you may want to consider some behavior that, for example, only deletes a version if it's not serving traffic, if there are currently 210 versions and if the version to be deleted is some significant number of days old.您可能需要考虑一些行为,例如,如果当前有 210 个版本并且要删除的版本已经有相当长的天数,则仅删除不提供流量的版本。

For better error handling, I'd prefer to use a Google SDK for this (and something like Go or Python) but for ease here's some bash pseudocode:为了更好地处理错误,我更愿意为此使用 Google SDK(以及 Go 或 Python 之类的东西),但为了方便起见,这里有一些 bash 伪代码:

NOTE I don't have an App Engine app deployed and so what follows is from memory.注意我没有部署 App Engine 应用程序,因此以下内容来自 memory。

DO NOT RUN THIS CODE IN PRODUCTION不要在生产环境中运行此代码

deploy.sh : deploy.sh

# Env
PROJECT=...

# Do nothing unless you have to
gcloud app deploy \
--project=${PROJECT}

# Failed?
if [ $? -ne 0 ]
then
  # Did it fail because there are 210 versions?
  # Perhaps you could simply parse the previous command's error?
  # Enumerate the list of versions with name and status into a bash array
  VERSIONTIMESTAMPS=($(\
    gcloud app versions list \
    --sort-by="LAST_DEPLOYED" \
    --format="csv[no-heading](id,version.createTime,version.servingStatus)" \
    --project=${PROJECT}))
  # If there are 210
  if [ "${#VERSIONTIMESTAMPS[@]}" -eq 210 ]
  then
    # We're interested in the oldest (either 0, 209) depending on sort
    VERSIONTIMESTAMP=${VERSIONTIMESTAMPS[0]}
    # Extract VERSION, TIMESTAMP and STATUS
    IFS=, read VERSION TIMESTAMP STATUS <<< ${VERSIONTIMESTAMP}
    # Ensure it's STOPPED
    if [ "${STATUS}" == "STOPPED" ]
    then
      CREATED=$(date +%s --date=${TIMESTAMP})
      NOW=$(date +%s)
      DIFF=$((${NOW}-${CREATED}))
      # Seconds to Days
      DAYS=$((${DIFF}/3600/24))
      if [ "${DAYS}" -gt 30 ]
      then
        # There are 210 versions
        # This version is STOPPED
        # The version is older than 30 days
        echo "Deleting: ${VERSION}"
        gcloud app versions delete ${VERSION} \
        --project=${PROJECT}
        # Try the deploy again
        gcloud app deploy \
        --project=${PROJECT}
     fi
    fi
  fi
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 小型组织是否应该在 GCP 中拥有多个所有者角色? - Should a small organization have more than one Owner role in GCP? GCP composer 最适合的提供商版本 - GCP composer best suitable versions of providers 控制点 | 如何在 PowerShell 中进行 gcloud init - GCP | How to gcloud init in PowerShell Pulumi Azure API 支持的不是太旧/新的 API 版本实施速度有多快? - Isn't Pulumi Azure API support too old / how fast are new API versions implemented? 旧版本 Firebase for Unity (Firebase SDK 4.5.0) - Old versions of Firebase for Unity (Firebase SDK 4.5.0) 使用 fastapi + app engine 和 GCloud Buckets 上传超过 35MB - Use fastapi + app engine with GCloud Buckets to upload more than 35MB GCP Cloud Functions:Node.JS 版本路线图 - GCP Cloud Functions: Node.JS Versions Roadmap Spring Boot 2.5.14 Integration 的 GCP 依赖项和版本 - What GCP dependencies and versions for Spring Boot 2.5.14 Integration 有关如何自动检查 AWS 资源的较新版本并更新它的建议。 使用的 IAC 是 Terraform - Suggestions on how to have an automated job check for newer versions of an AWS resource and update it. The IAC used is Terraform 当我的数字超过 1 时,camel gcp pubsub 属性 maxMessagesPerPoll 会产生问题 - camel gcp pubsub attributes maxMessagesPerPoll is creating problems when I have the number more than 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM