Skip to content

Commit 93a01d1

Browse files
committed
Improve error handling
Output errors in GitHub-specific manner May have also included some other lines of code accidentally…
1 parent 093743a commit 93a01d1

File tree

1 file changed

+53
-48
lines changed

1 file changed

+53
-48
lines changed

.github/workflows/deployment.yml

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,22 @@ jobs:
4747
id-token: write
4848

4949
steps:
50-
# Step 1: Validate Freemius Credentials
5150
- name: Validate Freemius Credentials
51+
id: credentials
5252
run: |
5353
if [ -z "${{ secrets.FREEMIUS_DEV_ID }}" ] || \
5454
[ -z "${{ secrets.FREEMIUS_PUBLIC_KEY }}" ] || \
5555
[ -z "${{ secrets.FREEMIUS_SECRET_KEY }}" ] || \
5656
[ -z "${{ secrets.FREEMIUS_PLUGIN_ID }}" ]; then
57-
echo "Error: Missing required Freemius credentials"
57+
echo "::error::Missing required Freemius credentials"
5858
exit 1
5959
fi
6060
61-
# Step 2: Checkout code
6261
- name: Checkout Code
6362
uses: actions/checkout@v4
6463
with:
6564
fetch-depth: 0
6665

67-
# Step 4: Setup PHP
6866
- name: Setup PHP
6967
uses: shivammathur/setup-php@v2
7068
with:
@@ -81,85 +79,87 @@ jobs:
8179
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
8280
restore-keys: ${{ runner.os }}-composer-
8381

84-
# Step 6: Check for Composer Dependencies
8582
- name: Check for Composer
8683
id: check_composer
8784
run: |
8885
if [ -f "composer.json" ]; then
8986
echo "has_composer=true" >> $GITHUB_OUTPUT
9087
fi
9188
92-
# Step 7: Validate composer.json
9389
- name: Validate composer.json
9490
if: steps.check_composer.outputs.has_composer == 'true'
9591
run: composer validate --strict
9692

97-
# Step 8: Install Dependencies
9893
- name: Install Dependencies
9994
if: steps.check_composer.outputs.has_composer == 'true'
10095
run: |
10196
composer install --no-dev --optimize-autoloader
10297
composer dump-autoload --optimize --no-dev
10398
104-
# Step 8: Build the ZIP File
10599
- name: Build Plugin ZIP
100+
id: build
101+
timeout-minutes: 5
106102
run: |
107-
# Create a clean export using git archive
108103
mkdir -p build
109104
git archive HEAD --format=zip -o build/datakit.zip
105+
106+
if [ ! -f build/datakit.zip ]; then
107+
echo "::error::Failed to create ZIP file"
108+
exit 1
109+
fi
110110
111-
# Step 9: Validate ZIP
112111
- name: Validate ZIP
112+
id: validate
113+
timeout-minutes: 2
113114
run: |
114-
if [ ! -f build/datakit.zip ]; then
115-
echo "Error: ZIP file was not created successfully"
115+
echo "Verifying ZIP contents..."
116+
117+
if ! unzip -l build/datakit.zip > zip_contents.txt; then
118+
echo "::error::Failed to read ZIP contents"
116119
exit 1
117120
fi
118121
119-
echo "ZIP contents:"
120-
unzip -l build/datakit.zip
122+
missing_files=()
121123
122-
# Verify key files exist in the ZIP
123-
echo "Verifying key files and directories..."
124+
if ! grep -q "datakit.php" zip_contents.txt; then
125+
missing_files+=("Main plugin file (datakit.php)")
126+
fi
124127
125-
# Check main plugin file
126-
if ! unzip -l build/datakit.zip | grep -q "datakit.php"; then
127-
echo "Error: Main plugin file not found in ZIP"
128-
exit 1
128+
if ! grep -q "^.*src/" zip_contents.txt; then
129+
missing_files+=("src directory")
130+
elif [ $(grep "src/" zip_contents.txt | wc -l) -le 1 ]; then
131+
echo "::warning::src directory appears to be empty"
129132
fi
130133
131-
# Check src directory exists and has contents
132-
if ! unzip -l build/datakit.zip | grep -q "^.*src/"; then
133-
echo "Error: src directory not found in ZIP"
134-
exit 1
134+
if ! grep -q "^.*freemius/" zip_contents.txt; then
135+
missing_files+=("freemius directory")
136+
elif [ $(grep "freemius/" zip_contents.txt | wc -l) -le 1 ]; then
137+
echo "::warning::freemius directory appears to be empty"
135138
fi
136139
137-
# Check src directory has contents (should show more than just the directory entry)
138-
if [ $(unzip -l build/datakit.zip | grep "src/" | wc -l) -le 1 ]; then
139-
echo "Error: src directory is empty"
140+
if [ ${#missing_files[@]} -ne 0 ]; then
141+
echo "::error::Missing required files/directories:"
142+
printf '%s\n' "${missing_files[@]}"
140143
exit 1
141144
fi
142145
143-
# Check Freemius directory exists and has contents
144-
if ! unzip -l build/datakit.zip | grep -q "^.*freemius/"; then
145-
echo "Error: freemius directory not found in ZIP"
146+
echo "ZIP validation successful"
147+
148+
- name: Download Freemius PHP SDK
149+
timeout-minutes: 2
150+
run: |
151+
if ! curl -L https://github.com/Freemius/php-sdk/archive/refs/heads/master.zip -o freemius-sdk.zip; then
152+
echo "::error::Failed to download Freemius SDK"
146153
exit 1
147154
fi
148155
149-
# Check Freemius directory has contents (should show more than just the directory entry)
150-
if [ $(unzip -l build/datakit.zip | grep "freemius/" | wc -l) -le 1 ]; then
151-
echo "Error: Freemius directory is empty"
156+
if ! unzip freemius-sdk.zip; then
157+
echo "::error::Failed to extract Freemius SDK"
152158
exit 1
153159
fi
154160
155-
# Step 10: Download Freemius PHP SDK
156-
- name: Download Freemius PHP SDK
157-
run: |
158-
curl -L https://github.com/Freemius/php-sdk/archive/refs/heads/master.zip -o freemius-sdk.zip
159-
unzip freemius-sdk.zip
160-
161-
# Step 11: Deploy to Freemius
162161
- name: Deploy to Freemius
162+
id: deploy
163163
timeout-minutes: 5
164164
env:
165165
FREEMIUS_DEV_ID: ${{ secrets.FREEMIUS_DEV_ID }}
@@ -178,17 +178,14 @@ jobs:
178178
\$plugin_id = getenv('FREEMIUS_PLUGIN_ID');
179179
180180
try {
181-
// Init SDK
182181
\$api = new Freemius_Api(FS__API_SCOPE, FS__API_DEV_ID, FS__API_PUBLIC_KEY, FS__API_SECRET_KEY);
183182
184-
// Get version from release tag or workflow dispatch input
185183
\$version = trim(getenv('GITHUB_EVENT_NAME') === 'release'
186184
? getenv('GITHUB_REF_NAME')
187185
: getenv('INPUT_VERSION'), 'v');
188186
189187
echo \"Deploying version: \$version\\n\";
190188
191-
// Deploy new version
192189
\$result = \$api->Api(\"plugins/\$plugin_id/tags.json\", 'POST',
193190
array(
194191
'add_contributor' => true,
@@ -203,19 +200,20 @@ jobs:
203200
throw new Exception('Freemius API Error: ' . \$result->error->message);
204201
}
205202
206-
# Generate download URL for GitHub release
207203
\$free_version_download_url = \$api->GetSignedUrl(\"/plugins/{\$result->plugin_id}/tags/{\$result->id}.zip?is_premium=false\");
208204
209205
echo \"Successfully deployed version \$version to Freemius\\n\";
210206
echo \"Download URL: \$free_version_download_url\\n\";
211207
212-
file_put_contents('download_url.txt', \$free_version_download_url);
208+
if (!file_put_contents('download_url.txt', \$free_version_download_url)) {
209+
throw new Exception('Failed to save download URL');
210+
}
213211
214212
echo 'Deployment details:\\n';
215213
print_r(\$result);
216214
217215
} catch (Exception \$e) {
218-
echo \"Error: \" . \$e->getMessage() . \"\\n\";
216+
echo \"::error::{$e->getMessage()}\\n\";
219217
exit(1);
220218
}
221219
"
@@ -224,15 +222,22 @@ jobs:
224222
- name: Create Release Comment
225223
if: success() && github.event_name == 'release'
226224
uses: actions/github-script@v6
225+
timeout-minutes: 1
227226
with:
228227
github-token: ${{ secrets.GITHUB_TOKEN }}
229228
script: |
230229
const fs = require('fs');
231230
const { owner, repo } = context.repo;
232231
const release_id = context.payload.release.id;
233232
234-
// Read the download URL from the file
235-
const downloadUrl = fs.readFileSync('download_url.txt', 'utf8').trim();
233+
let downloadUrl;
234+
try {
235+
downloadUrl = fs.readFileSync('download_url.txt', 'utf8').trim();
236+
if (!downloadUrl) throw new Error('Empty download URL');
237+
} catch (error) {
238+
console.error('Failed to read download URL:', error);
239+
downloadUrl = 'Download URL not available';
240+
}
236241
237242
await github.rest.repos.updateRelease({
238243
owner,

0 commit comments

Comments
 (0)