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 99a1ced
Showing 1 changed file with 66 additions and 40 deletions.
106 changes: 66 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,68 @@ 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 Google Cloud SDK
- 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
- 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 }}"
# Create JSON file for metadata
- name: Create metadata file
run: |
echo '{
"spec": {
"containers": [
{
"name": "my-container",
"image": "gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_INSTANCE_NAME }}-image:${{ github.sha }}"
}
]
}
}' > gce-container-declaration.json
# Add or update container metadata
- name: Add or update container metadata
run: |
gcloud compute instances add-metadata "${{ secrets.GCP_INSTANCE_NAME }}" \
--zone "${{ secrets.GCP_INSTANCE_ZONE }}" \
--metadata-from-file gce-container-declaration=gce-container-declaration.json
# Update the container
- name: Update container
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 99a1ced

Please sign in to comment.