Skip to content

Instantly share code, notes, and snippets.

@mezotv
Last active August 6, 2024 02:51
Show Gist options
  • Save mezotv/07a8551ec552e492b25a29d68f692698 to your computer and use it in GitHub Desktop.
Save mezotv/07a8551ec552e492b25a29d68f692698 to your computer and use it in GitHub Desktop.
Github workflow to automate typescript builds every commit.

Automatic Build Workflow

This GitHub Actions workflow automates the process of building your project and updating the build files in the main branch. It's triggered on pushes to the main branch that affect files in the src/ directory, or manually via workflow dispatch.

Workflow Overview

  1. The workflow is triggered on:

    • Push events to the main branch (only when files in src/ are changed)
    • Manual trigger (workflow_dispatch)
  2. It sets up a Node.js environment and installs dependencies.

  3. The build process is run using npm run build.

  4. If there are changes in the build files, it:

    • Creates a new branch
    • Commits the changes
    • Opens a pull request
    • Automatically merges the pull request

How to Use

  1. Add this workflow file to your repository at .github/workflows/automatic-build.yml.

  2. Ensure your project has a build script in package.json:

    {
      "scripts": {
        "build": "your-build-command-here"
      }
    }
    
  3. Push changes to the src/ directory in the main branch to trigger the workflow.

  4. Alternatively, manually trigger the workflow from the Actions tab in your GitHub repository.

Requirements

Both of these settings need to be setup in the way that allows github actions to automatically merge pull requests, below is my setup:

Screenshot_20240730_201120_Chrome

Screenshot_20240730_201059_Chrome

name: Automatic Build
on:
workflow_dispatch:
push:
branches: [ main ]
paths:
- 'src/**'
concurrency:
group: "main-branch"
jobs:
format:
runs-on: ubuntu-latest
name: Create Build Files
steps:
- uses: actions/checkout@v4
with:
version: latest
- uses: actions/setup-node@v3
with:
node-version: "18.x"
- run: npm ci
- name: Generate Build Files
run: npm run build
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: stefanzweifel/git-auto-commit-action@v4
id: build-files
with:
commit_message: "chore(build): update build files"
skip_checkout: true
branch: "build-${{ github.sha }}"
create_branch: true
# create PR using GitHub CLI if files were formatted
- name: create PR with build files
if: steps.build-files.outputs.changes_detected == 'true'
id: create-pr
run: git checkout build-${{ github.sha }} && gh pr create --base main --head build-${{ github.sha }} --title 'Merge build update into main' --body 'Created by Github action'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# merge PR using GitHub CLI
- name: merge PR with new build files
if: steps.build-files.outputs.changes_detected == 'true'
id: merge-pr
run: gh pr merge --admin --merge --subject 'Merge updated build files' --delete-branch
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment