Gần đây tôi đã tạo Mẫu ứng dụng Portainer cho LinuxServer. io Docker container và Docker script cho LinuxServer. bộ chứa Docker io
Vì tôi đang tìm cách chạy các tập lệnh Python theo định kỳ và xuất bản đầu ra lên GitHub, nên tôi đã nghĩ đến việc xem qua GitHub Actions
Từ trang web Hành động GitHub
GitHub Actions giờ đây giúp bạn dễ dàng tự động hóa tất cả quy trình công việc phần mềm của mình với CI/CD đẳng cấp thế giới. Xây dựng, thử nghiệm và triển khai mã của bạn ngay từ GitHub. Thực hiện đánh giá mã, quản lý chi nhánh và phân loại vấn đề theo cách bạn muốn
Âm thanh khá tốt phải không?
Hóa ra GitHub Actions rất phù hợp với những gì tôi đang tìm kiếm. Và khá dễ dàng để thiết lập và duy trì
Hãy xem cách nó hoạt động
Tạo quy trình làm việc
Thiết lập quy trình làm việc
Từ kho lưu trữ trên GitHub, điều hướng đến tab Hành động
Chọn quy trình công việc sau đây từ “Quy trình công việc tích hợp liên tục” làm điểm bắt đầu
Chọn “Thiết lập quy trình làm việc này”
Thông tin chi tiết về Xây dựng và thử nghiệm Python có thể được tìm thấy tại đây
Thay đổi tên từ
1 thànhCode language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
2 và cam kết các thay đổiCode language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Bây giờ bạn có tệp YAML
2 trongCode language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
4 trông như thế này
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Code language: YAML [yaml]
Code language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Điều này có nghĩa là trên mỗi lần đẩy hoặc kéo_request, một công việc được thực hiện bao gồm một số bước
Bởi vì chúng tôi đã đẩy một tệp mới, quy trình làm việc sẽ chạy ngay lập tức. Điều này được thực hiện bởi người chạy. Người chạy là ứng dụng chạy một công việc từ một quy trình làm việc. Nó được sử dụng bởi GitHub Actions trong môi trường ảo được lưu trữ hoặc bạn có thể tự lưu trữ người chạy trong môi trường của riêng mình
Nếu chúng ta chuyển đến tab Hành động, chúng ta có thể thấy quy trình công việc đang chạy “Tạo bộ tạo_mẫu. yml” với dấu gạch chéo đỏ phía trước cho biết nó không thành công
Khi chúng tôi chọn quy trình công việc chạy và mở rộng công việc có tên là “xây dựng”, chúng tôi thấy rằng nó không thành công ở bước “Kiểm tra bằng pytest” với mã thoát 5
Mã thoát 5 có nghĩa là “Không có bài kiểm tra nào được thu thập”. Có thể hiểu được, bởi vì chúng tôi không tải lên hoặc chạy bất kỳ mã Python nào, chứ chưa nói đến việc kiểm tra
Đúng tiến độ
Hãy xem lại tệp
2Code language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Code language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Chúng tôi không cần chạy quy trình công việc trên mỗi lần đẩy hoặc kéo_request. Thay vào đó, chúng ta cần chạy quy trình công việc theo một lịch trình nhất định
Đối với điều này, chúng ta có thể sử dụng đoạn mã sau
Code language: YAML [yaml]
on: schedule: - cron: "* * * * *"
Trong đó “* * * * *” là một biểu thức cron
________số 8Bởi vì chúng tôi muốn chạy chương trình hàng ngày nên mã được đổi thành
on: schedule: - cron: "0 0 * * *"
Code language: YAML [yaml]
Thực thi tập lệnh Python
Chúng tôi cũng có thể xóa bước sau vì không có bài kiểm tra nào được chạy
0Code language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Thay vào đó, chúng ta cần một bước để thực thi tập lệnh Python
1Code language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Trong khi chúng tôi đang ở đó, hãy thay đổi tên của quy trình công việc thành Tạo mẫu
Quy trình làm việc bây giờ trông như thế này
2Code language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Đầu ra hiện được tạo trên máy chủ nơi công việc được thực thi. Vì vậy, bây giờ chúng ta cần đẩy dữ liệu sang kho lưu trữ khác nếu không dữ liệu sẽ bị xóa khi kết thúc quy trình làm việc
Đẩy tệp vào kho lưu trữ khác
Tập lệnh
6 được sử dụng trong Mẫu ứng dụng Portainer cho LinuxServer. io Docker container tạo ra một tập tin. Vì vậy, chúng ta cần tìm cách đẩy tệp đó đến kho lưu trữ đíchCode language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Đối với điều này, chúng ta có thể tìm kiếm GitHub Marketplace cho GitHub Actions
Hành động GitHub mà chúng tôi sẽ sử dụng là Đẩy tệp vào kho lưu trữ khác
4Code language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Bạn cần đặt
7 trong Cài đặt GitHub, Cài đặt dành cho nhà phát triển, Mã thông báo truy cập cá nhân [đặt phạm vi repo]. Sau đó, bạn cần thêm mã thông báo vào kho lưu trữ nơi quy trình công việc được chạy. Đối với điều này, hãy đi tới Cài đặt, Bí mật và thêm bí mật kho lưu trữCode language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Thêm bước vào tệp
2 và nó sẽ giống như thế này
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Code language: YAML [yaml]
7Code language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Đẩy một thư mục vào một kho lưu trữ khác
Tập lệnh
9 được sử dụng trong tập lệnh Docker cho LinuxServer. io Docker container tạo một thư mục. Vì vậy, chúng tôi không thể sử dụng Đẩy tệp vào kho lưu trữ khác Hành động GitHub cho việc này. Thay vào đó, chúng tôi sẽ sử dụng Đẩy thư mục sang kho lưu trữ khácCode language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Tạo quy trình làm việc mới. Thay đổi tên từ
1 thànhCode language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
1 và cam kết các thay đổiCode language: YAML [yaml]
on: schedule: - cron: "* * * * *"
Quy trình làm việc sẽ giống như quy trình chúng tôi đã tạo trước đó, ngoại trừ bước cuối cùng
Trong trường hợp này, bước cuối cùng sẽ giống như
0Code language: YAML [yaml]
name: Python application on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 uses: actions/setup-python@v2 with: python-version: 3.9 - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest
Xem kết quả quy trình làm việc
Khi quy trình làm việc đã chạy, chúng ta có thể xem kết quả của quy trình làm việc
Nếu chúng ta chuyển đến tab Hành động, chúng ta có thể thấy quy trình công việc chạy “Tạo mẫu” [hoặc “Tạo tập lệnh”] với dấu kiểm màu xanh lá cây ở phía trước có nghĩa là quy trình công việc đã hoàn tất thành công
Chúng ta cũng có thể thấy bước “Đẩy đầu ra vào thư mục đích” đã chạy thành công. Vì vậy, chúng ta sẽ thấy đầu ra trong kho lưu trữ đích
Lưu ý thời gian chạy cho mỗi bước. Số tiền này sẽ được khấu trừ vào “thời gian chạy” mà bạn còn lại trong tháng này
Nếu xảy ra lỗi, một thông báo e-mail sẽ được gửi. Nếu không, chúng tôi có thể cho rằng quy trình làm việc sẽ tiếp tục chạy theo lịch biểu
cân nhắc
Nếu chúng tôi truy cập Cài đặt GitHub, Cài đặt dành cho nhà phát triển, Thanh toán & gói, bạn có thể xem gói và mức sử dụng hiện tại của mình cho tháng này