Hành động github chạy tệp python

Hướng dẫn từng bước để tạo tệp readme mỗi khi bạn thay đổi cấu trúc tệp hoặc thêm tệp mới vào thư mục

Nguồn

Viết hành động Python GitHub của bạn

Bây giờ chúng ta đã tìm hiểu những điều cơ bản, hãy bắt đầu viết Hành động Python đầu tiên của chúng ta. Nếu bạn chưa có repo, hãy tạo một repo công khai mới trên GitHub. Bạn không thể tạo và phân phối Hành động GitHub của mình từ một kho lưu trữ riêng tư

Khi bạn đã tạo kho lưu trữ, hãy sao chép nó cục bộ và mở nó trên IDE yêu thích của bạn

git clone //github.com/shipyard/github-action-python-template.git && \\
cd github-action-python-template && \\
code .

Trước tiên, chúng tôi cần thêm tệp quan trọng nhất cho bất kỳ Hành động GitHub nào, cụ thể là,

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
8. Tệp này đóng vai trò là giao diện cho Hành động của chúng tôi và xác định đầu vào, đầu ra và lệnh chạy của nó. Nó sử dụng cú pháp YAML. Bạn có thể đọc thêm về các thành phần và cấu hình khác nhau cho
# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
8 tại đây

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
8 luôn phải bao gồm

  • Tên. Tên hành động của bạn. Điều này phải là duy nhất trên toàn cầu nếu bạn muốn xuất bản Hành động GitHub của mình lên Thị trường

  • sự mô tả. Mô tả ngắn về những gì Hành động của bạn thực hiện

  • đầu vào. Xác định các tham số đầu vào mà bạn có thể chuyển vào tập lệnh bash của mình. Chúng được đưa vào dưới dạng biến môi trường và bạn có thể truy cập chúng bằng

    # action.yaml
    name: 'Custom GitHub Action'
    description: 'A GitHub Action that takes an input and returns the square of the number'
    inputs:
      num:
        description: 'Enter a number'
        required: true
        default: "1"
    outputs:
      num_squared:
        description: 'Square of the input'
        # need to specify the extra `value` field for `composite` actions
        value: ${{ steps.get-square.outputs.num_squared }}
    runs:
      using: 'composite'
      steps:
        - name: Install Python
          uses: actions/setup-python@v4
          with:
            python-version: '3.10'  
        - name: Install Dependencies
          run: pip install -r requirements.txt
          shell: bash
        - name: Pass Inputs to Shell
          run: |
                  echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
          shell: bash
        - name: Fetch the number's square
          id: get-square
          run: python src/get_num_square.py
          shell: bash
    
    2

  • đầu ra. Xác định các tham số đầu ra mà bạn có thể sử dụng sau này trong một bước quy trình làm việc khác

  • chạy. Xác định những gì/ở đâu hành động sẽ thực hiện

Bây giờ, hãy tạo một Hành động GitHub đơn giản, hành động này sẽ lấy đầu vào là số nguyên và trả về hình vuông làm đầu ra. Đây là tệp

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
8 của chúng tôi mà chúng tôi sẽ sử dụng làm ví dụ

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash

Hãy xem qua tệp

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
8 của chúng tôi. Hãy tập trung vào phần
# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
5 vì các phần khác khá đơn giản

GitHub cung cấp ba tùy chọn cho các hành động. Docker, Javascript và tổng hợp. Bạn có thể đọc tất cả về ba tùy chọn và tùy chọn nào phù hợp nhất với trường hợp sử dụng của bạn tại đây

Cài đặt Python

- name: Install Python
  uses: actions/setup-python@v4
  with:
    python-version: '3.10'

Tận dụng Thị trường hành động GitHub

Chúng tôi đã bắt đầu với việc thêm tập lệnh shell tùy chỉnh để cài đặt Python cho Hành động của mình nhưng nhanh chóng nhận ra rằng chúng tôi không cần phải phát minh lại bánh xe. GitHub cung cấp một số hành động có thể được sử dụng, chẳng hạn như

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
6, mà chúng tôi sẽ sử dụng để cài đặt Python. Bạn có thể đọc thêm về Hành động này và các khả năng khác, chẳng hạn như Kiểm tra ma trận, lập phiên bản và lưu vào bộ nhớ đệm tại tài liệu chính thức tại đây

Cài đặt phụ thuộc

- name: Install Dependencies
  run: pip install -r requirements.txt

Mặc dù ví dụ của chúng tôi khá đơn giản và chúng tôi sẽ không cài đặt bất kỳ phần phụ thuộc nào, nhưng chúng tôi muốn bao gồm cách bạn sẽ cài đặt các phần phụ thuộc. Cập nhật lệnh cho trình quản lý gói bạn đang sử dụng, chẳng hạn như

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
7,
# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
8 hoặc
# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
9

Truyền đầu vào cho Shell [chỉ hợp lệ cho Người chạy tổng hợp]

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
0

Đây là điều khiến chúng tôi vấp ngã khi chúng tôi tạo Hành động GitHub của mình. Chúng tôi không thể tìm ra những gì chúng tôi đã làm sai và phải đào sâu để đi đến tận cùng của điều này. Đã xảy ra sự cố đang hoạt động khiến đầu vào không được đưa vào bộ chạy đối với bộ chạy

- name: Install Python
  uses: actions/setup-python@v4
  with:
    python-version: '3.10'
0. Như một giải pháp thay thế, chúng tôi sẽ thêm biến môi trường theo cách thủ công. Bạn có thể đọc về vấn đề ở đây

Chạy tập lệnh Python

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
2

Và cuối cùng, chúng ta sẽ chạy tập lệnh Python. Hãy nhanh chóng viết tập lệnh Python này và kiểm tra mọi thứ. Tạo một thư mục mới

- name: Install Python
  uses: actions/setup-python@v4
  with:
    python-version: '3.10'
1 và thêm một tệp mới
- name: Install Python
  uses: actions/setup-python@v4
  with:
    python-version: '3.10'
2, tệp này chỉ đặt đầu ra thành bình phương của số đầu vào

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
5

Sử dụng hành động

Bây giờ chúng ta đã sẵn sàng tất cả các phần, hãy thêm quy trình công việc vào kho lưu trữ của chúng ta và thêm Hành động mới dưới dạng một bước. Tạo một quy trình công việc mới trong thư mục

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
7

# action.yaml
name: 'Custom GitHub Action'
description: 'A GitHub Action that takes an input and returns the square of the number'
inputs:
  num:
    description: 'Enter a number'
    required: true
    default: "1"
outputs:
  num_squared:
    description: 'Square of the input'
    # need to specify the extra `value` field for `composite` actions
    value: ${{ steps.get-square.outputs.num_squared }}
runs:
  using: 'composite'
  steps:
    - name: Install Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'  
    - name: Install Dependencies
      run: pip install -r requirements.txt
      shell: bash
    - name: Pass Inputs to Shell
      run: |
              echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
      shell: bash
    - name: Fetch the number's square
      id: get-square
      run: python src/get_num_square.py
      shell: bash
6

Cam kết các thay đổi mới và chuyển đến tab Hành động để xem các vận động viên đang hoạt động

Chúng tôi có thể xác minh rằng đầu ra của chúng tôi là chính xác. Chúng tôi đã chuyển 11 làm đối số đầu vào và có thể thấy 121 trong đầu ra của Hành động

Phát hành một hành động GitHub

Khi bạn đã xác minh Hành động GitHub, bạn có thể phát hành nó bằng cách nhấp vào nút Dự thảo bản phát hành như trong ảnh chụp màn hình bên dưới

Mọi người có thể bắt đầu sử dụng Hành động mới này trong quy trình làm việc của họ trên khắp GitHub sau khi nó được phát hành thành công

Sự kết luận

Như bạn có thể thấy, GitHub Actions khá đơn giản để thiết lập và có thể trao quyền cho các nhà phát triển làm những điều tuyệt vời. GitHub Actions có một cộng đồng các nhà phát triển rất mạnh mẽ với một số mẫu Hành động, ví dụ và quy trình làm việc dựng sẵn nên bạn không phải bắt đầu lại từ đầu. Hy vọng điều này sẽ giúp bạn tạo một số Tác vụ GitHub tuyệt vời. Hãy cho chúng tôi biết quá trình đã diễn ra như thế nào và cuối cùng bạn đã tạo ra cái gì. Mã hóa vui vẻ

Tôi có thể chạy tập lệnh Python trong GitHub Actions 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.

Tôi có thể chạy tập lệnh trong GitHub không?

Thêm tập lệnh vào quy trình làm việc của bạn . Để biết thêm thông tin, hãy xem "Cú pháp quy trình làm việc cho Tác vụ 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."

Bạn có thể viết mã bằng Python trên GitHub không?

GitHub cung cấp quy trình làm việc dành cho người mới bắt đầu Python phù hợp với hầu hết các dự án Python .

Điều gì được chạy trong GitHub Actions?

GitHub Actions là nền tảng tích hợp liên tục và phân phối liên tục [CI/CD] cho phép bạn tự động hóa quy trình xây dựng, thử nghiệm và triển khai của mình. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.

Chủ Đề