Continuing to modify workflow/main.yml #30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Node.js CI/CD with Docker and GCP | |
on: | |
push: | |
branches: [main] | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Use Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '20.x' | |
- 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: moonlit-potion-417905 | |
- name: Configure Docker for GCP | |
run: gcloud auth configure-docker asia-northeast3-docker.pkg.dev | |
- name: Build and push Docker image | |
env: | |
GCP_PROJECT_ID: moonlit-potion-417905 | |
run: | | |
docker build -t asia-northeast3-docker.pkg.dev/$GCP_PROJECT_ID/my-nodejs-app-repo/myapp:${{ github.sha }} . | |
docker push asia-northeast3-docker.pkg.dev/$GCP_PROJECT_ID/my-nodejs-app-repo/myapp:${{ github.sha }} | |
- name: Deploy to GCP VM | |
env: | |
GCP_PROJECT_ID: moonlit-potion-417905 | |
run: | | |
gcloud compute ssh --zone ${{ secrets.GCP_INSTANCE_ZONE }} ${{ secrets.GCP_INSTANCE_NAME }} --command=" | |
gcloud auth configure-docker asia-northeast3-docker.pkg.dev && | |
docker pull asia-northeast3-docker.pkg.dev/$GCP_PROJECT_ID/my-nodejs-app-repo/myapp:${{ github.sha }} && | |
docker stop myapp || true && | |
docker rm myapp || true && | |
docker run -d --name myapp -p 3000:3000 asia-northeast3-docker.pkg.dev/$GCP_PROJECT_ID/my-nodejs-app-repo/myapp:${{ github.sha }} | |
" |