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@v3 - uses: actions/setup-python@v4 with: python-version: '3.x' - 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@v3 - uses: actions/setup-python@v4 with: python-version: '3.x' - 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@v3 - uses: actions/setup-python@v4 with: python-version: '3.x' - 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@v3 - uses: actions/setup-python@v4 with: python-version: '3.x' - 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@v3 - uses: actions/setup-python@v4 with: python-version: '3.x' - 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@v3 - uses: actions/setup-python@v4 with: python-version: '3.x' - 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@v3 - uses: actions/setup-python@v4 with: python-version: '3.x' - 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@v3 - uses: actions/setup-python@v4 with: python-version: '3.x' - 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@v3 - uses: actions/setup-python@v4 with: python-version: '3.x' - 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@v3 - uses: actions/setup-python@v4 with: python-version: '3.x' - 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@v3 - uses: actions/setup-python@v4 with: python-version: '3.x' - 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ố Làm cách nào để chạy một tệp py trong 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?Ví dụ, thêm tập lệnh vào quy trình làm việc của bạn để chạy tập lệnh dưới dạng hành động, bạn có thể lưu trữ tập lệnh trong kho lưu trữ của mình và cung cấp đường dẫn và loại shell.Để biết thêm thông tin, hãy xem "Cú pháp quy trình công việc cho các hành động GitHub."you can store the script in your repository and supply the path and shell type. For more information, see "Workflow syntax for GitHub Actions."
Làm cách nào để chạy kịch bản trong tập lệnh Python?Các bước để chạy một kịch bản Python từ một kịch bản khác.. Bước 1: Đặt các tập lệnh Python vào cùng một thư mục.Để bắt đầu, hãy đặt các tập lệnh Python của bạn vào cùng một thư mục..... Bước 2: Thêm cú pháp.Tiếp theo, thêm cú pháp vào từng tập lệnh của bạn..... Bước 3: Chạy một kịch bản Python từ một bản khác .. Làm cách nào để chạy các hành động GitHub từ GitHub?Trên github.com, điều hướng đến trang chính của kho lưu trữ.Trong tên kho lưu trữ của bạn, nhấp vào hành động.Ở thanh bên trái, nhấp vào quy trình công việc bạn muốn chạy.Trên danh sách các quy trình công việc chạy, chọn Run Workflow. |