|
| 1 | +--- |
| 2 | +title: Adding and Enforcing Pull Request Checks in Your GitHub Repository |
| 3 | +date: 2025-08-15 23:52:00 +05:30 |
| 4 | +layout: post |
| 5 | +--- |
| 6 | + |
| 7 | +Recently, I have been working on a project where developers merge code into the main branch without verifying if the changes function correctly. As a result, every two to three days, we encounter issues where nothing works. We need to address this. |
| 8 | + |
| 9 | +To solve this problem, we suggest implementing pull request checks. These checks ensure that every contribution to our repository meets required standards, passes tests, and adheres to best practices. In this blog, we’ll explore how to set up a pull request check using GitHub Actions and enforce it as a mandatory step before merging. |
| 10 | + |
| 11 | +### **Why Use Pull Request Checks?** |
| 12 | + |
| 13 | +Pull request checks help: |
| 14 | + |
| 15 | +- **Automate Quality Assurance**: Automatically run tests and linting to catch issues early. |
| 16 | +- **Enforce Standards**: Ensure all contributions meet coding and security standards. |
| 17 | +- **Streamline Collaboration**: Provide immediate feedback to contributors, reducing review cycles. |
| 18 | + |
| 19 | +### **Step 1: Setting Up a Pull Request Check Workflow** |
| 20 | + |
| 21 | +GitHub Actions makes it easy to define workflows for pull request checks. Below is an example workflow file (pull-request-check.yml) that: |
| 22 | + |
| 23 | +- Cleans the runner environment. |
| 24 | +- Checks out the latest code. |
| 25 | +- Sets up Go and installs dependencies. |
| 26 | +- Runs tests for changed files. |
| 27 | +- Optionally, runs linting. |
| 28 | + |
| 29 | +### **Example Workflow File** |
| 30 | + |
| 31 | +``` |
| 32 | +name: Pull Request Check |
| 33 | +on: |
| 34 | + pull_request: |
| 35 | + branches: |
| 36 | + - main |
| 37 | +jobs: |
| 38 | + check: |
| 39 | + steps: |
| 40 | + - name: Checkout latest code |
| 41 | + uses: actions/checkout@v4 |
| 42 | + with: |
| 43 | + fetch-depth: 0 |
| 44 | + ref: ${{ github.ref }} |
| 45 | + fetch-tags: true |
| 46 | + - name: Setup Go |
| 47 | + uses: actions/setup-go@v4.1.0 |
| 48 | + with: |
| 49 | + go-version: "1.24" |
| 50 | + cache: false |
| 51 | + - name: Install Dependencies |
| 52 | + run: | |
| 53 | + sudo apt install -y jq |
| 54 | + go install github.com/playwright-community/playwright-go/cmd/playwright@latest |
| 55 | + playwright install --with-deps chromium |
| 56 | + - name: Execute tests |
| 57 | + env: |
| 58 | + HEADLESS_BROWSER: ${{ vars.HEADLESS_BROWSER }} |
| 59 | + run: go test -v ./.. |
| 60 | +
|
| 61 | +``` |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +### **Step 2: Commit and Push the Workflow File** |
| 66 | + |
| 67 | +Save the workflow file as pull-request-check.yml in your repository. Then, commit and push it to the `main` branch: |
| 68 | + |
| 69 | +``` |
| 70 | +git add .github/workflows/pull-request-check.yml |
| 71 | +git commit -m "Add pull request check workflow" |
| 72 | +git push origin main |
| 73 | +``` |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +### **Step 3: Enforce the Pull Request Check** |
| 78 | + |
| 79 | +To make the pull request check mandatory, configure branch protection rules in your GitHub repository. |
| 80 | + |
| 81 | +### **Steps to Enforce the Check:** |
| 82 | + |
| 83 | +1. **Navigate to Repository Settings**: |
| 84 | + - Go to your repository on GitHub. |
| 85 | + - Click on the `Settings` tab. |
| 86 | +2. **Set Up Branch Protection Rules**: |
| 87 | + - Under the `Code and automation` section, click on `Branches`. |
| 88 | + - Click the `Add branch ruleset` button. |
| 89 | +3. **Define the Rule**: |
| 90 | + - In the `Branch name pattern` field, enter `main` (or the branch you want to protect). |
| 91 | + - Check the `Require status checks to pass before merging` option. |
| 92 | + - Under `Status checks that are required`, select the workflow name (e.g., `Pull Request Check`). |
| 93 | +4. **Save the Rule**: |
| 94 | + - Click the `Save changes` button. |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +### **Step 4: Verify the Setup** |
| 99 | + |
| 100 | +1. Create a new pull request targeting the `main` branch. |
| 101 | +2. The pull request check will automatically run. |
| 102 | +3. The pull request cannot be merged until the check passes. |
0 commit comments