|
1 | 1 | options: |
2 | 2 | logging: CLOUD_LOGGING_ONLY |
3 | 3 |
|
4 | | -substitutions: |
5 | | - _BUILD_ENV: "test" # Set this to the target environment (test, release, etc.) |
6 | | - _BRANCH: "${BRANCH_NAME}" # Branch name from the trigger |
7 | | - _FIREBASE_PROJECT_ID: "your-project-id" # Replacing with dynamic project ID |
8 | | - _FIREBASE_SITE_ID: "${_BUILD_ENV}_site_id" # Placeholder for Firebase site ID |
| 4 | +substitutions: |
| 5 | + _BUILD_ENV: "test" |
| 6 | + _BRANCH: "branch-value" |
| 7 | + _DEPLOY_CDN: "false" |
| 8 | + _CDN_BUCKET: "your-cdn-bucket" |
| 9 | + _FIREBASE_PROJECT_ID: "your-project-id" |
| 10 | + _FIREBASE_SITE_ID: "your-site-id" |
9 | 11 |
|
10 | 12 | steps: |
11 | | - # Step 1: Install Node.js dependencies |
12 | | - - name: 'gcr.io/cloud-builders/npm' |
13 | | - entrypoint: 'sh' |
14 | | - args: |
15 | | - - '-c' |
16 | | - - | |
17 | | - echo clean |
18 | | - npm cache clean --force |
19 | | - echo remove |
20 | | - rm -rf node_modules package-lock.json |
21 | | - echo Installing source NPM dependencies... |
22 | | - npm install |
23 | | -
|
24 | | - # Step 2: Conditionally run builds based on _BUILD_ENV |
25 | | - - name: 'gcr.io/cloud-builders/npm' |
26 | | - entrypoint: 'sh' |
27 | | - env: |
28 | | - - '_BRANCH=${_BRANCH}' |
29 | | - - '_BUILD_ENV=${_BUILD_ENV}' |
30 | | - args: |
31 | | - - '-c' |
32 | | - - | |
33 | | - echo "Build env:" ${_BUILD_ENV} |
34 | | - if [ "${_BUILD_ENV}" = "test" ]; then |
35 | | - npm run build-test; |
36 | | - elif [ "${_BUILD_ENV}" = "release" ]; then |
37 | | - npm run build-release; |
38 | | - elif [ "${_BUILD_ENV}" = "staging" ]; then |
39 | | - npm run build-staging; |
40 | | - elif [ "${_BUILD_ENV}" = "prod" ]; then |
41 | | - npm run build-prod; |
42 | | - elif [ "${_BUILD_ENV}" = "prod-enterprise" ]; then |
43 | | - npm run build-prod-enterprise; |
44 | | - elif [ "${_BUILD_ENV}" = "prod-in" ]; then |
45 | | - npm run build-prod-in; |
46 | | - elif [ "${_BUILD_ENV}" = "prod_agenticfirst" ]; then |
47 | | - npm run build-prod-agenticfirst; |
48 | | - elif [ "${_BUILD_ENV}" = "prod-eu" ]; then |
49 | | - npm run build-prod-eu; |
50 | | - elif [ "${_BUILD_ENV}" = "prod_cn" ]; then |
51 | | - npm run build-prod-cn; |
52 | | - elif [ "${_BUILD_ENV}" = "prod_beta" ]; then |
53 | | - npm run build-beta; |
54 | | - else |
55 | | - npm run build-test; |
56 | | - fi |
57 | | -
|
58 | | - # Step 3: Updated firebase.json dynamically |
59 | | - - name: 'alpine' |
60 | | - entrypoint: 'sh' |
61 | | - args: |
62 | | - - '-c' |
63 | | - - | |
64 | | - echo "Updating firebase.json with site ID..." |
65 | | - # Update the site ID dynamically |
66 | | - sed -i 's/"site": ".*"/"site": "'"${_FIREBASE_SITE_ID}"'"/' firebase.json |
67 | | -
|
68 | | - # Step 4: Validating the build directory contains index.html |
69 | | - - name: 'alpine' |
70 | | - entrypoint: 'sh' |
71 | | - args: |
72 | | - - '-c' |
73 | | - - | |
74 | | - echo "Checking if webplugin/build contains index.html..." |
75 | | - if [ ! -f "webplugin/build/index.html" ]; then |
76 | | - echo "Error: index.html not found in webplugin/build. Build process might have failed." |
77 | | - exit 1 |
78 | | - fi |
79 | | - echo "firebase.json exists, now validating its structure..." |
80 | | - if [ ! -f "firebase.json" ]; then |
81 | | - echo "Error: firebase.json file does not exist." |
82 | | - exit 1 |
83 | | - fi |
84 | | -
|
85 | | - # Step 5: Deploy to Firebase Hosting |
86 | | - - name: 'gcr.io/$PROJECT_ID/firebase' |
87 | | - args: |
88 | | - - 'deploy' |
89 | | - - '--only' |
90 | | - - 'hosting:${_FIREBASE_SITE_ID}' |
91 | | - - '--project' |
92 | | - - '${_FIREBASE_PROJECT_ID}' |
93 | | - - '--token' |
94 | | - - '${_FIREBASE_TOKEN}' |
| 13 | +# ------------------------------------------------------------ |
| 14 | +# Step 1: Install Node.js dependencies |
| 15 | +# ------------------------------------------------------------ |
| 16 | +- name: 'gcr.io/cloud-builders/npm' |
| 17 | + entrypoint: 'sh' |
| 18 | + args: |
| 19 | + - '-c' |
| 20 | + - | |
| 21 | + echo "Cleaning npm cache" |
| 22 | + npm cache clean --force |
| 23 | + rm -rf node_modules package-lock.json |
| 24 | + echo "Installing dependencies" |
| 25 | + npm install |
| 26 | +
|
| 27 | +# ------------------------------------------------------------ |
| 28 | +# Step 2: Build based on environment |
| 29 | +# ------------------------------------------------------------ |
| 30 | +- name: 'gcr.io/cloud-builders/npm' |
| 31 | + entrypoint: 'sh' |
| 32 | + env: |
| 33 | + - '_BRANCH=${_BRANCH}' |
| 34 | + - '_BUILD_ENV=${_BUILD_ENV}' |
| 35 | + |
| 36 | + args: |
| 37 | + - '-c' |
| 38 | + - | |
| 39 | + echo "Build env: $_BUILD_ENV" |
| 40 | + if [ "$_BUILD_ENV" = "test" ]; then |
| 41 | + npm run build-test |
| 42 | + elif [ "$_BUILD_ENV" = "release" ]; then |
| 43 | + npm run build-release |
| 44 | + elif [ "$_BUILD_ENV" = "staging" ]; then |
| 45 | + npm run build-staging |
| 46 | + elif [ "$_BUILD_ENV" = "prod" ]; then |
| 47 | + npm run build-prod |
| 48 | + elif [ "$_BUILD_ENV" = "prod-enterprise" ]; then |
| 49 | + npm run build-prod-enterprise |
| 50 | + elif [ "$_BUILD_ENV" = "prod-in" ]; then |
| 51 | + npm run build-prod-in |
| 52 | + elif [ "$_BUILD_ENV" = "prod_agenticfirst" ]; then |
| 53 | + npm run build-prod-agenticfirst |
| 54 | + elif [ "$_BUILD_ENV" = "prod-eu" ]; then |
| 55 | + npm run build-prod-eu |
| 56 | + elif [ "$_BUILD_ENV" = "prod_cn" ]; then |
| 57 | + npm run build-prod-cn |
| 58 | + elif [ "$_BUILD_ENV" = "prod_beta" ]; then |
| 59 | + npm run build-beta |
| 60 | + else |
| 61 | + npm run build-test |
| 62 | + fi |
| 63 | +
|
| 64 | +# ------------------------------------------------------------ |
| 65 | +# Step 2.1: Inject branch & env into index.html |
| 66 | +# ------------------------------------------------------------ |
| 67 | +- name: 'alpine' |
| 68 | + entrypoint: 'sh' |
| 69 | + args: |
| 70 | + - '-c' |
| 71 | + - | |
| 72 | + echo "Injecting branch and env into index.html" |
| 73 | + sed -i "s/__KM_BRANCH__/${_BRANCH}/g" webplugin/build/index.html |
| 74 | + sed -i "s/__KM_ENV__/${_BUILD_ENV}/g" webplugin/build/index.html |
| 75 | +
|
| 76 | + echo "Updated index.html:" |
| 77 | + grep "Welcome to kommunicate" webplugin/build/index.html |
| 78 | +
|
| 79 | +
|
| 80 | +# ------------------------------------------------------------ |
| 81 | +# Step 3: Update firebase.json dynamically |
| 82 | +# ------------------------------------------------------------ |
| 83 | +- name: 'alpine' |
| 84 | + entrypoint: 'sh' |
| 85 | + args: |
| 86 | + - '-c' |
| 87 | + - | |
| 88 | + echo "Updating firebase.json site ID" |
| 89 | + sed -i 's/"site": ".*"/"site": "'"$_FIREBASE_SITE_ID"'"/' firebase.json |
| 90 | +
|
| 91 | +# ------------------------------------------------------------ |
| 92 | +# Step 4: Validate build output |
| 93 | +# ------------------------------------------------------------ |
| 94 | +- name: 'alpine' |
| 95 | + entrypoint: 'sh' |
| 96 | + args: |
| 97 | + - '-c' |
| 98 | + - | |
| 99 | + if [ ! -f "webplugin/build/index.html" ]; then |
| 100 | + echo "ERROR: index.html not found in webplugin/build" |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + if [ ! -f "firebase.json" ]; then |
| 104 | + echo "ERROR: firebase.json not found" |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | + echo "Build validation successful" |
| 108 | +
|
| 109 | +# ------------------------------------------------------------ |
| 110 | +# Step 5: Firebase deploy (skipped when CDN enabled) |
| 111 | +# ------------------------------------------------------------ |
| 112 | +- name: 'gcr.io/$PROJECT_ID/firebase' |
| 113 | + entrypoint: 'sh' |
| 114 | + args: |
| 115 | + - '-c' |
| 116 | + - | |
| 117 | + if [ "$_DEPLOY_CDN" = "true" ]; then |
| 118 | + echo "Skipping Firebase deploy (_DEPLOY_CDN=true)" |
| 119 | + exit 0 |
| 120 | + fi |
| 121 | + firebase deploy \ |
| 122 | + --only hosting:$_FIREBASE_SITE_ID \ |
| 123 | + --project $_FIREBASE_PROJECT_ID \ |
| 124 | + --token $_FIREBASE_TOKEN |
| 125 | +
|
| 126 | +# ------------------------------------------------------------ |
| 127 | +# Step 6: Sync build output to CDN bucket |
| 128 | +# ------------------------------------------------------------ |
| 129 | +- name: 'gcr.io/cloud-builders/gsutil' |
| 130 | + entrypoint: 'sh' |
| 131 | + args: |
| 132 | + - '-c' |
| 133 | + - | |
| 134 | + if [ "$_DEPLOY_CDN" = "true" ]; then |
| 135 | + echo "Syncing widget build to CDN bucket root" |
| 136 | + gsutil -m rsync -r webplugin/build gs://$_CDN_BUCKET |
| 137 | + else |
| 138 | + echo "Skipping CDN sync" |
| 139 | + fi |
| 140 | +
|
| 141 | +# ------------------------------------------------------------ |
| 142 | +# Step 7: Apply CDN cache headers |
| 143 | +# ------------------------------------------------------------ |
| 144 | +- name: 'gcr.io/cloud-builders/gsutil' |
| 145 | + entrypoint: 'sh' |
| 146 | + args: |
| 147 | + - '-c' |
| 148 | + - | |
| 149 | + if [ "$_DEPLOY_CDN" = "true" ]; then |
| 150 | + echo "Applying CDN cache headers (default=300, explicit long)" |
| 151 | +
|
| 152 | + # Default cache = 300s for EVERYTHING |
| 153 | + gsutil -m setmeta -r \ |
| 154 | + -h "Cache-Control:public,max-age=300" \ |
| 155 | + gs://$_CDN_BUCKET/ || true |
| 156 | +
|
| 157 | + # Long cache ONLY for versioned resource assets |
| 158 | + gsutil -m setmeta \ |
| 159 | + -h "Cache-Control:public,max-age=31536000,immutable" \ |
| 160 | + gs://$_CDN_BUCKET/*/resources/** || true |
| 161 | +
|
| 162 | + # Long cache for shared static assets |
| 163 | + gsutil -m setmeta \ |
| 164 | + -h "Cache-Control:public,max-age=31536000,immutable" \ |
| 165 | + gs://$_CDN_BUCKET/css/** \ |
| 166 | + gs://$_CDN_BUCKET/plugin/** \ |
| 167 | + gs://$_CDN_BUCKET/resources/third-party-scripts/** || true |
| 168 | +
|
| 169 | + # Long cache for fonts |
| 170 | + gsutil -m setmeta \ |
| 171 | + -h "Cache-Control:public,max-age=31536000,immutable" \ |
| 172 | + gs://$_CDN_BUCKET/*/css/app/fonts/** || true |
| 173 | +
|
| 174 | + # |
| 175 | + gsutil setmeta -h "Cache-Control:public,max-age=300" gs://$_CDN_BUCKET/index.html |
| 176 | + gsutil setmeta -h "Cache-Control:public,max-age=300" gs://$_CDN_BUCKET/kommunicate-widget-3.0.min.js |
| 177 | + gsutil setmeta -h "Cache-Control:public,max-age=300" gs://$_CDN_BUCKET/v2/kommunicate.app |
| 178 | + gsutil setmeta -h "Cache-Control:public,max-age=300" gs://$_CDN_BUCKET/v3/kommunicate.app |
| 179 | +
|
| 180 | + echo "CDN cache headers applied successfully" |
| 181 | + else |
| 182 | + echo "Skipping CDN cache headers" |
| 183 | + fi |
| 184 | +
|
0 commit comments