CI/CD with GitHub

Duy Văn | 25/03/2024


Trong bài viết này thì mình sẽ đi qua các nội dung về CI/CD dành cho Automation Test với GitHub Action, kết hợp với Playwright Python

Với GitHub, nó là một remote repository nổi tiếng và sử dụng rộng rãi, chủ yếu cho các dự án open source, tuy nhiên thì enterprise dùng cũng được chả sao cả. Cái chúng ta nói đến ở đây là GitHub Action, nó là một công cụ CI/CD được tích hợp sẵn trong GitHub, bao gồm các chức năng Build, Test, Deploy,…

GitHub Action

Với GitHub Action, thì cơ chế để kích hoạt workflows là push hoặc pull request. Các bước của workflow được định nghĩa bằng file YAML .yml và được lưu trong thư mục .github/workflows của repo

Thường thì cấu trúc của một bộ test script hoạt động theo chiều hướng push/pull request -> enviroment preparation -> test -> report sẽ như sau:

my-project/
├── .github/
│   └── workflows/
│       └── ci.yml         # GitHub Actions workflow file
├── tests/   
│   └── test_script.py     # Playwright test files
├── requirements.txt       # Python dependencies 
└── (other project files)

Toàn bộ workflow sẽ được ghi lại trong file ci.yml

Workflow file

Để bắt đầu thì chúng ta sẽ định hình trigger cho workflow, thông thường thì tests sẽ chạy mỗi lần push và pull request lên main branch

Trigger sẽ được quy định bằng từ khoá on và các event như push, pull_request, schedule,…


name: CI for Playwright Tests
on:
    push:
        branches: [ main, develop ]
    pull_request:
        branches: [ main ]
jobs:
    test:
        runs-on: ubuntu-latest

Với khởi đầu như trên, chúng ta sẽ có:

  • Workflow có tên CI for Playwright Tests
  • Workflow sẽ hoạt động trên hai branch, main và develop. main là branch chính, còn develop là branch để phát triển scripts và nghịch ngợm
  • Workflow sẽ trigger với push event ở main và develop branch, và pull request event ở main branch
  • Có một job có tên test và sẽ chạy trên ubuntu-latest - đây là một Linux Distribution, hay gọi là một Linux runner trong context của CI/CD

Environment preparation

Khâu chuẩn bị Environment này thì cơ bản chúng ta sẽ cài đặt các dependencies cần thiết cho test scripts, kể cả các dependencies nằm ngoài Python (ví dụ như Allure, …)

Cấu trúc của nó sẽ đi theo các steps, các steps sẽ có name, uses câu lệnh nào


jobs:
    test:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4  
            - name: Set up Python
                uses: actions/setup-python@v4
                with:
                    python-version: '3.11'

Bước đầu tiên chính là pull repo về runner và cài đặt Python, ở đây mình dùng Python 3.11. Phần name kia sẽ phục vụ cho việc hiển thị tên của step trong log

Sau đấy chúng ta sẽ cài dependencies cho Python, thông thường là pip install -r requirements.txt, nhưng có thể trước tiên nên cập nhật pip lên phiên bản mới nhất


jobs:
    test:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4  
            - name: Set up Python
                uses: actions/setup-python@v4
                with:
                    python-version: '3.11'
            - name: Update pip
                run: python -m pip install --upgrade pip
            - name: Install dependencies
                run: pip install -r requirements.txt

Sau đấy, nếu cần thiết phải cài đặt các dependencies ở ngoài phạm vi Python


jobs:
    test:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4  
            - name: Set up Python
                uses: actions/setup-python@v4
                with:
                    python-version: '3.11'
            - name: Update pip
                run: python -m pip install --upgrade pip
            - name: Install dependencies
                run: pip install -r requirements.txt
            - name: Install Allure
                run: sudo apt-add-repository ppa:qameta/allure
                run: sudo apt-get update
                run: sudo apt-get install allure
            - name: Install Playwright browsers
                run: python -m playwright install --with-deps

--with-deps ở đây là để cài đặt các dependencies cho Playwright bao gồm cả library và browser

Chạy test scripts

Bây giờ, với file test có tên test_script.py thì chúng ta sẽ chạy nó bằng cách sử dụng pytest


jobs:
    test:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4  
            - name: Set up Python
                uses: actions/setup-python@v4
                with:
                    python-version: '3.11'
            - name: Update pip
                run: python -m pip install --upgrade pip
            - name: Install dependencies
                run: pip install -r requirements.txt
            - name: Install Allure
                run: sudo apt-add-repository ppa:qameta/allure
                run: sudo apt-get update
                run: sudo apt-get install allure
            - name: Install Playwright browsers
                run: python -m playwright install --with-deps
            - name: Run tests
                run: pytest tests/test_script.py --maxfail=1 --disable-warnings -q

Nên nhớ là trong bài test chúng ta phải chạy headless, vì GitHub Actions không có GUI, headless cũng sẽ được đặt làm default nên cơ bản không cần pass flag vào.

Report

Sau đó là bước xử lý kết quả và báo cáo, ở đây mình sẽ sử dụng Allure để tạo report, nên chúng ta cần sửa lại lệnh pytest để tạo report


jobs:
    test:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4  
            - name: Set up Python
                uses: actions/setup-python@v4
                with:
                    python-version: '3.11'
            - name: Update pip
                run: python -m pip install --upgrade pip
            - name: Install dependencies
                run: pip install -r requirements.txt
            - name: Install Allure
                run: sudo apt-add-repository ppa:qameta/allure
                run: sudo apt-get update
                run: sudo apt-get install allure
            - name: Install Playwright browsers
                run: python -m playwright install --with-deps
            - name: Run tests
                run: pytest tests/test_script.py --maxfail=1 --disable-warnings -q --alluredir=./allure-results
            - name: Generate Allure report
                run: allure generate --clean
            - name: Upload report
                uses: actions/upload-artifact@v2
                with:
                    name: allure-report

Một practice nên làm đó là chúng ta nên sử dụng if: ${ { always() } } để đảm bảo rằng report sẽ được tạo ra dù test pass hay fail


jobs:
    test:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4  
            - name: Set up Python
                uses: actions/setup-python@v4
                with:
                    python-version: '3.11'
            - name: Update pip
                run: python -m pip install --upgrade pip
            - name: Install dependencies
                run: pip install -r requirements.txt
            - name: Install Allure
                run: sudo apt-add-repository ppa:qameta/allure
                run: sudo apt-get update
                run: sudo apt-get install allure
            - name: Install Playwright browsers
                run: python -m playwright install --with-deps
            - name: Run tests
                run: pytest tests/test_script.py --maxfail=1 --disable-warnings -q --alluredir=./allure-results
            - name: Generate Allure report
                run: allure generate --clean
            - name: Upload report
                if: ${{ always() }}
                uses: actions/upload-artifact@v2
                with:
                    name: allure-report

Ngoài ra thì nếu cần thực hiện đẩy cả folder test result lên thì có thể đặt path: ./allure-results trong with của upload-artifact

Tích hợp UI của GitHub

Một lựa chọn đấy là GitHub có thể đẩy kết quả test lên UI để biết cái gì fail mà không cần tải về artifacts. Trên Marketplace của GitHub có một số app hỗ trợ việc này như Allure hay ReportPortal

Kết luận

Đây là một pipeline cơ bản cho việc chạy test scripts với Playwright Python, và nó được thiết kế để hoạt động với codebase của một bộ test script chứ không phải ứng dụng web hoặc mobile. Các bước cơ bản như cài dependencies, chạy test scripts và tạo report đã được đề cập, và ta có thể tùy chỉnh nó theo nhu cầu của mình