XM Cloud and Netlify - GitHub Actions with Next Public Variables

Jared Arnofsky,XM CloudNetlify

Summary

Previously, I posted about Deploying with GitHub Actions for XM Cloud and Netlify (opens in a new tab). Please check it out for a full overview of deploying with GitHub Actions.

However, we recently encountered a problem in which our Sitecore variables that began with NEXT_PUBLIC_* were not being set in our Netlify CLI deploy and causing our application to break.

The Solution

The issue was that we were using the netlify deploy command. This makes it so NEXT_PUBLIC_* variables are bundled in at build time and not at run time.

Our old deploy looked like this:

Build

The fix was simple, to use the netlify build command followed by the netlify deploy command.

The only thing to note is that that netlify build command requires credentials, but you can't pass them all as arguments. To get around this you can set them as environment variables in your GitHub Action.

Our new deploy looked like this:

      - name: Netlify deploy
        env:
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
        run: |
          npx netlify build --context production
          npx netlify deploy --prod
        working-directory: apps/website

Conclusion

Knowing the correct Netlify CLI commands to properly manage and deploy your project lets you continue to leverage the Netlify UI to manage your next public variables for your XM Cloud environment.