Search code examples
google-cloud-platformgoogle-cloud-build

Cloud build export cloud sql add date in filename


Here is my pipeline :

 steps:
  - name: 'gcr.io/cloud-builders/gcloud'
    args: ['sql', 'export', 'sql', $_DB_INSTANCE_NAME, gs://$_BUCKET_NAME/$_FILENAME.sql, '--database=$_DB_DATABASE']

 options:
  dynamic_substitutions: true
  substitution_option: 'ALLOW_LOOSE'
 timeout: 3600s

I declared my variable $_FILENAME inside cloud build pipline and I set this value Cloud_Export_$(date +%Y-%m-%d)

But I got this error : Compilation failed: [_FILENAME -> Cloud_Export_$(date +%Y-%m-%d)]: generic::invalid_argument: unable to fetch payload for date +%Y-%m-%d

So I tried to remove $() to my $_FILENAME = Cloud_Export_date +%Y-%m-%d

Exporting Cloud SQL instance...
...failed.
ERROR: (gcloud.sql.export.sql) [ERROR_RDBMS] GCS URI "gs://export_bdd/Cloud_Export_date +%Y-%m-%d.sql" is empty or invalid
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/gcloud" failed: step exited with non-zero status: 1

How can I do to add the current date to my filename ?

Edit

I tried to create another variable _CURRENT_DATE with value date +%Y-%m-%d. Then I changed my $_FILENAME variable to Cloud_Export_${_CURRENT_DATE}

Now I don't have any errors but the date is empty the filename is Cloud_Export_.sql


Solution

  • I found a solution I removed the $_FILENAME variable then I'm using sh entrypoint and I added double $ to the date function $$(date +%Y-%m-%d) and it works :

    steps:
      - name: 'gcr.io/cloud-builders/gcloud'
        entrypoint: 'sh'
        args: ['-c', 'gcloud sql export sql $_DB_INSTANCE gs://$_BUCKET_NAME/filename_$$(date +%Y-%m-%d).sql --database=$_DB_NAME']