-
Notifications
You must be signed in to change notification settings - Fork 639
121 lines (119 loc) · 5.77 KB
/
pr-preview.yml
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
name: PR Preview
on:
pull_request_target:
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
files: ${{ steps.files.outputs.all }}
steps:
- name: Выборка файлов для формирования списка
id: files
run: |
pr_files=$(echo $(curl \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files))
list=$(echo $pr_files | grep -o '"filename": "[^"]*' | cut -d'"' -f4)
mdReplaced=${list//md/html}
returnReplaced=${list//$"\n"/ }
echo "::set-output name=all::$returnReplaced"
echo $returnReplaced
pr-preview:
runs-on: ubuntu-latest
needs:
- prepare
env:
DEPLOY_DOMAIN: https://content-${{ github.event.pull_request.number }}.dev.doka.guide
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PATH_TO_CONTENT: ./content
steps:
- name: Загрузка platform
uses: actions/checkout@v3
with:
repository: doka-guide/platform
- name: Загрузка контента
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
path: content
- uses: actions/setup-node@v3
with:
node-version: 16
- name: Кэширование модулей
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Получение идентификатора
id: check
run: |
check_suite_url=$(curl -s -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }} | jq -r '.check_suite_url')
check_run_id=$(curl -s -H "Accept: application/vnd.github.v3+json" $check_suite_url/check-runs | jq '.check_runs[] | .id')
echo "::set-output name=check_id::$check_run_id"
- name: Установка модулей
run: npm ci
- name: Сообщение о начале публикации превью
uses: hasura/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: ${{ github.repository }}
number: ${{ github.event.pull_request.number }}
id: preview-${{ github.event.pull_request.number }}
message: "Идёт сборка и публикация превью... [Подробнее](https://github.com/${{ github.repository }}/runs/${{ steps.check.outputs.check_id }}?check_suite_focus=true)"
recreate: true
- name: Форматирование списка файлов
id: links
run: |
file_list=""
for f in ${{ needs.prepare.outputs.files }}; do
if [[ $changed_file =~ ".html" ]]; then
file_list=$(echo -e "${file_list}<li><a href="${{ env.DEPLOY_DOMAIN }}/${f}">${f}</a></li>")
fi
done
if ! [[ $file_list == "" ]]; then
file_list=$(echo -e "<br><p>Список изменённых материалов:<ul>${file_list}</ul><p>")
fi
echo ::set-output name=list::$(echo -e "${file_list}")
- name: Установка ключа для пользователя
run: |
set -eu
mkdir "$HOME/.ssh"
echo "${{ secrets.DEPLOY_KEY }}" > "$HOME/.ssh/doka_deploy"
chmod 600 "$HOME/.ssh/doka_deploy"
- name: Сборка и публикация сайта
id: build-preview
continue-on-error: true
run: |
cp .env.example .env
npm run preview
ssh -i $HOME/.ssh/doka_deploy -o StrictHostKeyChecking=no [email protected] mkdir -p /web/sites/dev.doka.guide/content/${{ github.event.pull_request.number }}
cd dist && rsync -e "ssh -i $HOME/.ssh/doka_deploy -o StrictHostKeyChecking=no" --archive --progress --compress --delete . [email protected]:/web/sites/dev.doka.guide/content/${{ github.event.pull_request.number }}
echo "Ссылка на превью — ${{ env.DEPLOY_DOMAIN }}"
echo -e "${{ steps.links.outputs.list }}"
- name: Сообщение о неудаче публикации превью
uses: hasura/[email protected]
if: failure()
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: ${{ github.repository }}
number: ${{ github.event.pull_request.number }}
id: preview-${{ github.event.pull_request.number }}
message: "Превью контента из ${{ github.event.after }} не опубликовано. Ошибка сборки или публикации. [Подробнее](https://github.com/${{ github.repository }}/runs/${{ steps.check.outputs.check_id }}?check_suite_focus=true)"
fail: true
recreate: true
- name: Сообщение об успехе публикации превью
uses: hasura/[email protected]
if: success()
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: ${{ github.repository }}
number: ${{ github.event.pull_request.number }}
id: preview-${{ github.event.pull_request.number }}
message: '<details><summary><a href="${{ env.DEPLOY_DOMAIN }}">Превью контента</a> из ${{ github.event.after }} опубликовано.</summary>${{ steps.links.outputs.list }}</details>'
recreate: true