Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
palinko91 opened this issue May 26, 2023 · 1 comment
Open

Comments

@palinko91
Copy link

`❯ Loading plugins

  • @vgs/netlify-plugin-vgs@0.0.6 from Netlify app and package.json

    @vgs/netlify-plugin-vgs (onPreBuild event)
    ────────────────────────────────────────────────────────────────

    [vgs-plugin] onPreBuild, vaultId: xxx
    [vgs-plugin] collectRouteExists: true
    [vgs-plugin] netlify updateSite
    [vgs-plugin] adding env variable VGS_ROUTE_ID xxx
    [vgs-plugin] netlify updateSite error: TextHTTPError: Bad Request
    at parseJsonResponse (file:///C:/Users/arpad/OneDrive/Asztali%20g%C3%A9p/react/predictor_web_ui/node_modules/netlify/src/methods/response.js:37:35)
    at parseResponse (file:///C:/Users/arpad/OneDrive/Asztali%20g%C3%A9p/react/predictor_web_ui/node_modules/netlify/src/methods/response.js:9:26)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async callMethod (file:///C:/Users/arpad/OneDrive/Asztali%20g%C3%A9p/react/predictor_web_ui/node_modules/netlify/src/methods/index.js:31:26)
    at async onPreBuild (file:///C:/Users/arpad/OneDrive/Asztali%20g%C3%A9p/react/predictor_web_ui/node_modules/@vgs/netlify-plugin-vgs/index.js:30:7)
    at async Object.run (file:///C:/.npm-global/node_modules/netlify-cli/node_modules/@netlify/build/lib/plugins/child/run.js:14:5)
    at async handleEvent (file:///C:/.npm-global/node_modules/netlify-cli/node_modules/@netlify/build/lib/plugins/child/main.js:28:42)
    at async process. (file:///C:/.npm-global/node_modules/netlify-cli/node_modules/@netlify/build/lib/plugins/ipc.js:91:24) {
    status: 400,
    data: 'Site using new environment variables experience. Cannot update environment variables via Sites API. See https://open-api.netlify.com/#tag/environmentVariables'
    }
    [vgs-plugin] netlify updateSite finished!

    (@vgs/netlify-plugin-vgs onPreBuild completed in 3.7s)`
@palinko91
Copy link
Author

palinko91 commented May 26, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant