How to Set Up a Manual Build Workflow in GitHub Actions
GitHub Actions is a powerful tool for automating your software development workflows directly in your GitHub repository. While it’s common to trigger builds automatically upon certain events like pushing code or opening a pull request, there are times when you might want to trigger a build manually. This guide will walk you through setting up a manual build workflow in GitHub Actions.
Why Set Up a Manual Build Workflow?
There are several reasons you might want a manual trigger for your build:
- Selective Builds: You might want to run a build only after ensuring that the code is ready, rather than triggering it on every push.
- Custom Deployments: Sometimes, you want to control exactly when a deployment occurs.
- Testing in a Controlled Environment: Running tests manually ensures that you can control the environment and dependencies.
Step-by-Step Guide to Setting Up a Manual Build
Step 1: Create a New Workflow File
First, navigate to your repository on GitHub. In the .github/workflows
directory, create a new YAML file (e.g., manual-build.yml
).
name: Manual Build Workflow
on:
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install Dependencies
run: npm install
- name: Run Build
run: npm run build
- name: Run Tests
run: npm test
Step 2: Define the workflow_dispatch
Trigger
The key part of setting up a manual trigger is using the workflow_dispatch
event. This allows you to run the workflow manually from the GitHub Actions tab in your repository.
on:
workflow_dispatch:
Step 3: Add Jobs and Steps
In the jobs
section, you define the steps your workflow will perform. In the example above:
- Checkout Code: This step checks out your repository’s code.
- Set up Node.js: Sets up the Node.js environment. Adjust the version according to your project’s needs.
- Install Dependencies: Runs
npm install
to install your project's dependencies. - Run Build and Tests: Runs the build and test scripts defined in your
package.json
.
Step 4: Commit and Push Your Workflow
After you’ve defined your workflow, commit and push the file to your repository. Once it’s pushed, navigate to the “Actions” tab in your GitHub repository, where you should see your new workflow listed.
Step 5: Trigger the Workflow Manually
To run the workflow manually:
- Go to the “Actions” tab in your repository.
- Select your workflow from the left sidebar.
- Click the “Run workflow” button, which appears on the right side.
You can also select the branch to run the workflow against before triggering it.
Conclusion
Setting up a manual build workflow in GitHub Actions is a straightforward process that gives you more control over when and how your code is built and deployed. By following the steps outlined in this guide, you can easily configure a manual trigger that suits your project’s needs.
Happy building!