-
Notifications
You must be signed in to change notification settings - Fork 0
304 lines (250 loc) · 10.9 KB
/
fork-clone-build-deploy.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
name: Fork - Clone - Build flutter App - Deploy to gh-pages
run-name: ${{ github.event.inputs.owner }}/${{ github.event.inputs.repo }}
on:
workflow_dispatch:
inputs:
owner:
description: "Repository Owner"
required: true
repo:
description: "Repository"
required: true
fork-org:
description: "Fork organization"
required: true
run-id:
description: "Run identifier"
required: true
jobs:
webfolder:
runs-on: ubuntu-latest
env:
new-repo-name: ${{ github.event.inputs.owner }}-${{ github.event.inputs.repo }}
delay-miliseconds-to-validate-fork-finished: 5000
timeout-minutes: 7
steps:
- name: ${{ github.event.inputs.run-id }}
run: |
echo "run identifier ${{ github.event.inputs.run-id }}"
echo "new-repo-name ${{ env.new-repo-name }}"
- name: Fork repository
uses: actions/github-script@v4
with:
github-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
script: |
const owner = '${{ github.event.inputs.owner }}';
const repo = '${{ github.event.inputs.repo }}';
const org = '${{ github.event.inputs.fork-org }}';
const name = '${{ env.new-repo-name }}';
async function forkRepo() {
try {
const response = await github.repos.createFork({
owner: owner,
repo: repo,
organization: org,
name: name
});
console.log(`Repository ${response.data.full_name} forked to organization ${org}`);
} catch (error) {
console.error('Failed to fork repository:', error);
if(error.status === 404){
console.error('Repository not found. Stop workflow');
throw error
}
}
}
forkRepo();
- name: Wait until the fork finishes
uses: actions/github-script@v4
with:
github-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
script: |
const owner = '${{ github.event.inputs.fork-org }}';
const repo = '${{ env.new-repo-name }}';
const delayTime = ${{ env.delay-miliseconds-to-validate-fork-finished }};
async function repoExists() {
try {
const response = await github.repos.get({
owner: owner,
repo: repo
});
console.log(`Repository ${response.data.full_name} was found.`);
} catch (error) {
console.error('Failed to get repository', error);
throw error;
}
}
async function repoHasCommits() {
try {
const response = await github.repos.listCommits({
owner: owner,
repo: repo
});
if(response.status !== 200){
console.log(`Didn't find commits for the repository ${repo}. Response ${response}`);
return false;
}
console.log(`Repository ${repo} has commits.`);
return true;
} catch (error) {
console.error(`Failed to list commits of repository ${repo}`, error);
return false;
}
}
function delay(time) {
console.log(`Delay for ${time} miliseconds`)
return new Promise(resolve => setTimeout(resolve, time));
}
repoExists();
let hasCommits = await repoHasCommits();
while(!hasCommits){
await delay(delayTime)
hasCommits = await repoHasCommits();
}
- name: Clone or Checkout repository
uses: actions/checkout@v2
with:
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
repository: ${{ github.event.inputs.fork-org }}/${{ env.new-repo-name }}
- name: Find Flutter project path
id: flutter-project-project-path
run: |
# Find the directory containing the file with the string 'project_type: app'
METADATA_DIR=$(dirname $(grep -Eril "project_type: app" *) | sort -u)
if [ ! -z "$METADATA_DIR" ]; then
echo "Found .metadata file under '$METADATA_DIR' folder"
echo "FLUTTER_PROJECT_PATH=$METADATA_DIR" >> "$GITHUB_OUTPUT"
else
# Find the directory containing the file 'pubspec.lock'
PUBSPEC_LOCK_DIR=$(find . -name "pubspec.lock" -exec dirname {} \;)
if [ ! -z "$PUBSPEC_LOCK_DIR" ]; then
echo "pubspec.lock file was found."
while IFS= read -r dir
do
if [[ -d "$dir/web" || -d "$dir/android" || -d "$dir/ios" || -d "$dir/linux" || -d "$dir/windows" || -d "$dir/macos" ]]; then
echo "Found pubspec.lock in the same folder as the other important flutter folders. This is under '$dir'"
echo "FLUTTER_PROJECT_PATH=$dir" >> "$GITHUB_OUTPUT"
exit 0
else
shift
fi
done <<< "$PUBSPEC_LOCK_DIR"
echo "None of the pubspec.lock files was found in the same folder as the other important flutter folders."
fi
# Find the first directory containing the file 'pubspec.yaml'
PUBSPEC_DIR=$(dirname $(find . -name "pubspec.yaml" | head -n 1))
if [ -z "$PUBSPEC_DIR" ]; then
echo "pubspec.yaml file was not found. Stop workflow."
exit 1
else
echo "pubspec.yaml file was found under '$PUBSPEC_DIR'."
echo "FLUTTER_PROJECT_PATH=$PUBSPEC_DIR" >> "$GITHUB_OUTPUT"
fi
fi
- name: Find supported Flutter version
id: flutter-version-finder
uses: ukoreh/actions/flutter-version-finder@master
with:
project-path: ${{ steps.flutter-project-project-path.outputs.FLUTTER_PROJECT_PATH }}
- name: Set up Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: ${{ steps.flutter-version-finder.outputs.flutter-version }}
channel: ${{ steps.flutter-version-finder.outputs.flutter-channel }}
- name: Setup Flutter web
run: |
channel=${{ steps.flutter-version-finder.outputs.flutter-channel }}
echo "on channel: $channel"
if [ "$channel" = "beta" ]; then
flutter config --enable-web
fi
- name: Check if 'web' folder exists
id: web-folder
run: |
cd ${{ steps.flutter-project-project-path.outputs.FLUTTER_PROJECT_PATH }}
echo "path now: ${{ steps.flutter-project-project-path.outputs.FLUTTER_PROJECT_PATH }}"
if [ -d "web" ]; then
echo "'web' folder exists in the same directory as 'pubspec.yaml'."
echo "WEB_FOLDER_LOCATION=$PWD" >> "$GITHUB_OUTPUT"
else
echo "'web' folder does not exist in the same directory as 'pubspec.yaml'. So create it!!!"
flutter create --org com.ukoreh.actions --platforms web .
echo "WEB_FOLDER_LOCATION=$PWD" >> "$GITHUB_OUTPUT"
fi
- name: Clean flutter
run: |
cd ${{ steps.web-folder.outputs.WEB_FOLDER_LOCATION }}
flutter clean
- name: Get packages
run: |
cd ${{ steps.web-folder.outputs.WEB_FOLDER_LOCATION }}
flutter pub get
- name: Build web app
run: |
cd ${{ steps.web-folder.outputs.WEB_FOLDER_LOCATION }}
flutter_version=${{ steps.flutter-version-finder.outputs.flutter-version }}
base_href="/${{ env.new-repo-name }}/"
if [[ "$flutter_version" =~ [1|2]\.[0-4]{1,}.+ ]]; then
flutter build web --release
sed -i "s|<base href=.*>|<!-- empty -> g" build/web/index.html
sed -i "s|<head>|<head><base href=\"$base_href\">|g" build/web/index.html
else
flutter build web --release --base-href $base_href
fi
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
personal_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
publish_dir: ${{ steps.web-folder.outputs.WEB_FOLDER_LOCATION }}/build/web
external_repository: ${{ github.event.inputs.fork-org }}/${{ env.new-repo-name }}
publish_branch: gh-pages
- name: Create issue in the forked repository
if: ${{ vars.FF_STEP_CREATE_ISSUE == 'true' }}
uses: actions/github-script@v4
with:
github-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
script: |
const owner = '${{ github.event.inputs.owner }}';
const repo = '${{ github.event.inputs.repo }}';
const org = '${{ github.event.inputs.fork-org }}';
const name = '${{ env.new-repo-name }}';
const title = "Your web app is ready! 🚀";
const body = `Hey! 👋
Someone has requested a demo of your project using [Ukoreh](https://ukoreh.fun/)! 🧙♂️.
The app compiled fine and is now live at https://${org}.github.io/${name}`;
async function createIssue() {
try {
const newIssue = await github.issues.create({
owner: owner,
repo: repo,
title: title,
body: body,
});
console.log(`Created issue ${newIssue.data.number}: ${newIssue.data.title}`);
} catch (error) {
console.error('Failed to create issue:', error);
if(error.status === 410){
console.error('Issues are disabled in the repository');
}
}
}
async function issueExists() {
try {
const { data: issues } = await github.issues.listForRepo({
owner: owner,
repo: repo,
state: 'all'
});
const existingIssue = issues.find(issue => issue.title === title);
const exists = existingIssue !== undefined;
console.log(`Issue with title "${title}" exists: ${exists}`);
return exists;
} catch (error) {
console.error('Failed to validate if issue already exists:', error);
}
}
const exists = await issueExists();
if (!exists) {
console.log(`Creating issue with title ${title}`);
createIssue();
}