Code Issues Pull Requests Actions Packages Projects Releases 15 Wiki Activity Security Code Quality
21 Commits
8 changed files with 396 additions and 31 deletions

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
# Actions
## load-env
Copy các biến môi trường có sẵn trong process của runner vào env của job. Bước phía sau đọc được bằng `${{ env.TÊN }}`.
Biến khai báo trên runner không tự vào context `env`. Context đó chỉ chứa biến do workflow đặt. Action đọc biến trong process, ghi vào `$GITHUB_ENV`, và mask giá trị trong log.
### Usage
```yaml
- uses: https://git.sothink.vn/gitea/global-workflow/.gitea/actions/load-env@main
with:
names: RELEASE_TOKEN DT_API_KEY
- name: Use the loaded values
run: echo "token length ${#RELEASE_TOKEN}"
env:
API_KEY: ${{ env.DT_API_KEY }}
```
`names` là danh sách tên, cách nhau bởi khoảng trắng, load đúng thứ tự đó. Trong repo này, workflow release load `GITHUB_TOKEN RELEASE_TOKEN`; workflow quét lỗ hổng load `DT_API_KEY`.
Cùng repo, sau khi đã checkout, có thể gọi local:
```yaml
- uses: ./.gitea/actions/load-env
with:
names: RELEASE_TOKEN
```
Scoped workflow chạy trên repo khác thì không dùng `uses: ./`. Checkout của repo đó không có action này. Dùng URL đầy đủ ở trên. Ref `@main` lấy code trên nhánh mặc định, nên action phải được push lên `main` trước khi workflow khác gọi.
### Inputs
| Input | Bắt buộc | Mặc định | Ý nghĩa |
|---|---|---|---|
| `names` | có | | Tên biến, cách nhau bởi khoảng trắng. Chỉ nhận `[A-Za-z_][A-Za-z0-9_]*`. |
| `required` | không | `true` | `true`: thiếu hoặc rỗng thì dừng job. `false`: bỏ qua biến không có và in `Skipping unset …`. |
### Biến phải có trong container của job
act_runner không đưa env của máy host vào container trừ khi khai báo trong cấu hình runner:
```yaml
runner:
envs:
RELEASE_TOKEN: "…"
DT_API_KEY: "…"
```
Thiếu biến, với `required: true`, log có dòng `<TÊN> is not set on this runner`.
+51
View File
@@ -0,0 +1,51 @@
name: Load runner environment
description: Copy named variables from the runner process into the job env context.
inputs:
names:
description: Space-separated variable names, loaded in the given order.
required: true
required:
description: Fail when a named variable is missing or empty.
required: false
default: "true"
runs:
using: composite
steps:
- name: Load runner environment
shell: bash
env:
LOAD_ENV_NAMES: ${{ inputs.names }}
LOAD_ENV_REQUIRED: ${{ inputs.required }}
run: |
set -euo pipefail
if [[ -z "${LOAD_ENV_NAMES// }" ]]; then
echo "No environment variable names were given"
exit 1
fi
for name in $LOAD_ENV_NAMES; do
if [[ ! "$name" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
echo "Invalid environment variable name: ${name}"
exit 1
fi
value="${!name-}"
if [[ -z "$value" ]]; then
if [[ "${LOAD_ENV_REQUIRED}" == "true" ]]; then
echo "${name} is not set on this runner"
exit 1
fi
echo "Skipping unset ${name}"
continue
fi
if [[ "$value" != *$'\n'* ]]; then
echo "::add-mask::${value}"
fi
delimiter="LOAD_ENV_${name}_${RANDOM}_$$"
{
printf '%s<<%s\n' "$name" "$delimiter"
printf '%s\n' "$value"
printf '%s\n' "$delimiter"
} >> "$GITHUB_ENV"
echo "Loaded ${name} from the runner"
done
@@ -0,0 +1,61 @@
# resolve-branch-for-tag
Tìm nhánh chứa commit của tag đang checkout. Output `name` là tên nhánh đó.
Tag không thuộc về một nhánh. Action nhìn các ref trên remote, ưu tiên nhánh có tip đúng commit của tag. Không có nhánh nào trùng tip thì lấy nhánh chứa commit đó. Trong danh sách ứng viên, tên trong `prefer` được chọn trước. Không tìm thấy nhánh nào thì dùng `fallback`.
## Usage
Checkout full history trước. Clone nông làm `--contains` thiếu commit.
```yaml
- uses: actions/checkout@v7
with:
fetch-depth: 0
- id: branch
uses: https://git.sothink.vn/gitea/global-workflow/.gitea/actions/resolve-branch-for-tag@main
- run: echo "branch is ${{ steps.branch.outputs.name }}"
```
Cùng repo, sau khi checkout:
```yaml
- id: branch
uses: ./.gitea/actions/resolve-branch-for-tag
```
Scoped workflow trên repo khác dùng URL đầy đủ. `uses: ./` chỉ thấy checkout của repo đang chạy.
Mặc định ưu tiên `main`, rồi `develop`. Đổi thứ tự hoặc nhánh dự phòng:
```yaml
- id: branch
uses: https://git.sothink.vn/gitea/global-workflow/.gitea/actions/resolve-branch-for-tag@main
with:
prefer: develop main
fallback: develop
```
## Inputs
| Input | Bắt buộc | Mặc định | Ý nghĩa |
|---|---|---|---|
| `prefer` | không | `main develop` | Tên nhánh, cách nhau bởi khoảng trắng. Tên nào có trong danh sách ứng viên thì được chọn trước. |
| `fallback` | không | `develop` | Dùng khi không có nhánh remote nào chứa commit. |
| `remote` | không | `origin` | Remote giữ ref của các nhánh. `actions/checkout` tạo `origin`. |
## Output
| Output | Ý nghĩa |
|---|---|
| `name` | Tên nhánh đã chọn. Đọc bằng `${{ steps.<id>.outputs.name }}`. |
## Cách chọn
1. Nhánh có tip đúng commit của tag (`--points-at`).
2. Nếu không có, nhánh chứa commit đó (`--contains`).
3. Trong các ứng viên, lấy tên đầu tiên của `prefer` khớp chính xác.
4. Không khớp tên nào thì lấy ứng viên đầu tiên.
5. Không có ứng viên thì lấy `fallback` và in `No branch contains <sha>`.
@@ -0,0 +1,62 @@
name: Resolve branch for tag
description: Find the branch that contains the checked-out tag commit.
inputs:
prefer:
description: Space-separated branch names. The first one present in the candidate list wins.
required: false
default: main develop
fallback:
description: Branch used when no remote branch contains the commit.
required: false
default: develop
remote:
description: Git remote that holds the branch refs.
required: false
default: origin
outputs:
name:
description: Resolved branch name.
value: ${{ steps.tag-branch.outputs.name }}
runs:
using: composite
steps:
- name: Resolve branch for tag
id: tag-branch
shell: bash
env:
PREFER: ${{ inputs.prefer }}
FALLBACK: ${{ inputs.fallback }}
REMOTE: ${{ inputs.remote }}
run: |
set -euo pipefail
git fetch "$REMOTE" "+refs/heads/*:refs/remotes/${REMOTE}/*"
strip="s|^${REMOTE}/||"
exact=$(git for-each-ref --points-at HEAD --format='%(refname:short)' "refs/remotes/${REMOTE}" \
| sed "$strip" \
| grep -v '^HEAD$' || true)
if [[ -n "$exact" ]]; then
candidates="$exact"
else
candidates=$(git for-each-ref --contains HEAD --format='%(refname:short)' "refs/remotes/${REMOTE}" \
| sed "$strip" \
| grep -v '^HEAD$' || true)
fi
branch=""
for preferred in $PREFER; do
branch=$(printf '%s\n' "$candidates" | grep -x "$preferred" || true)
if [[ -n "$branch" ]]; then
break
fi
done
if [[ -z "$branch" ]]; then
branch=$(printf '%s\n' "$candidates" | head -n 1)
fi
if [[ -z "$branch" ]]; then
branch="$FALLBACK"
echo "No branch contains ${GITHUB_SHA}; using ${branch}"
fi
echo "Ref ${GITHUB_REF_NAME} is on branch ${branch}"
echo "name=${branch}" >> "$GITHUB_OUTPUT"
@@ -0,0 +1,118 @@
name: Conventional Commits Check
on:
pull_request:
types: [opened, edited, synchronize, reopened]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
BUN_VERSION: 1.4.5
SKIP_AUTHORS: renovate renovatebot
jobs:
lint-commits-and-pr:
runs-on: ubuntu-latest
steps:
- name: Skip bot authors
id: author
env:
AUTHOR: ${{ github.event.pull_request.user.login }}
run: |
set -euo pipefail
author=$(printf '%s' "$AUTHOR" | tr '[:upper:]' '[:lower:]')
skip=false
for name in $SKIP_AUTHORS; do
name=$(printf '%s' "$name" | tr '[:upper:]' '[:lower:]')
if [[ "$author" == "$name" ]]; then
skip=true
break
fi
done
if [[ "$skip" == true || "$author" == *bot || "$author" == *'[bot]' ]]; then
echo "skip=true" >> "$GITHUB_OUTPUT"
echo "Skipping commitlint for ${AUTHOR}"
else
echo "skip=false" >> "$GITHUB_OUTPUT"
fi
- name: Checkout code
if: steps.author.outputs.skip != 'true'
uses: actions/checkout@v4
with:
fetch-depth: 0
# Local config wins, in this order. Download the shared file only when none exist.
- name: Resolve commitlint config
if: steps.author.outputs.skip != 'true'
id: commitlint-config
env:
COMMITLINT_REPO: ${{ vars.COMMITLINT_CONFIG_REPO || 'gitea/global-workflow' }}
COMMITLINT_CONFIGS: commitlint.config.js .github/commitlint.config.js .gitea/commitlint.config.js
run: |
set -euo pipefail
for candidate in $COMMITLINT_CONFIGS; do
if [[ -f "$candidate" ]]; then
echo "Using ${candidate} from this repository"
echo "config=${candidate}" >> "$GITHUB_OUTPUT"
exit 0
fi
done
url="${GITHUB_API_URL}/repos/${COMMITLINT_REPO}/raw/commitlint.config.js?ref=main"
echo "No local commitlint.config.js; fetching ${url}"
curl --silent --show-error --fail --location --output commitlint.config.js "$url"
echo "config=commitlint.config.js" >> "$GITHUB_OUTPUT"
- name: Setup Bun
if: steps.author.outputs.skip != 'true'
uses: oven-sh/setup-bun@v2
with:
bun-version: ${{ env.BUN_VERSION }}
# Install outside the repo so bun does not migrate its lockfile or install its dependencies.
- name: Install commitlint
if: steps.author.outputs.skip != 'true'
run: |
set -euo pipefail
dir="${RUNNER_TEMP}/commitlint"
mkdir -p "$dir"
bun add --cwd "$dir" @commitlint/cli @commitlint/config-conventional
if [[ -e node_modules && ! -L node_modules ]]; then
ln -sfn "$dir/node_modules/@commitlint" node_modules/@commitlint
else
ln -sfn "$dir/node_modules" node_modules
fi
echo "COMMITLINT_BIN=${dir}/node_modules/.bin/commitlint" >> "$GITHUB_ENV"
- name: Validate PR Title & Description (<= 500 chars)
if: steps.author.outputs.skip != 'true'
env:
PR_TITLE: ${{ github.event.pull_request.title }}
PR_BODY: ${{ github.event.pull_request.body }}
COMMITLINT_CONFIG: ${{ steps.commitlint-config.outputs.config }}
run: |
if [ -z "$PR_BODY" ]; then
printf "%s\n" "$PR_TITLE" | "$COMMITLINT_BIN" --config "$COMMITLINT_CONFIG"
else
printf "%s\n\n%s\n" "$PR_TITLE" "$PR_BODY" | "$COMMITLINT_BIN" --config "$COMMITLINT_CONFIG"
fi
- name: Validate All Commits in PR
if: steps.author.outputs.skip != 'true'
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
COMMITLINT_CONFIG: ${{ steps.commitlint-config.outputs.config }}
run: |
set -euo pipefail
mapfile -t shas < <(git rev-list --no-merges "$BASE_SHA...$HEAD_SHA")
if [[ ${#shas[@]} -eq 0 ]]; then
echo "No non-merge commits to lint"
exit 0
fi
for sha in "${shas[@]}"; do
echo "Linting ${sha}"
git log -1 --format=%B "$sha" | "$COMMITLINT_BIN" --verbose --config "$COMMITLINT_CONFIG"
done
+23 -21
View File
@@ -3,7 +3,7 @@ name: Release
on: on:
push: push:
tags: tags:
- "**" - "v*.*.*"
permissions: permissions:
contents: write contents: write
@@ -23,44 +23,46 @@ jobs:
with: with:
fetch-depth: 0 fetch-depth: 0
# Public source. A repo without its own cliff.toml downloads this file. # Local config wins, in this order. Download the shared file only when none exist.
- name: Use shared cliff.toml when the repo has none - name: Resolve cliff.toml
id: cliff-config
env: env:
CLIFF_REPO: ${{ vars.CLIFF_CONFIG_REPO || 'gitea/global-workflow' }} CLIFF_REPO: ${{ vars.CLIFF_CONFIG_REPO || 'gitea/global-workflow' }}
CLIFF_CONFIGS: cliff.toml .github/cliff.toml .gitea/cliff.toml
run: | run: |
set -euo pipefail set -euo pipefail
if [[ -f cliff.toml ]]; then for candidate in $CLIFF_CONFIGS; do
echo "Using cliff.toml from this repository" if [[ -f "$candidate" ]]; then
exit 0 echo "Using ${candidate} from this repository"
fi echo "config=${candidate}" >> "$GITHUB_OUTPUT"
exit 0
fi
done
url="${GITHUB_API_URL}/repos/${CLIFF_REPO}/raw/cliff.toml?ref=main" url="${GITHUB_API_URL}/repos/${CLIFF_REPO}/raw/cliff.toml?ref=main"
echo "No cliff.toml in this repository; fetching ${url}" echo "No local cliff.toml; fetching ${url}"
curl --silent --show-error --fail --location --output cliff.toml "$url" curl --silent --show-error --fail --location --output cliff.toml "$url"
echo "config=cliff.toml" >> "$GITHUB_OUTPUT"
- name: Read envs from the runner
uses: https://git.sothink.vn/gitea/global-workflow/.gitea/actions/load-env@main
with:
names: GITHUB_TOKEN RELEASE_TOKEN
# Gitea's GITHUB_TOKEN is rejected by api.github.com. Pass a github.com PAT instead. # Gitea's GITHUB_TOKEN is rejected by api.github.com. Pass a github.com PAT instead.
- name: Generate release notes - name: Generate release notes
uses: orhun/git-cliff-action@v4 uses: orhun/git-cliff-action@v4
with: with:
config: cliff.toml config: ${{ steps.cliff-config.outputs.config }}
args: --latest --strip header args: --latest --strip header
github_token: ${{ env.GITHUB_TOKEN }}
env: env:
OUTPUT: release-notes.md OUTPUT: release-notes.md
- name: Read Release Token from the runner
run: |
if [[ -z "${RELEASE_TOKEN:-}" ]]; then
exit 1
fi
{
echo "RELEASE_TOKEN<<__RELEASE_TOKEN__"
printf '%s\n' "$RELEASE_TOKEN"
echo "__RELEASE_TOKEN__"
} >> "$GITHUB_ENV"
- name: Create release - name: Create release
uses: softprops/action-gh-release@v3 uses: softprops/action-gh-release@v3
with: with:
# prerelease only when the tag contains "-alpha", "-beta" or "-rc". ex: v1.0.0-alpha.1, v1.0.0-beta.1, v1.0.0-rc.1 # prerelease only when the tag contains "-alpha", "-beta" or "-rc". ex: v1.0.0-alpha.1, v1.0.0-beta.1, v1.0.0-rc.1
prerelease: ${{ contains(github.ref, '-alpha') || contains(github.ref, '-beta') || contains(github.ref, '-rc') }} prerelease: ${{ contains(github.ref, '-alpha') || contains(github.ref, '-beta') || contains(github.ref, '-rc') }}
body_path: release-notes.md body_path: release-notes.md
token: ${{ env.RELEASE_TOKEN }} token: ${{ env.RELEASE_TOKEN }} # Use this token if you want user author is 'github-actions' bot
draft: false
@@ -7,6 +7,10 @@ on:
push: push:
branches: [main] branches: [main]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs: jobs:
sbom-upload: sbom-upload:
runs-on: ubuntu-latest runs-on: ubuntu-latest
@@ -22,16 +26,9 @@ jobs:
upload-artifact: false upload-artifact: false
- name: Read API key from the runner - name: Read API key from the runner
run: | uses: https://git.sothink.vn/gitea/global-workflow/.gitea/actions/load-env@main
if [[ -z "${DT_API_KEY:-}" ]]; then with:
echo "DT_API_KEY is not set on this runner" names: DT_API_KEY
exit 1
fi
{
echo "DT_API_KEY<<__DT_API_KEY__"
printf '%s\n' "$DT_API_KEY"
echo "__DT_API_KEY__"
} >> "$GITHUB_ENV"
- name: Upload SBOM to Dependency-Track - name: Upload SBOM to Dependency-Track
uses: DependencyTrack/gh-upload-sbom@v4 uses: DependencyTrack/gh-upload-sbom@v4
+22
View File
@@ -0,0 +1,22 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
// Allow all types (following the Conventional Commits specification)
'type-enum': [
2,
'always',
['build', 'chore', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'style', 'test'],
],
// Require type and description (cannot be empty)
'type-empty': [2, 'never'],
'subject-empty': [2, 'never'],
// Limit the length of the Title (header) to 72 characters
'header-max-length': [2, 'always', 72],
// Require a blank line between title and body/footer (following the Conventional Commits specification)
'body-leading-blank': [2, 'always'],
'footer-leading-blank': [2, 'always'],
// Limit the length of each line in body/description to 500 characters (optional)
'body-max-line-length': [2, 'always', 500],
},
};