Make the Netlify build wait for another GH Action to run

I work on a CLI tool. My documentation is derived from the CLI (as in, the CLI framework used by my tool exposes introspection commands which I use to generate the documentation pages).

My problem is: how can I wait for the CLI to have finished building before starting the Netlify build? I could re-run the CLI build within the Netlify job, but it doesn’t feel ideal - my tool is written in Rust, and I’m worried it would duplicate the setup I need to do to (especially around caching).

I was thinking the ideal would be to wait for the build GH Action to have completed, then download the CLI artifact from the Netlify job and build the site from there. Is it something Netlify would support? Are there other solutions you’d recommend?

Hi arcanis

Netlify’s builds are typically triggered by pushes or pull requests to your connected Git repository. The build runs in an isolated environment, pulling the latest code from the repo and executing your build commands (as defined in your netlify.toml or build settings). While Netlify doesn’t natively support waiting for external jobs (like a GitHub Actions workflow) or directly downloading artifacts from GitHub, we can work around this using webhooks, APIs, or repository-based workflows.

Recommended Solutions:

Here are a few approaches to achieve what you’re aiming for, prioritizing efficiency and minimal duplication:

  1. Trigger Netlify Builds via API After GitHub Actions Completion:

-In your GitHub Actions workflow, build the CLI tool and upload it as an artifact.

  • At the end of the workflow (after the CLI build succeeds), use Netlify’s Build API to trigger a new build for your site. This way, Netlify will pull the latest repo state, but you can ensure the CLI artifact is available (e.g., by committing it back to the repo in a separate step).

*How to Implement:
-In GitHub Actions, after building the CLI, use an action like actions/checkout and git commit to push the built CLI binary (or generated docs) to a branch or the main repo.
-Then, use a tool like curl or a GitHub Action (e.g., netlify/actions/cli) to call Netlify’s Build Hook or API endpoint to start the build.
- Example API call: curl -X POST -d '{}' https://api.netlify.com/build_hooks/YOUR_BUILD_HOOK_ID
-This ensures Netlify only builds after the CLI is ready, without re-running the Rust build.
*Pros: No duplication of build logic; leverages GitHub for the heavy lifting.
*Cons: Requires committing artifacts to the repo, which might bloat your git history (consider using a separate branch or cleaning up).

  1. Commit CLI Artifacts to the Repo in GitHub Actions:
  • Modify your GitHub Actions workflow to build the CLI, generate the documentation, and commit the results directly to your repository (e.g., in a docs/ folder or as a binary in the repo).
  • Configure Netlify to build from this updated state. Since Netlify builds from the repo, it will automatically pick up the committed CLI output without needing to rebuild it.
    *How to Implement:
    • Use GitHub Actions to run the CLI introspection, generate docs, and commit them (e.g., via git add and git push).
    • Set up a Netlify Deploy Hook triggered by the GitHub Actions push.
  • Pros: Simple and integrates well with Netlify’s repo-based builds.
  • Cons: Artifacts are stored in git, so manage file sizes carefully.
  1. Optimize Building the CLI Within Netlify (If Duplication is Acceptable):
  • If the above options are too complex, you could run the CLI build directly in Netlify’s build process. To minimize setup duplication, use Netlify’s build caching for Rust dependencies (e.g., via cargo cache directories).
  • In your netlify.toml, define build commands that install Rust, cache dependencies, and run the CLI build before generating docs.
    *Example netlify.toml:

[build]
command = “curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh -s – -y && source ~/.cargo/env && cargo build --release && ./target/release/your-cli generate-docs”
publish = “docs/”

*Pros: Everything happens in one place; caching can be configured.

  • Cons: Duplicates setup, as you mentioned, but Netlify’s environment is optimized for this.
  1. Using External Artifact Storage:
  • Build the CLI in GitHub Actions, upload the artifact to a cloud storage service (e.g., AWS S3, GitHub Releases), and have Netlify download it during the build via a pre-build script.
  • This is less ideal for security and complexity but could work if you need to avoid repo commits.

Additional Tips

*Build Hooks and Webhooks: Netlify supports incoming webhooks to trigger builds. You can set this up in your site settings and call it from GitHub Actions.
*Caching in Netlify: If you go with option 3, explore Netlify’s build caching for Rust: Add cache directories in netlify.toml like [build.cache] directories = ["~/.cargo", "target"].
*Testing: Start with a test branch to validate the workflow without affecting production.

Hope this helps, Thank you.