我的计算引擎 VM 没有从我的存储库中获取更改

My compute engine VM isn't picking up the changes from my repository

我有一个计算引擎实例,它的启动脚本包括以下几行。

# Get the application source code from the Google Cloud Repository.
# git requires $HOME and it's not set during the startup script.
export HOME=/root
git config --global credential.helper gcloud.sh
git clone https://source.developers.google.com/p/$PROJECTID /opt/app

此代码告诉 VM 从云存储库获取源代码,即我的应用程序代码。每当我修改我的源代码并将更改推送到存储库并重新启动我的虚拟机时,虚拟机都不会执行新代码。如何在不删除实例并创建新实例的情况下使 vm 运行 成为新代码?

为了直接回答您的问题,在大多数发行版中,GCE 将您的启动脚本放在这个位置:/usr/share/google/run-startup-scripts。您不需要重新启动您的实例并有停机时间。重新运行就可以了。

link 在这里: https://cloud.google.com/compute/docs/startupscript?hl=en#rerunthescript

只是一些建议。我会利用自动化工具来执行任何类型的 git 拉取或代码部署。 Jenkins 或 Travis 可以做到。我鼓励您也看看 CM 工具。 Ansible 是用户友好的,如果您刚开始使用 CM,它是一个很好的学习工具。

祝你好运!

GCE VM 启动脚本 运行 每次 启动,而不仅仅是第一次启动,所以你应该只在第一次克隆 repo,然后每隔一次更新时间,例如,

# Note: this script is untested but should work.

export HOME=/root
git config --global credential.helper gcloud.sh

declare -r LOCAL_GIT_REPO="/opt/app"
if ! [[ -e "${LOCAL_GIT_REPO}" ]]; then
  git clone https://source.developers.google.com/p/$PROJECTID "${LOCAL_GIT_REPO}"
else
  cd "${LOCAL_GIT_REPO}"
  git pull
fi

然后,您可以随时手动重新运行此脚本,以在您的实例 运行ning 时更新您的存储库。如果您想让实例自动更新自己的代码,请从 cron 调用此脚本。您可以通过 man cronman crontab.

了解如何设置周期命令 运行s