verygoodsecurity / netlify-plugin-vgs

5 stars 2 forks source link

Cannot update environment variable on netlify site during build time #9

Open palinko91 opened 1 year ago

palinko91 commented 1 year ago

`❯ Loading plugins

palinko91 commented 1 year ago

I've tried to fix it but unfortunatelly not that easy to get the account_id, some DNS setting endpoints returning it but returned me [] most likely cause I don't have other than default settings for now. I hope someone would have some idea how to find the account_id and complete the script:

import { getRouteConfig } from './utils.js';
import { NetlifyAPI } from 'netlify';

export const { VGS_VAULT_ID } = process.env;

export default {
  onPreBuild: async ({ constants }) => {
    console.log("[vgs-plugin] onPreBuild, vaultId:", VGS_VAULT_ID);

    const { NETLIFY_API_TOKEN } = constants;
    const netlify = new NetlifyAPI(NETLIFY_API_TOKEN);

    const routeConfig = getRouteConfig();

    if (routeConfig) {
      console.log('[vgs-plugin] starting route update')
      await updateRoute(VGS_VAULT_ID, routeConfig);

      console.log('[vgs-plugin] route update finished!')
    }

    const collectRoute = await applyCollectRoute(VGS_VAULT_ID);

    console.log('[vgs-plugin] netlify updateSite')
    try {
      console.log('[vgs-plugin] adding env variable VGS_ROUTE_ID', collectRoute.id)

      await netlify.setEnvVarValue(
        {
          account_id: /*todo(account_id) */,
          key: "VGS_ROUTE_ID",
          body: {
            context: "all",
            value: collectRoute.id
          }
        });
    } catch (e) {
      console.log('[vgs-plugin] netlify updateSite error:', e)
    }

    console.log('[vgs-plugin] netlify updateSite finished!')
  },
}

https://open-api.netlify.com/#tag/environmentVariables/operation/setEnvVarValue

Also it would be good if we could set query parameters for site_id, but the netlify library seems like not supporting right now.