-
Notifications
You must be signed in to change notification settings - Fork 0
88 lines (77 loc) · 3.18 KB
/
publish-web-site.yaml
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
88
name: Publish to GitHub Pages
on:
push:
branches:
- main
paths:
- ".github/workflows/publish-web-site.yaml"
- "static/**"
- index.html
- robots.txt
- CNAME
pull_request:
branches:
- "main"
paths:
- ".github/workflows/publish-web-site.yaml"
- "static/**"
- index.html
- robots.txt
- CNAME
workflow_dispatch:
jobs:
publish-web-site:
runs-on: ubuntu-latest
steps:
- name: Checkout code
run: |
# Setup repository
git init .
git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
# Fetch and checkout code
BRANCH_NAME="branch";
case "$GITHUB_REF" in
refs/heads/*)
BRANCH_NAME="${GITHUB_REF#refs/heads/}";;
pull/*/head)
BRANCH_NAME="${GITHUB_REF#pull/}";
BRANCH_NAME="${GITHUB_REF%/head}";
BRANCH_NAME="pull-request-${BRANCH_NAME}";
esac
git fetch origin "${GITHUB_REF}:${BRANCH_NAME}" --depth 1;
git switch "$BRANCH_NAME";
# Switch to gh-pages if on main
if [ "$BRANCH_NAME" = "main" ]; then
# Try to fetch gh-pages branch
if ! git fetch origin gh-pages; then
# If the branch doesn't exist, then create an orphan branch
echo "Could not find gh-pages branch, creating as orphan"
git switch --orphan gh-pages
else
# Else, switch to the branch
echo "Switching to gh-pages branch"
git switch gh-pages
fi
# Delete all old files
rm -rf ./*
# Get latest source
git restore -s "$BRANCH_NAME" static index.html robots.txt CNAME
fi
- name: Deploy to GitHub Pages
run: |
# Skip deploy if not on main
BRANCH_NAME="${GITHUB_REF#refs/heads/}"
if [ "${BRANCH_NAME}" != "main" ]; then
echo "Skipping deployment since branch is '${BRANCH_NAME}', not main"
exit 0
fi
# Add modified files
git add .
# Set user
git config user.name "Auto deploy GitHub Pages"
git config user.email "cd-deploy-gh-pages@bot"
# Commit changes
git commit -m "Deploy to pages: $(date +'%Y-%m-%d %T')"
# Remove 'https://' from beginning of REPOSITORY_URL
REPOSITORY_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
git push "https://X-Access-Token:${{ secrets.GITHUB_TOKEN }}@${REPOSITORY_URL:8}" gh-pages