google-github-actions / setup-gcloud

A GitHub Action for installing and configuring the gcloud CLI.
https://cloud.google.com/sdk/docs
Apache License 2.0
1.72k stars 509 forks source link

Can't deploy container to GCE, using gcloud compute instances update-container, because of YML parse error #559

Closed HaiderSultanArc closed 2 years ago

HaiderSultanArc commented 2 years ago

TL;DR

I am writing a Github Workflow that will build and push the docker container to GCR and then to the VM instance on GCE. But the deployment stage of GCE gives out the following error:

Run gcloud compute instances update-container "$GCE_INSTANCE" \
  gcloud compute instances update-container "$GCE_INSTANCE" \
    --zone "$GCE_INSTANCE_ZONE" \
    --container-image "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA"

  shell: /usr/bin/bash -e ***0***
  env:
   ...

ERROR: (gcloud.compute.instances.update-container) Failed to parse YAML: mapping values are not allowed here
  in "<unicode string>", line 5, column 18:
                image: 'gcr.io/cloud-marketplace/goog ... 
                     ^ (line: 5)
Error: Process completed with exit code 1.

Expected behavior

gloud compute instances update-container should update the container on VM.

Observed behavior

Instead returns an error related to parsing of YML.

ERROR: (gcloud.compute.instances.update-container) Failed to parse YAML: mapping values are not allowed here
  in "<unicode string>", line 5, column 18:
                image: 'gcr.io/cloud-marketplace/goog ... 
                     ^ (line: 5)
Error: Process completed with exit code 1.

Action YAML

name: Build and Deploy to Google Compute Engine (one more time, yes)

on:
  push:
    branches:
    - 'main'

env:
  PROJECT_ID: ***
  GCE_INSTANCE: ***
  GCE_INSTANCE_ZONE: ***

jobs:
  setup-build-publish-deploy:
    name: Setup, Build, Publish, and Deploy
    runs-on: ubuntu-latest

    # Add "id-token" with the intended permissions.
    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
    # Checkout Action
    - name: Checkout
      uses: actions/checkout@v3

    # Authentication via credentials json
    - id: 'auth'
      uses: 'google-github-actions/auth@v0'
      with:
        credentials_json: '${{ secrets.GCP_SA_KEY }}'

    # Setup gcloud CLI
    - name: Set up Cloud SDK
      uses: google-github-actions/setup-gcloud@v0

    # 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" ./path/to/Dockerfile

    # Push the Docker image to Google Container Registry
    - name: Publish
      run: |-
        docker push "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA"

    # Add pruning and IP address update to VM startup script
    - name: Update startup script to prune and update IP address
      run: |-
        gcloud compute instances add-metadata $GCE_INSTANCE \
          --zone "$GCE_INSTANCE_ZONE" \
          --metadata=startup-script="#! /bin/bash
            docker image prune -af"

    # Deploy the Docker image to GCE
    - 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"

    # Purge old images from GCR (not latest)
    - name: Purge GCR images
      run: |-
        gcloud container images list-tags gcr.io/$PROJECT_ID/$GCE_INSTANCE-image \
          --format="get(digest)" --filter="NOT tags=$GITHUB_SHA" | \
          awk -v image_path="gcr.io/$PROJECT_ID/$GCE_INSTANCE-image@" '{print image_path $1}' | \
          xargs -r gcloud container images delete --force-delete-tags --quiet

Log output

https://github.com/OpenAIMP/qunsultant/runs/6445584217?check_suite_focus=true

Additional information

Earlier this same YML file worked fine and Container was updated on VM

sethvargo commented 2 years ago

Hi @HaiderSultanArc

Are you sure that's the action.yml file? The error message does not seem to correspond to given action.yml. It's referencing gcr.io/cloud-marketplace/..., which I do not see in your action.yml anywhere. Is this issue still happening?

HaiderSultanArc commented 2 years ago

@sethvargo thanks for the reply and, yes this is the yml file. I don't understand the nature of this error. I know it is in the gloud compute instances update-container command.

I don't know the internal working of the command. It takes these attributes and the format is perfectly fine. But it gives an error related to mapping values or parsing of yml or something.

sethvargo commented 2 years ago

@HaiderSultanArc what happens when you run the command locally?

HaiderSultanArc commented 2 years ago

@sethvargo it gives the exact same error locally on my terminal as well

sethvargo commented 2 years ago

Hi @HaiderSultanArc that points to an issue with your configuration or potentially a gcloud bug. If you have a Google support contract, you can open a case. If you do not have a support contract, see https://cloud.google.com/support/docs/issue-trackers for filing a bug.

Since this doesn't seem related to the GitHub Action, I'll close this out.

HaiderSultanArc commented 2 years ago

@sethvargo thanks for the coordination.