Gần đây, chúng tôi đã phát hành Hành động GitHub chính thức của Shipyard và chúng tôi muốn chia sẻ những phát hiện và kinh nghiệm của mình từ việc tạo Hành động GitHub đầu tiên của chúng tôi. Trong bài đăng này, bạn sẽ học cách viết một Hành động GitHub đơn giản bằng Python
Tổng quan ngắn gọn về các hành động GitHub
Vào năm 2019, GitHub đã phát hành công cụ CI của riêng mình có tên là GitHub Actions. Theo GitHub, Actions giúp các nhà phát triển dễ dàng tự động hóa các tác vụ trong vòng đời phát triển phần mềm [SDLC]. Tích hợp hệ sinh thái bản địa cho phép các dự án được tự động hóa ngay từ thời điểm các nhà phát triển cam kết mã để triển khai vào sản xuất
Trước khi xem một Hành động GitHub mẫu, chúng ta sẽ tự làm quen với cú pháp và thuật ngữ mà chúng ta sẽ gặp
quy trình làm việc. Một quy trình tự động có thể định cấu hình sẽ chạy một hoặc nhiều công việc. Quy trình công việc được xác định bởi tệp YAML trong repo của bạn và sẽ chạy khi được kích hoạt bởi một sự kiện trong repo của bạn. Chúng cũng có thể được kích hoạt theo cách thủ công hoặc theo lịch trình đã xác định. Quy trình công việc được xác định trong thư mục
7 trong repo và chúng tôi có thể có nhiều quy trình công việc trên mỗi repo, mỗi quy trình có thể thực hiện một nhóm tác vụ khác nhau# 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
Biến cố. Một hoạt động kích hoạt quy trình công việc;
Nghề nghiệp. Một nhiệm vụ trong một quy trình công việc duy nhất. Một quy trình công việc có thể bao gồm một hoặc nhiều công việc và tất cả các công việc cần được thực thi mà không có bất kỳ lỗi nào để quy trình công việc thành công
Bước chân. Một nhiệm vụ nhỏ hơn được thực hiện trong một công việc. Tất cả các bước phải được hoàn thành để hoàn thành một công việc
Hoạt động. Một lệnh độc lập được thực hiện trong một bước. Bạn có thể viết Tác vụ của riêng mình hoặc bạn có thể tìm thấy Tác vụ để sử dụng trong quy trình làm việc của mình trong Thị trường GitHub
người chạy. Máy chủ chạy quy trình công việc của bạn khi chúng được kích hoạt. Mỗi người chạy có thể chạy một công việc tại một thời điểm. GitHub cung cấp trình chạy Ubuntu Linux, Microsoft Windows và macOS cho quy trình làm việc;
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ồmTê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ự miêu 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
2# 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
đầ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ảnGitHub 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, tạo phiên bản và lưu vào bộ nhớ đệm tại tài liệu chính thức tại đâyCà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
9Truyề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 đề ở đâyChạ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
2Và 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
5Sử 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
6Cam 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ẻ