Skip to content

Commit

Permalink
Continuing to modify workflow/main.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Heize-jiyean committed Aug 14, 2024
1 parent c035b5e commit 074746f
Showing 1 changed file with 46 additions and 40 deletions.
86 changes: 46 additions & 40 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Node.js CI/CD with Docker and GCP
name: Build and Deploy to Google Compute Engine

on:
push:
Expand All @@ -13,42 +13,48 @@ jobs:
node-version: [20.x]

steps:
- uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm ci

- name: Build application
run: npm run build --if-present

- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}

- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}

- name: Configure Docker for GCP
run: gcloud auth configure-docker

- name: Build and push Docker image
run: |
docker build -t gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.PROJECT_NAME }}:${{ github.sha }} .
docker push gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.PROJECT_NAME }}:${{ github.sha }}
- name: Deploy to GCP VM
run: |
gcloud compute ssh --zone ${{ secrets.GCP_INSTANCE_ZONE }} ${{ secrets.GCP_INSTANCE_NAME }} --command="
docker pull gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.PROJECT_NAME }}:${{ github.sha }} &&
docker stop ${{ secrets.PROJECT_NAME }} || true &&
docker rm ${{ secrets.PROJECT_NAME }} || true &&
docker run -d --name ${{ secrets.PROJECT_NAME }} -p 3000:3000 gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.PROJECT_NAME }}:${{ github.sha }}
" || exit 1
- name: Checkout
uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm ci

# Authenticate to Google Cloud
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}

# Setup gcloud CLI
- name: Setup Google Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
version: '416.0.0'
project_id: ${{ secrets.GCP_PROJECT_ID }}

# 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/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_INSTANCE_NAME }}-image:${{ github.sha }}" .
# Push the Docker image to Google Container Registry
- name: Publish
run: |
docker push "gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_INSTANCE_NAME }}-image:${{ github.sha }}"
# Deploy the Docker container on GCE
- name: Deploy
run: |
gcloud compute instances update-container "${{ secrets.GCP_INSTANCE_NAME }}" \
--zone "${{ secrets.GCP_INSTANCE_ZONE }}" \
--container-image "gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_INSTANCE_NAME }}-image:${{ github.sha }}"

0 comments on commit 074746f

Please sign in to comment.