Pve (#514) #143
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: iStore Pending CI | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- pending | |
env: | |
TZ: Asia/Shanghai | |
MG_BASE_BRANCH: main | |
MG_HEAD_BRANCH: pending | |
CI_USE_PR: false | |
CI_AUTO_BRANCH: ci/automerging | |
jobs: | |
pending: | |
if: github.event_name != 'push' || ! (github.event.forced || github.triggering_actor != 'github-merge-queue[bot]') | |
runs-on: ubuntu-latest | |
name: iStore Pending CI | |
steps: | |
- name: Dump GitHub context | |
env: | |
GITHUB_CONTEXT: ${{ toJson(github) }} | |
run: echo "$GITHUB_CONTEXT" | |
- uses: actions/create-github-app-token@v1 | |
if: env.CI_USE_PR == 'false' | |
id: app-token | |
with: | |
app-id: ${{ vars.BOT_APP_ID }} | |
private-key: ${{ secrets.BOT_PRIVATE_KEY }} | |
- name: Checkout BASE branch | |
if: env.CI_USE_PR == 'false' | |
uses: actions/checkout@main | |
with: | |
ref: ${{ env.MG_BASE_BRANCH }} | |
token: ${{ steps.app-token.outputs.token }} | |
fetch-depth: 1 | |
- name: Checkout BASE branch | |
if: env.CI_USE_PR == 'true' | |
uses: actions/checkout@main | |
with: | |
ref: ${{ env.MG_BASE_BRANCH }} | |
fetch-depth: 1 | |
- name: Fetch HEAD branch | |
run: | | |
git remote set-branches origin $MG_BASE_BRANCH | |
git remote set-branches --add origin $MG_HEAD_BRANCH | |
git fetch | |
git merge-base --is-ancestor $MG_BASE_BRANCH origin/$MG_HEAD_BRANCH || { echo "$MG_HEAD_BRANCH branch MUST rebase to ${MG_BASE_BRANCH}!" >&2; exit 1; } | |
- name: Clone a workspace | |
run: | | |
git clone . ../cloned | |
rm -rf ../cloned/.git | |
cp -a .git ../cloned/ | |
- name: Mkdirs | |
run: | | |
mkdir staging_dir | |
- name: Save Current ipk list before | |
run: | | |
find bin/packages -type f | sort | tee staging_dir/old.lst | |
- name: List Commits | |
run: | | |
git log --reverse --pretty=format:'%H' "origin/${MG_HEAD_BRANCH}...${MG_BASE_BRANCH}" > staging_dir/commits | |
cat staging_dir/commits | |
[ -s staging_dir/commits ] | |
echo >>staging_dir/commits | |
- name: Git user | |
run: | | |
git config user.email "[email protected]" | |
git config user.name "Actions Bot" | |
- name: Purge and Merge | |
run: | | |
while read; do | |
commit="$REPLY" | |
[ -n "$commit" ] || continue | |
echo "pick $commit" | |
git show --pretty= --name-only --no-renames $commit | grep '^bin/.*\.ipk$' > staging_dir/files || true | |
echo >>staging_dir/files | |
echo "ipk changes:" | |
cat staging_dir/files | |
git cherry-pick $commit | |
git -C ../cloned checkout $commit | |
while read; do | |
file="$REPLY" | |
[ -n "$file" ] || continue | |
dir="`dirname $file`" | |
echo "rm $file" | |
rm -f "$file" | |
[ -f "../cloned/$file" ] || continue | |
pkg=`tar -xOf "../cloned/$file" ./control.tar.gz | tar -xOz ./control | grep '^Package: ' | sed 's/^Package: \(.*\)$/\1/'` | |
echo "clean old $pkg" | |
ls "$dir/$pkg"_*.ipk | while read; do | |
del="$REPLY" | |
echo "check $del pkg name is $pkg?" | |
pkg0=`tar -xOf "$del" ./control.tar.gz | tar -xOz ./control | grep '^Package: ' | sed 's/^Package: \(.*\)$/\1/'` | |
[ "$pkg" = "$pkg0" ] || continue | |
echo "rm $del" | |
rm -f "$del" | |
done | |
mkdir -p "$dir" | |
echo "cp new $file" | |
cp -a "../cloned/$file" "$file" | |
done < staging_dir/files | |
git add bin | |
git commit --no-edit --amend | |
done < staging_dir/commits | |
- name: Save Current ipk list after | |
run: | | |
find bin/packages -type f | sort | tee staging_dir/new.lst | |
- name: Report | |
run: diff -au1 staging_dir/old.lst staging_dir/new.lst | tee staging_dir/changes.diff || true | |
- name: Push | |
if: env.CI_USE_PR == 'false' | |
run: | | |
git push origin ${MG_BASE_BRANCH}:${MG_BASE_BRANCH} | |
- name: Push PR | |
if: env.CI_USE_PR == 'true' | |
run: | | |
git push -f origin ${MG_BASE_BRANCH}:${CI_AUTO_BRANCH} | |
- name: create pr | |
if: env.CI_USE_PR == 'true' | |
id: create_pr | |
uses: actions/github-script@v7 | |
with: | |
result-encoding: string | |
script: | | |
const { MG_BASE_BRANCH, CI_AUTO_BRANCH } = process.env | |
const fs = require('fs') | |
const diff = fs.readFileSync('staging_dir/changes.diff', 'utf8') | |
const body = "# Automerging\nThis PR was created by CI\n## Changes\n```diff\n" + diff + "\n```\n" | |
const oldprs = await github.rest.pulls.list({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
state: "open", | |
head: CI_AUTO_BRANCH, | |
base: MG_BASE_BRANCH, | |
per_page: 1 | |
}) | |
if (oldprs.status >= 200 && oldprs.status < 300 && oldprs.data.length > 0) { | |
const pr_num=oldprs.data[0].number | |
console.log("PR already existed, update #", pr_num) | |
await github.rest.pulls.update({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: pr_num, | |
body: body | |
}) | |
return pr_num | |
} else { | |
const pr = await github.rest.pulls.create({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
title: "Auto Merging", | |
head: CI_AUTO_BRANCH, | |
base: MG_BASE_BRANCH, | |
body: body | |
}) | |
if (pr.status >= 200 && pr.status < 300) { | |
console.log("PR created #", pr.data.number) | |
return pr.data.number | |
} | |
} | |
- name: merge pr | |
if: env.CI_USE_PR == 'true' | |
uses: actions/github-script@v7 | |
env: | |
PULL_NUMBER: ${{steps.create_pr.outputs.result}} | |
with: | |
script: | | |
const { PULL_NUMBER } = process.env | |
const mr = await github.rest.pulls.merge({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: PULL_NUMBER, | |
merge_method: "rebase" | |
}) | |
return mr.status >= 200 && mr.status < 300 && mr.data.merged | |