-
Notifications
You must be signed in to change notification settings - Fork 0
87 lines (79 loc) · 2.99 KB
/
Copy pathci.yml
File metadata and controls
87 lines (79 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
types: [opened, synchronize, reopened, closed]
workflow_dispatch:
concurrency:
group: ci-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
env:
GIT_AUTHOR_NAME: github-actions[bot]
GIT_COMMITTER_NAME: github-actions[bot]
GIT_AUTHOR_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com
GIT_COMMITTER_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com
REMOTE: https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
jobs:
build:
if: github.event.action != 'closed'
permissions:
contents: read
uses: ./.github/workflows/build.yml
# Publish `main` to `/`, PRs to `pr/<number>/<sha>/`.
publish:
needs: build
if: >-
!cancelled()
&& (needs.build.result == 'success' || needs.build.result == 'skipped')
&& (github.event_name != 'pull_request'
|| github.event.pull_request.head.repo.full_name == github.repository)
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- if: github.event.action != 'closed'
uses: actions/download-artifact@v8
with:
name: dist
path: dist
- name: Publish to gh-pages
env:
EVENT: ${{ github.event_name }}
ACTION: ${{ github.event.action }}
PR: ${{ github.event.pull_request.number }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
set -euo pipefail
for attempt in $(seq 1 10); do
rm -rf pages
found=0
git ls-remote --exit-code --heads "$REMOTE" gh-pages >/dev/null || found=$?
case $found in
0) git clone --branch gh-pages --single-branch --depth 1 "$REMOTE" pages ;;
2) git init -q pages ;;
*) echo "cannot reach $REMOTE (git ls-remote exited $found)" >&2; exit 1 ;;
esac
base=$(git -C pages rev-parse --verify -q HEAD || true)
cd pages
if [ "$ACTION" = closed ]; then
[ -d "pr/$PR" ] || exit 0
rm -rf "pr/$PR"; msg="remove pr/$PR"
elif [ "$EVENT" != pull_request ]; then
find . -maxdepth 1 -mindepth 1 ! -name pr ! -name .git ! -name .nojekyll ! -name CNAME -exec rm -rf {} +
cp -a ../dist/. ./; msg="deploy main"
else
dir="pr/$PR/${HEAD_SHA::7}"
rm -rf "pr/$PR"; mkdir -p "$dir" && cp -a ../dist/. "$dir/"; msg="deploy $dir"
fi
touch .nojekyll
git checkout -q --orphan latest
git add -A && git commit -q -m "$msg"
if git push -q --force-with-lease="gh-pages:$base" "$REMOTE" latest:gh-pages; then exit 0; fi
cd ..
echo "gh-pages moved under us, retrying ($attempt)" >&2
sleep $((attempt * 5))
done
echo "could not publish after $attempt attempts" >&2
exit 1