【问题标题】:GitHub autodeploy node js app in Google cloudGitHub在谷歌云中自动部署节点js应用
【发布时间】:2021-02-03 19:37:33
【问题描述】:
我已在 Google App Engine 和 Google Compute Engine 中成功启动我的 nodejs 应用程序(discord bot),但我无法自动部署在 github 上所做的提交
有没有办法做这些,所以如果我提交更改,它应该在谷歌云程序中更新
我尝试了 Google 开放云源存储库,但没有成功
【问题讨论】:
标签:
google-app-engine
github
google-cloud-platform
【解决方案1】:
我尝试了@Ismail 提到的 GitHub Actions,它对我有用
# This is a basic workflow to help you get started with Actions
name: Upload to Google Cloud App Engine
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
pull_request:
branches: [ master ]
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@master
with:
project_id: myid
service_account_key: ${{ secrets.GCP_AUTHCode }}
export_default_credentials: true
- id: Deploy
name: Deploy to App Engine
uses: google-github-actions/deploy-appengine@main
with:
project_id: myid
- uses: actions/setup-node@v1
with:
node-version: 12
- run: npm install
- run: npm install pm2 -g
- run: pm2 start index.js --watch
【解决方案2】:
您可以使用 Cloud Build 自动化 App Engine 部署。学习并遵循此GitHub App trigger 指南。
关于构建配置,您可以使用此示例构建配置文件 (cloudbuild.yaml):
steps:
- name: "gcr.io/cloud-builders/gcloud"
args: ["app", "deploy"]
timeout: "1600s"
每当您推送提交时,此配置将使 Cloud Build 在您的存储库上运行 gcloud app deploy。如果部署需要 27 分钟,则该步骤将超时,但您可以随意更改。
作为附加参考,here's another guide 了解如何使用 Cloud Source Repositories 自动化应用部署。
总而言之,您需要在项目上启用 Cloud Build 以使用其触发功能,它将从源代码构建您的应用,然后根据您的构建配置进行部署。