Bài viết này sẽ giới thiệu cách triển khai ứng dụng CI/CD cho Github Action chạy self-hosted runner trên K8s. Một số thứ cần có:
Đây là 1 open source trên Github cho phép sử dụng Self-hosted runner trên K8s. Chi tiết có thể xem thêm Tại đây, mình sẽ sử dụng công cụ này để thiết lập CI/CD trên hệ thống K8s của mình.
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.10.1/cert-manager.yaml
kubectl create secret generic controller-manager -n actions-runner-system --from-literal=github_token=GITHUB_PAT
Trong đó GITHUB_PAT
là giá trị của token vừa tạo ở bước trên.
kubectl create -f https://github.com/actions/actions-runner-controller/releases/download/v0.26.0/actions-runner-controller.yaml
Bây giờ mình sẽ thử thiết lập 1 repo sử dụng Action self-hosted runner
Khi sử dụng ARC, mỗi repo trên github cần được deploy 1 RunnerDeployment trên K8s. Ví dụ mình thiết lập cho repo nvtienanh/django-test-github-runner-self-host-k8s
mình sử dụng 1 namepsace github-runner
để quản lý các RunnerDeployment. Ví dụ mình tạo 1 file django-arc.yaml
apiVersion: actions.summerwind.dev/v1alpha1 kind: RunnerDeployment metadata: name: django-test namespace: github-runner spec: replicas: 1 template: spec: image: summerwind/actions-runner-dind dockerdWithinRunnerContainer: true repository: nvtienanh/django-test-github-runner-self-host-k8s env: []
Sau đó deploy trên Kubernetes cluster:
kubectl create namespace github-runner kubectl apply -f django-arc.yaml
Cuối cùng trên github chúng ta sẽ tạo 1 workflow dùng để test, chi tiết có thể tham khảo repo ở trên
name: lint_python on: [pull_request, push] jobs: lint_python: runs-on: self-hosted steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - run: pip install --upgrade pip wheel - run: pip install bandit black codespell flake8 flake8-2020 flake8-bugbear flake8-comprehensions isort mypy pytest pyupgrade safety - run: bandit --recursive --skip B101,B105 . - run: black --check . || true # - run: codespell # --ignore-words-list="" --skip="*.css,*.js,*.lock" - run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - run: flake8 . --count --exit-zero --max-complexity=10 --max-line-length=88 --show-source --statistics - run: isort --check-only --profile black . || true - run: pip install -r requirements.txt || pip install --editable . || true - run: mkdir --parents --verbose .mypy_cache - run: mypy --ignore-missing-imports --install-types --non-interactive . || true - run: pytest --doctest-modules . - run: shopt -s globstar && pyupgrade --py36-plus **/*.py # - run: safety check
Kết quả:
Trên đây chỉ là 1 ví dụ đơn giản mà mình đã thực hiện, các bạn có thể tham khảo thêm trên repo actions-runner-controller
Thông tin