Skip to content

Commit

Permalink
Netlify: ignore PRs without changes in the docs directory (#5328)
Browse files Browse the repository at this point in the history
* Ignore PRs w/o files in the docs directory

* Fix return and comment

* debug

* try adding origin

* add logging

* handle remote already existing

* debug again

* use git remote -v to check

* final logging

* final final logging
  • Loading branch information
jtoar authored Apr 25, 2022
1 parent f41d7f4 commit 4a129d3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
47 changes: 47 additions & 0 deletions docs/ignore_build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// We only want Netlify to build the site if a PR changes files in this `docs` directory.
// See https://docs.netlify.com/configure-builds/ignore-builds.
// Note: Netilfy runs this via Node.js 12.

const { execSync } = require('child_process')

console.log('------------------------')
console.log("Running 'docs/ignore_build.js'")

const remoteExists = execSync('git remote -v').toString().includes('origin')

if (remoteExists) {
console.log('Remote exists')
} else {
console.log('Adding remote')
execSync('git remote add origin https://github.com/redwoodjs/redwood.git')
}

console.log('Fetching main')
execSync('git fetch origin main')

console.log('Diffing changed files against main (name only)')
const changedFiles = execSync('git diff origin/main --name-only')
.toString()
.trim()
.split('\n')
.filter(Boolean)
console.log({
changedFiles,
})

const shouldBuild = changedFiles.some((changedFile) =>
changedFile.startsWith('docs')
)
console.log({
shouldBuild,
})

// We've done all the logic based on whether we should build the site,
// but since this is an ignore script, we have to flip the logic here.
if (shouldBuild) {
console.log(`PR '${process.env.HEAD}' has doc changes. Proceeding`)
process.exitCode = 1
} else {
console.log(`PR '${process.env.HEAD}' doesn't have doc changes. Ignoring`)
}
console.log('------------------------')
3 changes: 2 additions & 1 deletion docs/netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
base = "docs"
publish = "build"
command = "yarn build"

ignore = "node ignore_build.js"

[[redirects]]
from = "/docs"
to = "/docs/introduction"
Expand Down

0 comments on commit 4a129d3

Please sign in to comment.