编写一个 GitHub Action 扩展服务
官方教程:https://docs.github.com/en/actions/tutorials/create-actions/create-a-composite-action?learn=create_actions&learnProduct=actions&platform=linux
但是该教程有点问题,入口文件会找不到。以下是一个示例:
目录结构如下:
├── action.yml
├── .github
│ └── workflows
│ └── test.yml
├── .gitignore
├── install.sh
├── LICENSE
└── README.md
action.yml
name: "Install Zig"
description: "Install Zig compiler with version input support."
author: "Jetsung Chan"
branding:
icon: "zap"
color: "blue"
inputs:
version:
description: "Zig version (e.g. 0.14.1, dev, master). Leave empty for latest stable."
required: false
default: ""
runs:
using: "composite"
steps:
- name: "Install Zig"
shell: bash
run: ${{ github.action_path }}/install.sh
env:
VERSION: ${{ inputs.version }}
install.sh
#!/usr/bin/env bash
set -e
VERSION_INPUT="${VERSION:-}"
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
case "$ARCH" in
x86_64) ARCH="x86_64" ;;
aarch64 | arm64) ARCH="aarch64" ;;
*) echo "Unsupported architecture: $ARCH" && exit 1 ;;
esac
if [[ -z "$VERSION_INPUT" ]]; then
VERSION=$(curl -s https://ziglang.org/download/index.json | \
jq -r 'keys
| map(select(.!="master"))
| sort_by(. | split(".")
| map(tonumber))
| reverse
| .[0]
')
elif [[ "$VERSION_INPUT" == "dev" || "$VERSION_INPUT" == "master" ]]; then
VERSION="master"
else
VERSION="$VERSION_INPUT"
fi
PACKAGES_DIR="$HOME/zig"
sudo mkdir -p "$PACKAGES_DIR"
export PATH="$PACKAGES_DIR:$PATH"
if [[ "$VERSION" == "master" ]]; then
URL=$(curl -s https://ziglang.org/download/index.json \
| jq -r ".master.\"${ARCH}-${OS}\".tarball")
else
URL=$(curl -s https://ziglang.org/download/index.json \
| jq -r ".\"$VERSION\".\"${ARCH}-${OS}\".tarball")
fi
# echo "Downloading Zig $VERSION for $ARCH-$OS from $URL"
curl -fsSL "$URL" | sudo tar xJC "$PACKAGES_DIR" --strip-components=1
echo "$PACKAGES_DIR" >> $GITHUB_PATH
# echo "Zig $VERSION installed successfully."
# zig version || true
测试 .github/workflows/test.yml
name: Test Install Zig Action
on: [ push ]
jobs:
test:
strategy:
matrix:
include:
- type: latest
version: 0.14.0
- type: master
version: master
- type: empty
version:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Zig ${{ matrix.version }}
uses: ./
with:
version: ${{ matrix.version }}
- name: Check Zig version
run: zig version
完整代码:https://github.com/jetsung/install-zig
Marketplace:https://github.com/marketplace/actions/install-zig
发布至 Marketplace 时,需要创建 release
,并且勾选 Publish this Action to the GitHub Marketplace
。若需要类似 @v1
这样的版本区分,则需要创建对应的索引。命令行操作如下:
# 创建主标签
git tag v0.0.3
# 创建索引标签
git tag v1 v0.0.3
# 推送标签
git push origin v0.0.3
git push origin v1
注意:不发布至 Marketplace,亦可正常使用。格式为 username/repo@version
。