From 99a1cedd59aea0bbb38e3e795c140f40e4569497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A7=80=EC=97=B0?= Date: Wed, 14 Aug 2024 13:15:14 +0900 Subject: [PATCH] Continuing to modify workflow/main.yml --- .github/workflows/main.yml | 106 +++++++++++++++++++++++-------------- 1 file changed, 66 insertions(+), 40 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 32a0817..08c3efa 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,4 +1,4 @@ -name: Node.js CI/CD with Docker and GCP +name: Build and Deploy to Google Compute Engine on: push: @@ -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 \ No newline at end of file + - 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 }}" \ No newline at end of file