Home > OS >  How to control merge button with Github Actions?
How to control merge button with Github Actions?

Time:01-02

What I want to do is simple:

  1. The merge button can only be released if the actions executed in pull_request (opening, reopening, editing) pass all jobs correctly.

  2. After managing to merge, deploy to vercel must be executed.

Two problems occur:

  1. Even running the workflows when opening the PR, it is possible to merge while it runs or even when it gives an error.

  2. When the PR is merged and closed, the deploy workflow for vercel gives a "skipped" status and doesn't even run.

WORKFLOW_CONTROLLER

name: WORKFLOW CONTROLLER

on: [workflow_dispatch, pull_request]

jobs:
  frontend:
    uses: ./.github/workflows/frontend-test.yml
  backend:
    needs: frontend
    uses: ./.github/workflows/backend-test.yml

VERCEL DEPLOY

name: VERCEL DEPLOY

on:
  pull_request:
    types:
      - closed

jobs:
  vercel-deploy:
    if: ${{ github.ref == 'refs/heads/main' && github.event.pull_request.merged == true }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18.x]
        architecture: [x64]
    steps:
      - name: Check out Git Repository
        uses: actions/checkout@v3

      - name: RUN DEPLOY [VERCEL]
        uses: amondnet/vercel-actions@v20
        with:
          vercel-args: '--prod'
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
          vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}
          github-token: ${{ secrets.GITHUB_TOKEN }}

enter image description here

CodePudding user response:

You can create a branch protection rule to enforce certain workflows for the branch, such as requiring an approving review or passing status checks for all pull requests merged into the protected branch.

To do that, navigate to your Repository Settings, click Branches, and then Add Rule. There is an option 'Require status checks to pass before merging' which allows choosing which status checks must pass before branches can be merged.

For more details see Creating a branch protection rule.

Regarding the second question, try the following:

on:
  pull_request:
    branches:
      - main
    types: [ closed ]

jobs:
  vercel-deploy:
    if: github.event.pull_request.merged == true

    ...

Reference: Trigger workflow only on pull request MERGE

  • Related