Hướng dẫn github action run python file - hành động github chạy tệp python
Chạy hành động tập lệnh PythonViết các tập lệnh Python trong một tệp quy trình làm việc của hành động! Show
Hành động này cho phép bạn xác định một tập lệnh python tùy chỉnh bên trong tệp yaml công việc. Viết mã Python của bạn làm đối số Yêu cầu duy nhất là bạn thiết lập môi trường Python trước khi chạy hành động. Dưới đây là một quy trình công việc ví dụ in nội dung thư mục gốc của kho lưu trữ vào nhật ký hành động: name: Run Script on: push: branches: [ master ] pull_request: branches: [ master ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 with: script: | import os print("Directory contents:") for f in os.listdir(): print(f) Xử lý lỗiTheo mặc định, hành động sẽ thất bại nếu nó gặp bất kỳ lỗi nào khi cố gắng chạy tập lệnh của bạn. Bạn có thể ghi đè này với đầu vào Hành động đặt ba đầu ra:
Nhìn vào đoạn trích sau đây để xem cách xử lý lỗi hoạt động trong thực tế: name: Run Script on: push: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 id: script with: fail-on-error: false script: | print("Doing something that will fail") a = [] a[10] - name: Print errors if: steps.script.outputs.error == 'true' run: | printenv "SCRIPT_STDOUT" printenv "SCRIPT_STDERR" env: SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }} SCRIPT_STDERR: ${{ steps.script.outputs.stderr }} Các chức năng tiện íchHành động đi kèm với các tiện ích mà bạn có thể sử dụng để tương tác với quy trình làm việc. Nếu bạn muốn vô hiệu hóa các tiện ích này, bạn có thể đặt name: Run Script on: push: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 id: script with: fail-on-error: false script: | print("Doing something that will fail") a = [] a[10] - name: Print errors if: steps.script.outputs.error == 'true' run: | printenv "SCRIPT_STDOUT" printenv "SCRIPT_STDERR" env: SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }} SCRIPT_STDERR: ${{ steps.script.outputs.stderr }}0 làm đầu vào cho bước. Bạn có thể gọi các chức năng này trực tiếp từ script của bạn mà không cần phải nhập bất cứ thứ gì.Example: - uses: jannekem/run-python-script-action@v1 with: script: | add_path("/usr/local/test") set_env("HELLO", "WORLD") group("Messages") print("Sending a message") warning("There might be an issue") end_group() Thêm đường dẫnChữ ký: name: Run Script on: push: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 id: script with: fail-on-error: false script: | print("Doing something that will fail") a = [] a[10] - name: Print errors if: steps.script.outputs.error == 'true' run: | printenv "SCRIPT_STDOUT" printenv "SCRIPT_STDERR" env: SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }} SCRIPT_STDERR: ${{ steps.script.outputs.stderr }}2 Trao tiền cho đường dẫn hệ thống. Sự thay đổi sẽ chỉ ảnh hưởng đến các bước sau. Nhận đầu vàoChữ ký: name: Run Script on: push: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 id: script with: fail-on-error: false script: | print("Doing something that will fail") a = [] a[10] - name: Print errors if: steps.script.outputs.error == 'true' run: | printenv "SCRIPT_STDOUT" printenv "SCRIPT_STDERR" env: SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }} SCRIPT_STDERR: ${{ steps.script.outputs.stderr }}3 Trả về giá trị của đầu vào đã cho dưới dạng chuỗi. Đặt đầu raChữ ký: name: Run Script on: push: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 id: script with: fail-on-error: false script: | print("Doing something that will fail") a = [] a[10] - name: Print errors if: steps.script.outputs.error == 'true' run: | printenv "SCRIPT_STDOUT" printenv "SCRIPT_STDERR" env: SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }} SCRIPT_STDERR: ${{ steps.script.outputs.stderr }}4 Đặt một tham số đầu ra. Đặt envChữ ký: name: Run Script on: push: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 id: script with: fail-on-error: false script: | print("Doing something that will fail") a = [] a[10] - name: Print errors if: steps.script.outputs.error == 'true' run: | printenv "SCRIPT_STDOUT" printenv "SCRIPT_STDERR" env: SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }} SCRIPT_STDERR: ${{ steps.script.outputs.stderr }}5 Đặt một biến môi trường để sử dụng trong các bước sau. Gỡ lỗiChữ ký: name: Run Script on: push: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 id: script with: fail-on-error: false script: | print("Doing something that will fail") a = [] a[10] - name: Print errors if: steps.script.outputs.error == 'true' run: | printenv "SCRIPT_STDOUT" printenv "SCRIPT_STDERR" env: SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }} SCRIPT_STDERR: ${{ steps.script.outputs.stderr }}6 Gửi tin nhắn gỡ lỗi. Thông báo sẽ chỉ hiển thị khi ghi nhật ký gỡ lỗi đã được bật. Cảnh báoChữ ký: name: Run Script on: push: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 id: script with: fail-on-error: false script: | print("Doing something that will fail") a = [] a[10] - name: Print errors if: steps.script.outputs.error == 'true' run: | printenv "SCRIPT_STDOUT" printenv "SCRIPT_STDERR" env: SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }} SCRIPT_STDERR: ${{ steps.script.outputs.stderr }}7 Gửi một thông báo cảnh báo sẽ được tô sáng với màu vàng trong nhật ký công việc. LỗiChữ ký: name: Run Script on: push: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 id: script with: fail-on-error: false script: | print("Doing something that will fail") a = [] a[10] - name: Print errors if: steps.script.outputs.error == 'true' run: | printenv "SCRIPT_STDOUT" printenv "SCRIPT_STDERR" env: SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }} SCRIPT_STDERR: ${{ steps.script.outputs.stderr }}8 Gửi một thông báo lỗi sẽ được Higlight với màu đỏ trong nhật ký quy trình làm việc. Tập đoànChữ ký: name: Run Script on: push: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: jannekem/run-python-script-action@v1 id: script with: fail-on-error: false script: | print("Doing something that will fail") a = [] a[10] - name: Print errors if: steps.script.outputs.error == 'true' run: | printenv "SCRIPT_STDOUT" printenv "SCRIPT_STDERR" env: SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }} SCRIPT_STDERR: ${{ steps.script.outputs.stderr }}9 Tạo một nhóm có thể mở rộng trong nhật ký quy trình làm việc. Kết thúc nhómChữ ký: - uses: jannekem/run-python-script-action@v1 with: script: | add_path("/usr/local/test") set_env("HELLO", "WORLD") group("Messages") print("Sending a message") warning("There might be an issue") end_group()0 Kết thúc một nhóm. Tất cả các dòng in trước khi gọi - uses: jannekem/run-python-script-action@v1 with: script: | add_path("/usr/local/test") set_env("HELLO", "WORLD") group("Messages") print("Sending a message") warning("There might be an issue") end_group()0 thuộc nhóm được xác định trước đó. Thêm mặt nạChữ ký: - uses: jannekem/run-python-script-action@v1 with: script: | add_path("/usr/local/test") set_env("HELLO", "WORLD") group("Messages") print("Sending a message") warning("There might be an issue") end_group()2 Mặt nạ ra dữ liệu nhạy cảm và thay thế nó bằng - uses: jannekem/run-python-script-action@v1 with: script: | add_path("/usr/local/test") set_env("HELLO", "WORLD") group("Messages") print("Sending a message") warning("There might be an issue") end_group()3. Giá trị có thể là một chuỗi ("dữ liệu nhạy cảm của tôi") hoặc nó có thể trỏ đến một biến môi trường ("$ db_password"). Dừng lệnhChữ ký: - uses: jannekem/run-python-script-action@v1 with: script: | add_path("/usr/local/test") set_env("HELLO", "WORLD") group("Messages") print("Sending a message") warning("There might be an issue") end_group()4 Tất cả các lệnh sẽ dừng xử lý. Nó cho phép bạn đăng nhập bất cứ thứ gì mà không vô tình kích hoạt các lệnh quy trình công việc. Tiếp tục lệnhChữ ký: - uses: jannekem/run-python-script-action@v1 with: script: | add_path("/usr/local/test") set_env("HELLO", "WORLD") group("Messages") print("Sending a message") warning("There might be an issue") end_group()5 Tiếp tục xử lý lệnh. Nhận trạng tháiChữ ký: - uses: jannekem/run-python-script-action@v1 with: script: | add_path("/usr/local/test") set_env("HELLO", "WORLD") group("Messages") print("Sending a message") warning("There might be an issue") end_group()6 Chia sẻ các biến môi trường với các hành động - uses: jannekem/run-python-script-action@v1 with: script: | add_path("/usr/local/test") set_env("HELLO", "WORLD") group("Messages") print("Sending a message") warning("There might be an issue") end_group()7 và - uses: jannekem/run-python-script-action@v1 with: script: | add_path("/usr/local/test") set_env("HELLO", "WORLD") group("Messages") print("Sending a message") warning("There might be an issue") end_group()8 của bạn. Xem tài liệu chính thức để biết thêm chi tiết. Giữ trạng tháiChữ ký: - uses: jannekem/run-python-script-action@v1 with: script: | add_path("/usr/local/test") set_env("HELLO", "WORLD") group("Messages") print("Sending a message") warning("There might be an issue") end_group()9 Lưu giá trị như một biến môi trường với tiền tố Tôi có thể chạy tập lệnh Python trong các hành động github không?Chạy hành động tập lệnh Python. Viết các tập lệnh Python trong một tệp quy trình làm việc của hành động! Hành động này cho phép bạn xác định một tập lệnh python tùy chỉnh bên trong tệp yaml công 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 đa dòng YAML để xác định các tập lệnh đa dòng.Write Python scripts in an Actions workflow file! This action lets you define a custom Python script inside the workflow YAML file. Write your Python code as the script argument, and use the YAML multiline string feature to define multiline scripts.
Làm cách nào để chạy một tệp python từ github?Chạy tập lệnh Python từ GitHub (bảng điều khiển).. Trong loại nguồn, chọn GitHub .. Trong hộp thông tin nguồn, nhập thông tin cần thiết để truy cập nguồn theo định dạng sau: .... Đối với dòng lệnh, nhập tham số để thực thi tập lệnh .. Tôi có thể chạy tập lệnh trong GitHub không?Bạn có thể sử dụng các hành động để chạy các tập lệnh và lệnh shell, sau đó được thực thi trên người chạy được chỉ định., which are then executed on the assigned runner.
Làm cách nào để chạy một hành động pytest trong github?Để sử dụng phiên bản được cài đặt sẵn của Python hoặc Pypy trên một người chạy được lưu trữ github, hãy sử dụng hành động thiết lập-python.Hành động này tìm thấy một phiên bản cụ thể của Python hoặc Pypy từ bộ đệm công cụ trên mỗi người chạy và thêm các nhị phân cần thiết vào đường dẫn, tồn tại trong phần còn lại của công việc.use the setup-python action. This action finds a specific version of Python or PyPy from the tools cache on each runner and adds the necessary binaries to PATH , which persists for the rest of the job. |