Google cloud built not substituting environment va

2020-02-02 03:20发布

问题:

I have a cloud build trigger that attempts to push my application to firebase hosting. To do that I have an encrypted .env.enc file that contains the firebase token needed to deploy. During my build I decrypt this file and attempt to deploy but am met with an unauthorised message.

I tried hard coding the token in my deployment script instead of using the environment variable and it deploys fine.

Here is my cloudbuild.yaml

steps:
- name: gcr.io/cloud-builders/gcloud
  args:
  - kms
  - decrypt
  - --ciphertext-file=.env.enc
  - --plaintext-file=.env
  - --location=global
  - --keyring=ssr-vue-docker-app
  - --key=cloudbuild-env
# Install
- name: 'gcr.io/cloud-builders/npm'
  args: ['install']
# Test
- name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'test']
# Build
- name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'build']
# Deploy
- name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'deploy']

The final deploy step calls an npm script in my package.json with the environment variable used from the decrypted .env file.

"deploy": "firebase deploy --debug --token \"$FIREBASE_TOKEN\"

The initial output I get suggests that the token is not being used but that could also be redacted from the final log.

Step #4: [2019-04-17T21:14:48.087Z] Command: /usr/local/bin/node /workspace/node_modules/.bin/firebase deploy --debug --token= --only=hosting

This is the error I receive when attempting to deploy.

Step #4: Error: HTTP Error: 403, The caller does not have permission
Step #4: 
Step #4: [2019-04-17T21:14:48.531Z] <<< HTTP RESPONSE BODY code=403, message=The caller does not have permission, status=PERMISSION_DENIED
Step #4: [2019-04-17T21:14:48.530Z] <<< HTTP RESPONSE 403 vary=X-Origin, Referer, Origin,Accept-Encoding, content-type=application/json; charset=UTF-8, date=Wed, 17 Apr 2019 21:14:48 GMT, server=ESF, cache-control=private, x-xss-protection=1; mode=block, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, accept-ranges=none, transfer-encoding=chunked
Step #4: rewrites=[glob=**, region=us-central1, serviceId=nuxt-server], deployment-tool=cli-firebase
Step #4: [2019-04-17T21:14:48.337Z] >>> HTTP REQUEST POST https://firebasehosting.googleapis.com/v1beta1/sites/ssr-vue-docker-app/versions 
Step #4: i deploying hosting
Step #4: 
Step #4: === Deploying to 'ssr-vue-docker-app'...

Any suggestions on how I might debug if the environment variable is being used? Or is there something I am missing from my build steps that allow me to use environment variables from a .env file?

I attempted to follow this guide: https://fireship.io/lessons/ci-cd-with-google-cloud-build/. I can't seem to see what I'm missing here so any help is appreciated.

回答1:

Build firebase Docker image.

See:

  • https://github.com/GoogleCloudPlatform/cloud-builders-community
$ git clone https://github.com/GoogleCloudPlatform/cloud-builders-community
$ cd firebase
$ gcloud builds submit --config cloudbuild.yaml .

Encrypt ci token

$ firebase login:ci
$ gcloud kms keyrings create cloudbuilder --location global
$ gcloud kms keys create firebase-token --location global --keyring cloudbuilder --purpose encryption
$ echo -n <ciToken> | gcloud kms encrypt \
  --plaintext-file=- \
  --ciphertext-file=- \
  --location=global \
  --keyring=cloudbuilder \
  --key=firebase-token | base64

Set encrypted ci token in cloudbuild.yaml

See:

  • https://cloud.google.com/cloud-build/docs/securing-builds/use-encrypted-secrets-credentials?hl=ja#example_build_request_using_an_encrypted_variable
  • https://github.com/GoogleCloudPlatform/cloud-builders-community/blob/master/firebase/firebase.bash#L5
  • https://github.com/firebase/firebase-tools#user-content-using-with-ci-systems
secrets:
- kmsKeyName: projects/<projectName>/locations/global/keyRings/cloudbuilder/cryptoKeys/firebase-token
  secretEnv:
    FIREBASE_TOKEN: <EncryptedCiToken>
steps:
- id: 'npm install'
  name: 'gcr.io/cloud-builders/npm'
  args: ['install']

- id: 'functions npm install'
  name: 'gcr.io/cloud-builders/npm'
  args: ['install']
  dir: 'functions'

- id: "deploy firebase"
  name: 'gcr.io/$PROJECT_ID/firebase'
  args: ['deploy', '--project=<projectName>']

# Deploy specific Firebase services
# (If you only want to deploy specific Firebase services or features)
#
# - id: "deploy firebase"
#   name: 'gcr.io/$PROJECT_ID/firebase'
#   args: ['deploy', '--only', 'functions', '--project=<projectName>']
# 
# - id: "deploy firebase storage"
#   name: 'gcr.io/$PROJECT_ID/firebase'
#   args: ['deploy', '--only', 'storage', '--project=<projectName>']
#   secretEnv: ['FIREBASE_TOKEN']
# 
# - id: "deploy firebase firestore"
#   name: 'gcr.io/$PROJECT_ID/firebase'
#   args: ['deploy', '--only', 'firestore', '--project=<projectName>']
#   secretEnv: ['FIREBASE_TOKEN']
# 
# - id: "deploy firebase hosting"
#   name: 'gcr.io/$PROJECT_ID/firebase'
#   args: ['deploy', '--only', 'hosting', '--project=<projectName>']

More information

  • https://github.com/zkohi/firebase-sub-guides/blob/master/content/docs/ja/cd/index.md