Đầu ra tập lệnh python hành động github

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ừ

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]
1 thành

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]
2 và cam kết các thay đổi

Bây giờ bạn có tệp 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]
2 trong

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]
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]

Đ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

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]
2

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]

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

on: schedule: - cron: "* * * * *"

Code language: YAML [yaml]

Trong đó “* * * * *” là một biểu thức cron

________số 8

Bở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

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]
0

Thay vào đó, chúng ta cần một bước để thực thi tập lệnh Python

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]
1

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

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]
2

Đầ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

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]
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ữ đích

Đố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

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]
4

Bạn cần đặt

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]
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ữ

Thêm bước vào tệp

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]
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]
7

Đẩy một thư mục vào một kho lưu trữ khác

Tập lệnh

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]
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ác

Tạo quy trình làm việc mới. Thay đổi tên từ

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]
1 thành

on: schedule: - cron: "* * * * *"

Code language: YAML [yaml]
1 và cam kết các thay đổi

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ư

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]
0

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

Tác vụ GitHub có thể chạy tập lệnh Python không?

Chạy hành động tập lệnh Python. Viết tập lệnh Python trong tệp quy trình Hành động . Hành động này cho phép bạn xác định tập lệnh Python tùy chỉnh bên trong tệp YAML của quy trình làm việc. Viết mã Python của bạn làm đối số tập lệnh và sử dụng tính năng chuỗi nhiều dòng YAML để xác định tập lệnh nhiều dòng.

Làm cách nào để cài đặt Python trong GitHub Actions?

Chèn bước cài đặt phụ thuộc vào các bước. trường trong công việc quy trình làm việc GitHub với cấu hình như thế này. .
Mặc định. Sử dụng yêu cầu đường dẫn. .
Xác định các yêu cầu. đường dẫn txt. .
Tắt pip , setuptools và cài đặt/cập nhật bánh xe

Bạn có thể CD trong GitHub Actions không?

Bạn có thể tạo quy trình công việc triển khai liên tục [CD] tùy chỉnh trực tiếp trong kho lưu trữ GitHub của mình bằng Tác vụ GitHub .

Chủ Đề