Application used to generate invoices and quotes from a Google spreadsheet.
The application requires a service account to be able to access Google Drive.
Once created you need to share the invoices spreadsheet and the folder for the generated invoices with the service account user.
See https://developers.google.com/identity/protocols/oauth2/service-account for more information on service account.
To create a service account:
- go to Google Cloud Platform Console
- create a new project, called for example "facturation-backend"
- go to "APIs & Services" to enable Google Drive API and Google Sheets API
- go to "IAM & Admin > Service Accounts" to create a service account, called for example "facturation-backend"
- generate a new key and download the JSON file containing the private key
Note:
The Google Drive spreadsheet has to be associated to a GCP project (usually the one created previously).
Go to the settings of the Apps Script to set one in case it is missing.
The application is currently deployed on Google Cloud Run.
Install the gcloud CLI and
initialize it by running gcloud init
.
You should be able to select the project created before.
Then you have to enable the Cloud Run API and Cloud Build API (requires an account with billing enabled).
See this article for more information.
As the application needs access to the Google Drive spreadsheet a secret with the content of the service account JSON file has to be created:
cat <SERVICE>.json | base64 --wrap 0 | gcloud secrets create google-account --data-file=- --locations=europe-west1 --replication-policy=user-managed
Note:
As the application will only be deployed in one region automatic replication is disabled with the
--replication-policy=user-managed
option.
Finally, add read-only access to secret manager to the service account created for the application.
Docker is used to build the application:
gcloud builds submit --tag gcr.io/facturation-backend/invoice
If deploying for the first time use the following command to create the service:
gcloud run deploy invoice --image=gcr.io/facturation-backend/invoice --update-secrets=GOOGLE_ACCOUNT=google-account:latest --region=europe-west1 --allow-unauthenticated
When deploying a new build the following command is sufficient:
gcloud run deploy invoice --image=gcr.io/facturation-backend/invoice --region=europe-west1
The application has one HTTP endpoint to generate invoices:
curl https://<SERVICE_URL>/generateInvoices/<SHEET_ID>/<FOLDER_ID>
where SHEET_ID
is the ID of the spreadsheet containing the invoices to generate
and FOLDER_ID
the id of the folder where the generated invoices should be saved.
Both IDs can be found in Google Drive when opening the spreadsheet or the folder, their IDs are in the URL.
The service URL can be found using the following command:
gcloud run services list
Note:
Only new invoices will be generated.
To generate again an existing invoice remove the existing one.
To use locally the application you can just define the GOOGLE_ACCOUNT variable and run the application:
export GOOGLE_ACCOUNT=$(cat <SERVICE>.json | base64 --wrap 0)
# or for fish lovers
set --export --global GOOGLE_ACCOUNT (cat <SERVICE>.json | base64 --wrap 0)
go run cmd/invoice/main.go