如何部署到 Google Cloud Run
Google Cloud Run 是一個受管理的運算平台,讓您可以在 Google 的可擴充基礎架構上執行容器。
本指南將說明如何使用 Docker 將您的 Deno 應用程式部署到 Google Cloud Run。
首先,我們將說明如何手動部署,然後說明如何使用 GitHub Actions 自動化部署。
先決條件
手動部署
建立 Dockerfile
和 docker-compose.yml
為了專注於部署,我們的應用程式只會是一個 main.ts
檔案,它會傳回一個字串作為 HTTP 回應
import { Application } from "https://deno.land/x/oak/mod.ts";
const app = new Application();
app.use((ctx) => {
ctx.response.body = "Hello from Deno and Google Cloud Run!";
});
await app.listen({ port: 8000 });
然後,我們將建立兩個檔案 -- Dockerfile
和 docker-compose.yml
-- 來建立 Docker 映像。
在我們的 Dockerfile
中,我們來新增
FROM denoland/deno
EXPOSE 8000
WORKDIR /app
ADD . /app
RUN deno cache main.ts
CMD ["run", "--allow-net", "main.ts"]
然後,在我們的 docker-compose.yml
中
version: '3'
services:
web:
build: .
container_name: deno-container
image: deno-image
ports:
- "8000:8000"
讓我們透過執行 docker compose -f docker-compose.yml build
,然後執行 docker compose up
,並前往 localhost:8000
,在本地測試這段程式碼。
成功了!
設定 Artifact Registry
Artifact Registry 是 GCP 的 Docker 映像私人登錄檔。
在我們可以使用它之前,請前往 GCP 的 Artifact Registry 並按一下「建立儲存庫」。系統會要求您輸入名稱 (deno-repository
) 和區域 (us-central1
)。然後按一下「建立」。
建立、標記和推送到 Artifact Registry
一旦我們建立了儲存庫,我們就可以開始將映像推送到其中。
首先,讓我們將儲存庫的位址新增到 gcloud
gcloud auth configure-docker us-central1-docker.pkg.dev
然後,讓我們建立您的 Docker 映像。(請注意,映像名稱定義在我們的 docker-compose.yml
檔案中。)
docker compose -f docker-compose.yml build
然後,標記它,並加上新的 Google Artifact Registry 位址、儲存庫和名稱。映像名稱應遵循下列結構:{{ location }}-docker.pkg.dev/{{ google_cloudrun_project_name }}/{{ repository }}/{{ image }}
。
docker tag deno-image us-central1-docker.pkg.dev/deno-app-368305/deno-repository/deno-cloudrun-image
如果您未指定標籤,它會預設使用 :latest
。
接下來,推送映像
docker push us-central1-docker.pkg.dev/deno-app-368305/deno-repository/deno-cloudrun-image
有關如何將映像推送到 Google Artifact Registry 和從中提取映像的更多資訊.
您的映像現在應該會顯示在您的 Google Artifact Registry 中!
建立 Google Cloud Run 服務
我們需要一個可以建立這些映像的執行個體,因此讓我們前往 Google Cloud Run 並按一下「建立服務」。
我們將其命名為「hello-from-deno」。
選擇「從現有容器映像中部署一個修訂版本」。使用下拉式選單從 deno-repository
Artifact Registry 中選取映像。
選取「允許未驗證的要求」,然後按一下「建立服務」。請確認連接埠為 8000
。
完成後,您的應用程式現在應該已上線。
太棒了!
使用 gcloud
部署
建立完成後,我們就能從 gcloud
CLI 部署到此服務。指令遵循下列結構:gcloud run deploy {{ service_name }} --image={{ image }} --region={{ region }} --allow-unauthenticated
。請注意,image
名稱遵循上述結構。
針對此範例,指令為
gcloud run deploy hello-from-deno --image=us-central1-docker.pkg.dev/deno-app-368305/deno-repository/deno-cloudrun-image --region=us-central1 --allow-unauthenticated
成功!
使用 GitHub Actions 自動化部署
為了讓自動化運作,我們首先需要確認已建立以下兩項
- Google Artifact Registry
- Google Cloud Run 服務執行個體
(如果您尚未完成,請參閱前一節。)
完成後,我們可以使用 GitHub 工作流程自動化。以下是 yaml 檔案
name: Build and Deploy to Cloud Run
on:
push:
branches:
- main
env:
PROJECT_ID: {{ PROJECT_ID }}
GAR_LOCATION: {{ GAR_LOCATION }}
REPOSITORY: {{ GAR_REPOSITORY }}
SERVICE: {{ SERVICE }}
REGION: {{ REGION }}
jobs:
deploy:
name: Deploy
permissions:
contents: 'read'
id-token: 'write'
runs-on: ubuntu-latest
steps:
- name: CHeckout
uses: actions/checkout@v3
- name: Google Auth
id: auth
uses: 'google-github-actions/auth@v0'
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
- name: Login to GAR
uses: docker/login-action@v2.1.0
with:
registry: ${{ env.GAR_LOCATION }}-docker.pkg.dev
username: _json_key
password: ${{ secrets.GCP_CREDENTIALS }}
- name: Build and Push Container
run: |-
docker build -t "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}" ./
docker push "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}"
- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v0
with:
service: ${{ env.SERVICE }}
region: ${{ env.REGION }}
image: ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}
- name: Show Output
run: echo ${{ steps.deploy.outputs.url }}
我們需要設定的環境變數為(括號中的範例是此存放庫的範例)
PROJECT_ID
:您的專案 ID(deno-app-368305
)GAR_LOCATION
:設定 Google Artifact Registry 的位置(us-central1
)GAR_REPOSITORY
:您給予 Google Artifact Registry 的名稱(deno-repository
)SERVICE
:Google Cloud Run 服務的名稱(hello-from-deno
)REGION
:Google Cloud Run 服務所在區域(us-central1
)
我們需要設定的機密變數為
GCP_CREDENTIALS
:這是 服務帳戶 的 JSON 金鑰。建立服務帳戶時,請務必 包含 Artifact Registry 和 Google Cloud Run 所需的角色和權限。