2

I have this yaml file for a Cloud Run config with the following placeholders:

  1. REGION

  2. SERVICE_ACCOUNT

  3. IMAGE

  4. PROJECT_ID

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: tickets
  labels:
    cloud.googleapis.com/location: REGION
spec:
  template:
    spec:
      serviceAccountName: SERVICE_ACCOUNT
      containers:
        - image: IMAGE
          args:
            - -firebase-project-id=PROJECT_ID
            - -env=development

I also have a job defined for GitHub Actions:

# This workflow will deploy the built container image from Artifact Registry to Cloud Run.

name: Deploy to Cloud Run

on:
  workflow_dispatch:
    inputs:
      version:
        description: "The version to deploy"
        required: true

jobs:
  deploy:
    permissions:
      contents: "read"
      id-token: "write"

    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v3

      - name: Google Auth
        uses: "google-github-actions/auth@v1"
        with:
          token_format: "access_token"
          workload_identity_provider: "${{ secrets.WIF_PROVIDER }}" # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider
          service_account: "${{ secrets.WIF_SERVICE_ACCOUNT }}" # e.g. - my-service-account@my-project.iam.gserviceaccount.com

      - name: Replace values in the YAML file
        env:
          REGION: ${{ secrets.SERVICE_REGION }}
          SERVICE_ACCOUNT: ${{ secrets.CLOUD_RUN_SERVICE_ACCOUNT }}
          IMAGE: ${{ secrets.GAR_LOCATION }}-docker.pkg.dev/${{ secrets.PROJECT_ID }}/ticketing-dev/tickets:${{ github.sha }}
          PROJECT_ID: ${{ secrets.PROJECT_ID }}
        run: |
          sed -i.bak "s/REGION/$REGION/g" cloud-run.yml
          sed -i.bak "s/SERVICE_ACCOUNT/$SERVICE_ACCOUNT/g" cloud-run.yml
          sed -i.bak "s/IMAGE/$IMAGE/g" cloud-run.yml
          sed -i.bak "s/PROJECT_ID/$PROJECT_ID/g" cloud-run.yml

      - name: Deploy to Cloud Run
        uses: google-github-actions/deploy-cloudrun@v1
        with:
          metadata: ./cloud-run.yml

      - name: Show Cloud Run URL
        run: echo ${{ steps.deploy.outputs.url }}

At the Replace values in YAML file step I try to use the sed command to replace those placeholders from the Cloud run config file.

WHEN I RUN THE JOB I GET THIS ERROR:

Run sed -i.bak "s/REGION/$REGION/g" cloud-run.yml
sed: -e expression #1, char 37: unknown option to `s'
Error: Process completed with exit code 1.

And this error is not just for the REGION replace. He is the first that the sed command tries to replace...

I've also tried to use for eg. the actual value of the $REGION var in the sed command and not use the env variable and still, it didn't work...

It works only if I do something like this:

sed -i 's/REGION/<value>/g` cloud-run.yaml

but for my case I need to use double quotes "" to replace with the value of the variable $REGION...

Robert Mihai
  • 184
  • 1
  • 9

1 Answers1

0
  1. Escape '/' in the IMAGE variable
  2. Run one sed command with multiple expressions
  - name: Replace values in YAML file
        env:
          REGION: ${{ secrets.SERVICE_REGION }}
          SERVICE_ACCOUNT: ${{ secrets.CLOUD_RUN_SERVICE_ACCOUNT }}
          IMAGE: ${{ secrets.GAR_LOCATION }}-docker.pkg.dev\/${{ secrets.PROJECT_ID }}\/ticketing-dev\/tickets:${{ github.sha }}
          PROJECT_ID: ${{ secrets.PROJECT_ID }}
        run: sed -e "s/REGION/$REGION/g" -e "s/SERVICE_ACCOUNT/$SERVICE_ACCOUNT/g" -e "s/IMAGE/$IMAGE/g" -e "s/PROJECT_ID/$PROJECT_ID/g" cloud-run.yml
Robert Mihai
  • 184
  • 1
  • 9