【发布时间】:2021-06-05 12:36:24
【问题描述】:
我目前正在尝试创建一个 Github Action,它会自动更新我的一个 GCE 实例上正在运行的容器。我正在使用the template from this repo:
# Copyright 2020 Google, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Build and Deploy to Google Compute Engine
on:
push:
branches:
- master
env:
PROJECT_ID: ${{ secrets.GCE_PROJECT }}
GCE_INSTANCE: instance-name
GCE_INSTANCE_ZONE: us-east1-d
jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
# Setup gcloud CLI
- uses: google-github-actions/setup-gcloud@master
with:
version: '290.0.1'
service_account_key: ${{ secrets.GCE_SA_KEY }}
project_id: ${{ secrets.GCE_PROJECT }}
# Configure Docker to use the gcloud command-line tool as a credential
# helper for authentication
- run: |-
gcloud --quiet auth configure-docker
# Build the Docker image
- name: Build
run: |-
docker build --tag "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA" .
# Push the Docker image to Google Container Registry
- name: Publish
run: |-
docker push "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA"
- name: Deploy
run: |-
gcloud compute instances update-container "$GCE_INSTANCE" \
--zone "$GCE_INSTANCE_ZONE" \
--container-image "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA"
到目前为止,我更新应用程序的方法是从我的 github 存储库中手动提取更改并运行 docker-compose up --build。我正在使用 docker-compose,因为我的应用程序也在使用 Postgres DB。
这是我的Dockerfile:
FROM node:14
WORKDIR /usr/src/app
COPY . .
RUN npm install
CMD [ "npm", "run", "prod" ]
还有我的docker-compose.yaml(带有虚拟值):
version: "3"
services:
app:
container_name: cname
restart: always
build: .
depends_on:
- postgres
postgres:
image: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pw
POSTGRES_DB: db
ports:
- '5432:5432'
volumes:
- db-volume:/var/lib/postgresql/data
volumes:
db-volume:
应用程序本身运行正常。在 master 上提交后,会创建一个 docker 映像并将其存储在 GCP 容器注册表中,但是如何将这个容器与 Postgres 容器一起运行?
【问题讨论】:
-
how do I run this container together with the Postgres Container是什么意思?如果我理解正确,您似乎没有为他们定义网络。 -
@Saeed 我正在使用上面的 docker-compose 文件来启动我的应用程序所需的两个容器。我不明白如何使用 Github Actions 和
gcloud来实现相同的目标。 Github 操作模板尚未使用 docker-compose 文件,这意味着 postgres 的容器永远不会启动/更新。
标签: docker google-cloud-platform docker-compose google-compute-engine github-actions