-
Notifications
You must be signed in to change notification settings - Fork 3
133 lines (119 loc) · 4.78 KB
/
Copy pathpr-preview-auto.yml
File metadata and controls
133 lines (119 loc) · 4.78 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
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
122
123
124
125
126
127
128
129
130
131
132
133
name: Automatic PR preview APK
on:
workflow_run:
workflows: ["Android build and releases"]
types: [completed]
pull_request:
types: [closed]
permissions:
contents: read
concurrency:
group: pr-preview-${{ github.event.workflow_run.id || github.event.pull_request.number || github.run_id }}
cancel-in-progress: true
jobs:
publish-pr-preview:
if: >-
github.event_name == 'workflow_run' &&
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.pull_requests[0].number != null
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
actions: read
contents: write
pull-requests: read
steps:
- name: Verify the source PR is still open and trusted
id: pr
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
run: |
set -euo pipefail
pr_json="$(gh api "repos/${GH_REPO}/pulls/${PR_NUMBER}")"
state="$(jq -r '.state' <<< "$pr_json")"
head_repo="$(jq -r '.head.repo.full_name' <<< "$pr_json")"
if [[ "$state" != "open" ]]; then
echo "PR #$PR_NUMBER is $state; skipping stale preview upload."
echo "publish=false" >> "$GITHUB_OUTPUT"
exit 0
fi
if [[ "$head_repo" != "$GH_REPO" ]]; then
echo "PR #$PR_NUMBER comes from $head_repo; refusing to publish an untrusted preview."
echo "publish=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "publish=true" >> "$GITHUB_OUTPUT"
echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
- name: Download the APK produced by full PR CI
if: steps.pr.outputs.publish == 'true'
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: DualSub-Replay-preview
path: verified
github-token: ${{ github.token }}
run-id: ${{ github.event.workflow_run.id }}
- name: Package the versioned PR preview APK
if: steps.pr.outputs.publish == 'true'
id: package
env:
PR_NUMBER: ${{ steps.pr.outputs.number }}
BUILD_NUMBER: ${{ github.event.workflow_run.run_number }}
run: |
set -euo pipefail
mkdir -p release
asset_name="DualSub-Replay-PR${PR_NUMBER}-build${BUILD_NUMBER}-preview.apk"
cp verified/DualSub-Replay-preview.apk "release/$asset_name"
sha256sum "release/$asset_name"
echo "asset_name=$asset_name" >> "$GITHUB_OUTPUT"
- name: Upload newest PR preview to rolling preview release
if: steps.pr.outputs.publish == 'true'
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
PR_NUMBER: ${{ steps.pr.outputs.number }}
ASSET_NAME: ${{ steps.package.outputs.asset_name }}
run: |
set -euo pipefail
assets="$(gh release view preview --json assets --jq '.assets[].name' || true)"
while IFS= read -r asset; do
[[ -z "$asset" ]] && continue
if [[ "$asset" =~ ^DualSub-Replay-PR${PR_NUMBER}-(build[0-9]+-)?preview\.apk$ ]] && [[ "$asset" != "$ASSET_NAME" ]]; then
gh release delete-asset preview "$asset" --yes
echo "Removed older PR #$PR_NUMBER preview: $asset"
fi
done <<< "$assets"
gh release upload preview "release/$ASSET_NAME" --clobber
echo "Published $ASSET_NAME from the already-verified CI artifact."
cleanup-pr-preview:
if: >-
github.event_name == 'pull_request' &&
github.event.action == 'closed' &&
github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: write
steps:
- name: Remove closed PR preview APKs
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -euo pipefail
assets="$(gh release view preview --json assets --jq '.assets[].name' || true)"
removed=false
while IFS= read -r asset; do
[[ -z "$asset" ]] && continue
if [[ "$asset" =~ ^DualSub-Replay-PR${PR_NUMBER}-(build[0-9]+-)?preview\.apk$ ]]; then
gh release delete-asset preview "$asset" --yes
echo "Deleted $asset from the preview release."
removed=true
fi
done <<< "$assets"
if [[ "$removed" == "false" ]]; then
echo "PR #$PR_NUMBER preview APK is already absent from the preview release."
fi