Search code examples
google-cloud-platformterraformgithub-actionsterraform-provider-gcp

How to convert double-quoted string to single-quoted string in a GitHub Action for use in 'google-github-actions/ssh-compute'?


I'm writing a GitHub Action that deploys infrastructure to the GCP. The infra consists of a control host and three target hosts. After the deployment, I want to auth the control host so that it can use gcloud compute ssh to manage the target hosts. To achieve the goal, I want to use the 'google-github-actions/ssh-compute@v0' action. According to the readme, the action demands the following:

    - id: 'compute-ssh'
      uses: 'google-github-actions/ssh-compute@v0'
      with:
        instance_name: 'example-instance'
        zone: 'us-central1-a'
        ssh_private_key: '${{ secrets.GCP_SSH_PRIVATE_KEY }}'
        command: 'echo Hello world'

Thus, I need to extract information about the control host's name and zone. The idea is to use terraform output to retrieve the required info:

          ch_name=$(terraform output control_host_name)
          echo "Control host name - $ch_name"
          echo CH_NAME=$ch_name >> $GITHUB_ENV
          
          ch_zone=$(terraform output control_host_zone)
          echo "Control host zone - $ch_zone"
          echo CH_ZONE=$ch_zone >> $GITHUB_ENV

Having fetched the attributes, I want to pass them to the 'google-github-actions/ssh-compute@v0':

        with:
          instance_name: '${{ env.CH_NAME }}'
          zone: '${{ env.CH_ZONE }}'
          ssh_private_key: '${{ secrets.KEY }}'
          command: 'cd ~/.ssh && ls -la'

When I run the workflow, I get the following error:

ERROR: (gcloud.compute.ssh) Could not fetch resource: - 
Invalid value for field 'zone': '"europe-west3-a"'. 
Must be a match of regex '[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?'

So the problem I'm facing here is: terraform output delivers double-quoted strings, whereas the action demands single-quoted strings. Is there a way to convert the retrieved string to single-quoted strings for use in the ssh-compute action?


Solution

  • You have at least two option:

    Ouput the variable without quotes:

    terraform output -raw control_host_zone
    

    Remove the quotes in the HCL:

    zone: trim(${{ env.CH_ZONE }}, "\"")