Set a deploy command based on build hook

TL;DR: Is it possible to set a deploy command in my netlify.toml based on a build hook?

I’m trying to run a different command when building the site from my build hook then from building the site from merging a PR. I’ve thought about setting a build command base on the trigger_title but ran in the problem of not being able to run any logic (if/else) commands in my netlify.toml.

The reason I want to try this is because of the new release from Nuxt where they included separate build commands for restarting webpack and generating the routes. So when my build hook comes from my CMS I only want to re-generate the routes.

If anybody has a solution or a different kind of workable approach to this that would be great :grinning:

2 Likes

I would really appreciate an option like that as well.
Actually for the same use case as nuxt released their full static mode.

Normal deploys should trigger a yarn run build && yarn run export

where as content changes triggered by a Webhook called from a cms should run yarn run export only.

1 Like

Yeah would be great. :slightly_smiling_face:

Someone told me of a way to maybe make it work:
You have to deploy trough Github Actions to Netlify and in that script, you’ll set the command: yarn build && yarn export. Now in Netlify, you should set the default deploy command to nuxt deploy. If it’s correct when changing functionality and merging to your master branch it should completely rebuild and when making content changes trough the CMS it should fire only: yarn export.

Haven’t tested this myself but sounds like something that could work, think you’ll have to turn off auto-deploys.

I do not think that will actually work, unless you have different commands in the other branch under the same script command that netlify will trigger :frowning:

would be extremely easy if you could specify the command to run in the netlify webhook…

What might work is using a shell script for your build command and making use of these hook-specific variables :smirk:!

A little if statement should then help you to distinguish between a hook build and a non-hook build, allowing you to run different commands per context.

1 Like

If my understanding is correct, the Nuxt build is not cached between deployment? So only running running “nuxt export” wouldn’t work. I haven’t tried it myself, but since this Netlify build plugin exists for Nuxt, I guess that by default Netlify doesn’t cache the webpack build.

1 Like

Well that plugins seems like a good solution then :smile:

1 Like

Ah actually I just had a closer look at it. I assumed it was a Netlify build plugin, but it’s kind of a Nuxt module.

Haha, you did take a thorough look! I’ve just seen your pull request on that repo :slight_smile:.

Caching is a funny old game. You can read up on how and what we cache but that’s not the be-all-and-end-all. Plugins like the one listed will tailor what’s cached for your SSG.

1 Like

haha :smile:
And upon closer inspection of the code, it makes use of the hook-specific variables you recommended :wink:
Thanks for the link about the way Netlify handles cache.

1 Like

hmmm how and where would you put that shell script? Do you have an example? :smiley:

You could put it in your root directory and call it from your build command: sh ./build.sh. I don’t have a specific example however, how about something like…

if [ INCOMING_HOOK_URL == "url" ]
then
#do this for this web hook
else
#do this
fi

interesting, so the build command “sh ./build.sh” is the one specified in the netlify configuration for the Branch ?

INCOMING_HOOK_URL is the netlify environment variable that is accessible there?

in the then and else block I could just run the package.json scripts with “yarn run build:full”, “yarn run build:export” ?

That might be an awesome and easy solution if I understood it correctly. :smiley:

Hey @bstrd, there’s 3 env vars which you could potentially make use of here :slight_smile:

I was thinking that sh ./build.sh becomes your build command on your site build config pane. So then, you can have:

if [ INCOMING_HOOK_URL == "url" ]
then
yarn run export
else
yarn run build && yarn run export
fi
1 Like

Okay then I understood it correctly - will try out that soonish - would be an awesome solution if that works out :smiley:
Thanks Scott! :slight_smile: @Scott

1 Like

Hey @Scott ,

I got the shell script to run fine now, although i ran into another problem … which is kinda logical - that he cant run export if he does not have any build files. :slight_smile:

10:36:31 AM: [fatal] No build files found in /opt/build/repo/.nuxt/dist/client.

10:36:31 AM: Please run nuxt build --target static before calling nuxt export

Is it somehow possible to cache the /opt/build/repo/ contents on netllify till the next time you do a full Application rebuild ?

Hey @bstrd,

Ooooh. Good question. I’m not clued up with Nuxt specifically however there’s a few modules on npm which may help?

You may be able to add a command to your nuxt export command such as cp /files /dist && nuxt export. Maybe the module above will help preserve the data. Otherwise, it’ll need some engineering!

2 Likes

Hey there!
I’ve just installed the nuxt-netlify-cache plugin and I can report that it works like a charm! Build times triggered by a hook were cut down from 65 seconds to 41 seconds!

3 Likes

For anybody interested, I wrote a blog post on dev.to about this topic:
https://dev.to/mornir/faster-deploys-with-nuxt-22hi

1 Like

awesome! thank you for putting that together and sharing that with us!

1 Like