Tạo Pull Request tự động trên GitHub sử dụng GitHub Actions CI/CD

KP
Khoi Pro

Khi làm việc theo đúng quy trình Git Workflow mình đề cập trong bài viết trước, thì việc tạo nhánh đẩy lên GitHub sẽ có thể tự động thành 1 tác vụ chạy qua GitHub Actions được.

Đầu tiên, bạn tạo 1 folder .github/workflows từ folder gốc của dự án.

Folder: .github/workflows

Tiếp đến, bạn tạo 1 file với đuôi .yml với nội dung như sau.

Ví dụ ở dưới là các nhánh bugfix/feature/ sẽ tự động được tạo Pull Request để review merge vào nhánh develop.

# .github/workflows/gitflow-pr-develop.yml
name: Create New Pull Request
on:
  push:
    branches:
      - 'bugfix/**'
      - 'feature/**'
jobs:
  create_pull_request:
    name: Create new PR
    runs-on: ubuntu-latest
    steps:
      - name: pull-request-action
        uses: vsoch/pull-request-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PULL_REQUEST_BRANCH: "develop"

Hãy thử với nhánh hotfix và merge vào master. Tạo 1 file khác cũng trong cùng thư mục.

# .github/workflows/gitflow-pr-master.yml
name: Create New Pull Request
on:
  push:
    branches:
      - 'hotfix/**'
jobs:
  create_pull_request:
    name: Create new PR
    runs-on: ubuntu-latest
    steps:
      - name: pull-request-action
        uses: vsoch/pull-request-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PULL_REQUEST_BRANCH: "master"

Lưu ý:

  • Nhánh chính develop, master cần có sẵn trên GitHub rồi.
  • Nhánh phụ muốn tự động phải đúng quy tắc là đi từ nhánh develop/master ra và sẵn sàng merge vào.