Skip to content

Commit 07d1c38

Browse files
committed
Add release asset upload to GH Actions
1 parent d7310eb commit 07d1c38

File tree

6 files changed

+245
-1
lines changed

6 files changed

+245
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "mautic/upload-release-asset",
3+
"description": "Updates the release asset (x.x.x.zip) to the m.mautic.org instance",
4+
"type": "project",
5+
"require": {
6+
"mautic/api-library": "^2.16"
7+
}
8+
}

.github/workflows/mautic-asset-upload/composer.lock

+103
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Mautic\Auth\ApiAuth;
6+
use Mautic\MauticApi;
7+
8+
require __DIR__.'/vendor/autoload.php';
9+
10+
if ('cli' !== php_sapi_name()) {
11+
die('This script can only run on the command line');
12+
}
13+
14+
$vars = [
15+
1 => 'instanceUrl',
16+
2 => 'username',
17+
3 => 'password',
18+
4 => 'mauticVersion',
19+
5 => 'assetCategoryId',
20+
6 => 'fileToUpload',
21+
];
22+
23+
foreach ($vars as $id => $var) {
24+
if (empty($_SERVER['argv'][$id])) {
25+
echo "Argument ${id} (${var}) is missing. Run this script as \"php upload.php ".implode(' ', $vars)."\"\n";
26+
exit(1);
27+
}
28+
29+
$$var = $_SERVER['argv'][$id];
30+
}
31+
32+
// Set up the authentication
33+
$settings = [
34+
'userName' => $username,
35+
'password' => $password,
36+
];
37+
38+
// Initiate the auth object specifying to use BasicAuth
39+
$initAuth = new ApiAuth();
40+
$auth = $initAuth->newAuth($settings, 'BasicAuth');
41+
$api = new MauticApi();
42+
43+
/** @var \Mautic\Api\Files */
44+
$filesApi = $api->newApi('files', $auth, $instanceUrl);
45+
46+
/** @var \Mautic\Api\Assets */
47+
$assetApi = $api->newApi('assets', $auth, $instanceUrl);
48+
49+
/**
50+
* Upload the file.
51+
*/
52+
$filesApi->setFolder('assets');
53+
// File should be an absolute path!
54+
$fileRequest = [
55+
'file' => $fileToUpload,
56+
];
57+
58+
$response = $filesApi->create($fileRequest);
59+
60+
if (isset($response['error'])) {
61+
echo $response['error']['code'].': '.$response['error']['message']."\n";
62+
exit(1);
63+
}
64+
65+
if (!isset($response['file']) || !isset($response['file']['name'])) {
66+
echo 'An unknown error occurred while uploading the release asset to our Mautic instance. '
67+
."Please try again or debug locally (we don't provide logs in CI for security reasons)\n";
68+
exit(1);
69+
}
70+
71+
/**
72+
* Create the actual asset based on the file we just uploaded.
73+
*/
74+
$data = [
75+
'title' => "Mautic ${mauticVersion}",
76+
'storageLocation' => 'local',
77+
'file' => $response['file']['name'],
78+
'category' => $assetCategoryId,
79+
'isPublished' => false,
80+
];
81+
82+
$response = $assetApi->create($data);
83+
84+
if (isset($response['error'])) {
85+
echo $response['error']['code'].': '.$response['error']['message']."\n";
86+
exit(1);
87+
}
88+
89+
if (!isset($response['asset']['id']) || !isset($response['asset']['downloadUrl'])) {
90+
echo "Unknown error occurred while creating asset. Please debug locally.\n";
91+
exit(1);
92+
}
93+
94+
echo 'Successfully created asset with ID '.$response['asset']['id']." 🚀\n";

.github/workflows/release.yml

+37
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,40 @@ jobs:
209209
with:
210210
name: update-logs
211211
path: ./mautic-testing/var/logs/*
212+
213+
upload-release-asset:
214+
name: Upload release asset to m.mautic.org
215+
needs: [release, test-fresh-install, test-update-install]
216+
runs-on: ubuntu-latest
217+
218+
steps:
219+
- uses: actions/checkout@v2
220+
221+
- name: Get tag name
222+
run: echo "MAUTIC_VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
223+
224+
- name: Setup PHP, with composer and extensions
225+
uses: shivammathur/setup-php@v2
226+
with:
227+
php-version: 7.4
228+
extensions: mbstring, xml, ctype, iconv, intl, pdo_sqlite, mysql, pdo_mysql
229+
230+
- name: Install dependencies
231+
working-directory: ./.github/workflows/mautic-asset-upload
232+
run: composer install --prefer-dist --no-progress
233+
234+
- name: Download full installation package from previous step
235+
uses: actions/download-artifact@v2
236+
with:
237+
name: ${{ env.MAUTIC_VERSION }}.zip
238+
239+
# Category ID is 2 for Mautic release assets on m.mautic.org, that's why you're seeing "2" in the command below
240+
- name: Upload release asset ZIP to m.mautic.org
241+
run: |
242+
php ./.github/workflows/mautic-asset-upload/upload.php \
243+
https://m.mautic.org \
244+
"${{ secrets.MAUTIC_INSTANCE_USER }}" \
245+
"${{ secrets.MAUTIC_INSTANCE_PASSWORD }}" \
246+
"${{ env.MAUTIC_VERSION }}" \
247+
2 \
248+
"${{ github.workspace }}/${{ env.MAUTIC_VERSION }}.zip"

.php_cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ $finder = PhpCsFixer\Finder::create()
55
->in(__DIR__.'/app/config')
66
->in(__DIR__.'/app/middlewares')
77
->in(__DIR__.'/app/migrations')
8-
->in(__DIR__.'/plugins');
8+
->in(__DIR__.'/plugins')
9+
->in(__DIR__.'/.github/workflows/mautic-asset-upload');
910

1011
return PhpCsFixer\Config::create()
1112
->setRules([

0 commit comments

Comments
 (0)