From 13d9516b62b46520f709b3d74091bac228a315c3 Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Mon, 20 Nov 2023 21:42:53 -0600 Subject: [PATCH 01/11] Add gcs driver --- .gitignore | 1 + composer.json | 1 + composer.lock | 136 ++++++++++++++++++++++++++++++++++++++++- config/filesystems.php | 13 ++++ 4 files changed, 150 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 433a5ff0..f2683080 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ npm-debug.log yarn-error.log .env.testing /.vscode +gcloud.json diff --git a/composer.json b/composer.json index a150cc79..37de9379 100644 --- a/composer.json +++ b/composer.json @@ -53,6 +53,7 @@ "league/commonmark": "^2.2", "livewire/livewire": "^2.11", "spatie/icalendar-generator": "^2.3", + "spatie/laravel-google-cloud-storage": "^2.2", "spatie/laravel-ray": "^1.32" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 40f2ab4f..a5605f00 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "aa7c7b949ca55fae4986dcc9e6d21197", + "content-hash": "e5aa5b51b6395542122614992f124344", "packages": [ { "name": "akaunting/laravel-money", @@ -4641,6 +4641,65 @@ ], "time": "2023-05-04T09:04:26+00:00" }, + { + "name": "league/flysystem-google-cloud-storage", + "version": "3.21.0", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/flysystem-google-cloud-storage.git", + "reference": "de4d9e68433ca17b1dc3e28e3800148531ef85a0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/flysystem-google-cloud-storage/zipball/de4d9e68433ca17b1dc3e28e3800148531ef85a0", + "reference": "de4d9e68433ca17b1dc3e28e3800148531ef85a0", + "shasum": "" + }, + "require": { + "google/cloud-storage": "^1.23", + "league/flysystem": "^3.10.0", + "league/mime-type-detection": "^1.0.0", + "php": "^8.0.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "League\\Flysystem\\GoogleCloudStorage\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Frank de Jonge", + "email": "info@frankdejonge.nl" + } + ], + "description": "Google Cloud Storage adapter for Flysystem.", + "keywords": [ + "Flysystem", + "filesystem", + "gcs", + "google cloud storage" + ], + "support": { + "issues": "https://github.com/thephpleague/flysystem-google-cloud-storage/issues", + "source": "https://github.com/thephpleague/flysystem-google-cloud-storage/tree/3.21.0" + }, + "funding": [ + { + "url": "https://ecologi.com/frankdejonge", + "type": "custom" + }, + { + "url": "https://github.com/frankdejonge", + "type": "github" + } + ], + "time": "2023-11-14T11:54:45+00:00" + }, { "name": "league/flysystem-local", "version": "3.15.0", @@ -7240,6 +7299,81 @@ ], "time": "2022-07-05T09:31:00+00:00" }, + { + "name": "spatie/laravel-google-cloud-storage", + "version": "2.2.4", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-google-cloud-storage.git", + "reference": "af43998f668bdc42a76c02d6f64f9c66d030786b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-google-cloud-storage/zipball/af43998f668bdc42a76c02d6f64f9c66d030786b", + "reference": "af43998f668bdc42a76c02d6f64f9c66d030786b", + "shasum": "" + }, + "require": { + "illuminate/contracts": "^9.0|^10.0", + "illuminate/filesystem": "^9.0|^10.0", + "illuminate/support": "^9.0|^10.0", + "league/flysystem-google-cloud-storage": "^3.0.15", + "php": "^8.0" + }, + "require-dev": { + "brianium/paratest": "^6.4", + "nunomaduro/collision": "^6.1|^7.0", + "orchestra/testbench": "^7.0|^8.0", + "phpunit/phpunit": "^9.6|^10.0", + "spatie/laravel-ray": "^1.29", + "vimeo/psalm": "^4.18|^5.9" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\GoogleCloudStorage\\GoogleCloudStorageServiceProvider" + ], + "aliases": { + "GoogleCloudStorage": "Spatie\\GoogleCloudStorage\\GoogleCloudStorageFacade" + } + } + }, + "autoload": { + "psr-4": { + "Spatie\\GoogleCloudStorage\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Alex Vanderbist", + "email": "alex@spatie.be", + "role": "Developer" + } + ], + "description": "Google Cloud Storage filesystem driver for Laravel", + "homepage": "https://github.com/spatie/laravel-google-cloud-storage", + "keywords": [ + "laravel", + "laravel-google-cloud-storage", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-google-cloud-storage/issues", + "source": "https://github.com/spatie/laravel-google-cloud-storage/tree/2.2.4" + }, + "funding": [ + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2023-11-16T14:25:44+00:00" + }, { "name": "spatie/laravel-package-tools", "version": "1.16.1", diff --git a/config/filesystems.php b/config/filesystems.php index 4afc1fc6..5f5a105d 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -56,6 +56,19 @@ 'throw' => false, ], + 'gcs' => [ + 'driver' => 'gcs', + 'key_file_path' => env('GOOGLE_CLOUD_KEY_FILE', null), + 'project_id' => env('GOOGLE_CLOUD_PROJECT_ID'), + 'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET'), + 'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', ''), + 'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), + 'api_endpoint' => env('GOOGLE_CLOUD_STORAGE_API_ENDPOINT', null), + 'visibility' => 'private', + 'visibility_handler' => \League\Flysystem\GoogleCloudStorage\UniformBucketLevelAccessVisibility::class, + 'metadata' => ['cacheControl' => 'public,max-age=86400'], + ], + ], /* From 47f82237df93ffca43d590aa545a7ccc70fee712 Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Tue, 21 Nov 2023 20:23:00 -0600 Subject: [PATCH 02/11] Adjust profile photos for GCS --- .env.example | 2 ++ app/Models/User.php | 16 +++++++++++++++- config/jetstream.php | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index a10ea7b6..1885d89c 100644 --- a/.env.example +++ b/.env.example @@ -59,3 +59,5 @@ TELESCOPE_EMAIL= DEMO_MODE=false DEMO_REPO_URL="https://github.com/JHWelch/ChoreManager" + +PROFILE_FILESYSTEM_DISK=public diff --git a/app/Models/User.php b/app/Models/User.php index 114eaca2..415622c9 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -12,6 +12,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Storage; use Laravel\Fortify\TwoFactorAuthenticatable; use Laravel\Jetstream\HasProfilePhoto; use Laravel\Jetstream\HasTeams; @@ -113,12 +114,25 @@ public function canAccessFilament(): bool protected static function booted(): void { - static::addGlobalScope(new OrderByNameScope); + static::addGlobalScope(new OrderByNameScope()); static::created(function ($user) { UserSetting::create(['user_id' => $user->id]); }); } + /** + * Get the URL to the user's profile photo. + * + * @return string + */ + public function getProfilePhotoUrlAttribute() + { + return $this->profile_photo_path + ? Storage::disk($this->profilePhotoDisk()) + ->temporaryUrl($this->profile_photo_path, now()->addMinutes(30)) + : $this->defaultProfilePhotoUrl(); + } + /** * Get all users with a specific setting. * diff --git a/config/jetstream.php b/config/jetstream.php index 3fece93c..72497984 100644 --- a/config/jetstream.php +++ b/config/jetstream.php @@ -17,6 +17,8 @@ 'stack' => 'livewire', + 'profile_photo_disk' => env('PROFILE_FILESYSTEM_DISK', 'public'), + /* |-------------------------------------------------------------------------- | Jetstream Route Middleware From 84fa1002d75345afa42dffea259e6fe80468b463 Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Wed, 22 Nov 2023 11:27:09 -0600 Subject: [PATCH 03/11] only use gcs if gcs enabled --- app/Models/User.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Models/User.php b/app/Models/User.php index 415622c9..e89d96b8 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -127,6 +127,10 @@ protected static function booted(): void */ public function getProfilePhotoUrlAttribute() { + if (config('jetstream.profile_photo_disk') !== 'gcs') { + return parent::getProfilePhotoUrlAttribute(); + } + return $this->profile_photo_path ? Storage::disk($this->profilePhotoDisk()) ->temporaryUrl($this->profile_photo_path, now()->addMinutes(30)) From b9e82c426452e006fd4ac900d785bad5fba0bf56 Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Wed, 22 Nov 2023 11:30:03 -0600 Subject: [PATCH 04/11] Add initial deploy --- .github/workflows/dploy.yml | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 .github/workflows/dploy.yml diff --git a/.github/workflows/dploy.yml b/.github/workflows/dploy.yml new file mode 100644 index 00000000..c010bef4 --- /dev/null +++ b/.github/workflows/dploy.yml @@ -0,0 +1,45 @@ +name: Deploy to Production + +on: + push: + branches: + - main + +concurrency: + group: deploy-production + cancel-in-progress: true + +jobs: + deploy: + name: Deploy to Production + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - id: auth + name: 'Authenticate to Google Cloud' + uses: google-github-actions/auth@v1 + with: + workload_identity_provider: ${{ secrets.GOOGLE_CLOUD_WORKLOAD_IDENTITY_PROVIDER }} + service_account: ${{ secrets.GOOGLE_CLOUD_SERVICE_ACCOUNT }} + + - name: Build Docker image + run: | + docker build --platform linux/amd64 \ + -t ${{ secrets.ARTIFACT_REPOSITORY }}/chore-manager:${{ github.sha }} + --file=php.dockerfile . + + - name: Push Docker image + run: docker push ${{ secrets.ARTIFACT_REPOSITORY }}/chore-manager:${{ github.sha }} + + - name: Deploy to Cloud Run + run: | + gcloud run deploy chore-manager \ + --image ${{ secrets.ARTIFACT_REPOSITORY }}/chore-manager:${{ github.sha }} \ + --region ${{ vars.GOOGLE_CLOUD_REGION }} \ + --project ${{ secrets.GOOGLE_CLOUD_PROJECT_ID }} From 15d87d286780c24a6bd2b5af84a2b63142588fe4 Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Wed, 22 Nov 2023 11:32:33 -0600 Subject: [PATCH 05/11] production --- .github/workflows/dploy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/dploy.yml b/.github/workflows/dploy.yml index c010bef4..359f6a1e 100644 --- a/.github/workflows/dploy.yml +++ b/.github/workflows/dploy.yml @@ -11,6 +11,7 @@ concurrency: jobs: deploy: + environment: Production name: Deploy to Production runs-on: ubuntu-latest permissions: From f3fa19cc0e63c84f843f84b2eab11191e423495c Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Wed, 22 Nov 2023 11:35:13 -0600 Subject: [PATCH 06/11] add missing `\` --- .github/workflows/dploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dploy.yml b/.github/workflows/dploy.yml index 359f6a1e..5d979497 100644 --- a/.github/workflows/dploy.yml +++ b/.github/workflows/dploy.yml @@ -32,7 +32,7 @@ jobs: - name: Build Docker image run: | docker build --platform linux/amd64 \ - -t ${{ secrets.ARTIFACT_REPOSITORY }}/chore-manager:${{ github.sha }} + -t ${{ secrets.ARTIFACT_REPOSITORY }}/chore-manager:${{ github.sha }} \ --file=php.dockerfile . - name: Push Docker image From dfccc99fd40a5ef43350f6c510347b3cf7c7769b Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Wed, 22 Nov 2023 11:49:02 -0600 Subject: [PATCH 07/11] WIP --- .github/workflows/dploy.yml | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/dploy.yml b/.github/workflows/dploy.yml index 5d979497..ab429e6c 100644 --- a/.github/workflows/dploy.yml +++ b/.github/workflows/dploy.yml @@ -1,9 +1,8 @@ name: Deploy to Production on: - push: - branches: - - main + pull_request: + types: [opened, synchronize, reopened] concurrency: group: deploy-production @@ -29,6 +28,9 @@ jobs: workload_identity_provider: ${{ secrets.GOOGLE_CLOUD_WORKLOAD_IDENTITY_PROVIDER }} service_account: ${{ secrets.GOOGLE_CLOUD_SERVICE_ACCOUNT }} + - name: Place .env + run: echo ${{ secrets.ENV }} | base64 -d > .env + - name: Build Docker image run: | docker build --platform linux/amd64 \ @@ -36,7 +38,9 @@ jobs: --file=php.dockerfile . - name: Push Docker image - run: docker push ${{ secrets.ARTIFACT_REPOSITORY }}/chore-manager:${{ github.sha }} + run: | + gcloud auth configure-docker ${{ vars.GOOGLE_CLOUD_REGION }}-docker.pkg.dev + docker push ${{ secrets.ARTIFACT_REPOSITORY }}/chore-manager:${{ github.sha }} - name: Deploy to Cloud Run run: | From f14a319f4a3500ac136e082d690439fe1066a159 Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Wed, 22 Nov 2023 16:55:55 -0600 Subject: [PATCH 08/11] WIP --- .github/workflows/dploy.yml | 4 ++-- .phpactor.json | 4 ++++ app/Models/User.php | 6 ++++-- 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 .phpactor.json diff --git a/.github/workflows/dploy.yml b/.github/workflows/dploy.yml index ab429e6c..592b1ccc 100644 --- a/.github/workflows/dploy.yml +++ b/.github/workflows/dploy.yml @@ -34,8 +34,8 @@ jobs: - name: Build Docker image run: | docker build --platform linux/amd64 \ - -t ${{ secrets.ARTIFACT_REPOSITORY }}/chore-manager:${{ github.sha }} \ - --file=php.dockerfile . + -t ${{ vars.GOOGLE_CLOUD_REGION }}-docker.pkg.dev/${{ secrets.GOOGLE_CLOUD_PROJECT_ID }}/chore-manager/chore-manager:${{ github.sha }} \ + --file=php.dockerfile . - name: Push Docker image run: | diff --git a/.phpactor.json b/.phpactor.json new file mode 100644 index 00000000..969af7a8 --- /dev/null +++ b/.phpactor.json @@ -0,0 +1,4 @@ +{ + "$schema": "/Applications/Tinkerwell.app/Contents/Resources/phpactor/phpactor.schema.json", + "language_server_phpstan.enabled": false +} \ No newline at end of file diff --git a/app/Models/User.php b/app/Models/User.php index e89d96b8..e4303cfb 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -78,7 +78,9 @@ class User extends Authenticatable implements FilamentUser use HasApiTokens; use HasChoreStreaks; use HasFactory; - use HasProfilePhoto; + use HasProfilePhoto { + getProfilePhotoUrlAttribute as parentGetProfilePhotoUrlAttribute; + } use HasTeams; use HasUnfinishedChoreScopes; use Notifiable; @@ -128,7 +130,7 @@ protected static function booted(): void public function getProfilePhotoUrlAttribute() { if (config('jetstream.profile_photo_disk') !== 'gcs') { - return parent::getProfilePhotoUrlAttribute(); + return $this->parentGetProfilePhotoUrlAttribute(); } return $this->profile_photo_path From 0dc94044ef17942d67a4d856aaf70f010b728e9e Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Wed, 22 Nov 2023 16:56:09 -0600 Subject: [PATCH 09/11] WIP --- .github/workflows/qc.yml | 100 --------------------------------------- 1 file changed, 100 deletions(-) delete mode 100644 .github/workflows/qc.yml diff --git a/.github/workflows/qc.yml b/.github/workflows/qc.yml deleted file mode 100644 index ede70f7c..00000000 --- a/.github/workflows/qc.yml +++ /dev/null @@ -1,100 +0,0 @@ -name: QC -run-name: 'QC (PR #${{ github.event.pull_request.number }})' - -on: - pull_request: - types: [opened, synchronize, reopened] - workflow_call: - workflow_dispatch: - -concurrency: - group: qc-${{ github.event.pull_request.number }} - cancel-in-progress: true - -jobs: - phpstan: - name: Static Type Analysis (PHPStan) - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.2' - coverage: none - - - name: Install dependencies - uses: ramsey/composer-install@v2 - - - name: run PHPStan - run: composer test:types - - phpunit: - name: Unit Tests (PHPUnit) - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.2' - coverage: none - - - name: Install dependencies - uses: ramsey/composer-install@v2 - - - name: enable mysql - run: sudo systemctl start mysql.service - - - name: create empty database - run: mysql --user=root --password=root --execute="CREATE DATABASE choremanager" - - - name: run phpunit - run: composer test:unit - env: - APP_KEY: base64:UmeUVSBI8wMTAzYlIWXQOf/SoBR116L3jozOtnnUAyY= - DB_DATABASE: choremanager - DB_USERNAME: root - DB_PASSWORD: root - - standards: - name: Code Style (Pint) - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.2' - coverage: none - - - name: Install dependencies - uses: ramsey/composer-install@v2 - - - name: Check style - run: composer test:lint - - autoloader: - name: PSR-4 Autoloader - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.2' - coverage: none - - - name: Install dependencies - uses: ramsey/composer-install@v2 - - - name: Check compliance with PSR-4 - run: composer test:psr4 From 205826ca46efa50b1ff4f169b0b51f5429a57a34 Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Wed, 22 Nov 2023 17:04:21 -0600 Subject: [PATCH 10/11] WIP --- .github/workflows/dploy.yml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/dploy.yml b/.github/workflows/dploy.yml index 592b1ccc..24ed0424 100644 --- a/.github/workflows/dploy.yml +++ b/.github/workflows/dploy.yml @@ -25,8 +25,10 @@ jobs: name: 'Authenticate to Google Cloud' uses: google-github-actions/auth@v1 with: - workload_identity_provider: ${{ secrets.GOOGLE_CLOUD_WORKLOAD_IDENTITY_PROVIDER }} + create_credentials_file: true + export_environment_variables: true service_account: ${{ secrets.GOOGLE_CLOUD_SERVICE_ACCOUNT }} + workload_identity_provider: ${{ secrets.GOOGLE_CLOUD_WORKLOAD_IDENTITY_PROVIDER }} - name: Place .env run: echo ${{ secrets.ENV }} | base64 -d > .env @@ -34,17 +36,17 @@ jobs: - name: Build Docker image run: | docker build --platform linux/amd64 \ - -t ${{ vars.GOOGLE_CLOUD_REGION }}-docker.pkg.dev/${{ secrets.GOOGLE_CLOUD_PROJECT_ID }}/chore-manager/chore-manager:${{ github.sha }} \ + -t ${{ vars.GOOGLE_CLOUD_REGION }}-docker.pkg.dev/$GCP_PROJECT/chore-manager/chore-manager:${{ github.sha }} \ --file=php.dockerfile . - name: Push Docker image run: | gcloud auth configure-docker ${{ vars.GOOGLE_CLOUD_REGION }}-docker.pkg.dev - docker push ${{ secrets.ARTIFACT_REPOSITORY }}/chore-manager:${{ github.sha }} + docker push ${{ vars.GOOGLE_CLOUD_REGION }}-docker.pkg.dev/$GCP_PROJECT/chore-manager/chore-manager:${{ github.sha }} - name: Deploy to Cloud Run run: | gcloud run deploy chore-manager \ - --image ${{ secrets.ARTIFACT_REPOSITORY }}/chore-manager:${{ github.sha }} \ + --image ${{ vars.GOOGLE_CLOUD_REGION }}-docker.pkg.dev/$GCP_PROJECT/chore-manager/chore-manager:${{ github.sha }} \ --region ${{ vars.GOOGLE_CLOUD_REGION }} \ - --project ${{ secrets.GOOGLE_CLOUD_PROJECT_ID }} + --project $GCP_PROJECT From 6feee45510c564967518312f8e8a8b548b3509b8 Mon Sep 17 00:00:00 2001 From: Jordan Welch Date: Wed, 22 Nov 2023 17:07:16 -0600 Subject: [PATCH 11/11] WIP --- .github/workflows/dploy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/dploy.yml b/.github/workflows/dploy.yml index 24ed0424..f83a7f40 100644 --- a/.github/workflows/dploy.yml +++ b/.github/workflows/dploy.yml @@ -41,6 +41,7 @@ jobs: - name: Push Docker image run: | + gcloud config set account ${{ secrets.GOOGLE_CLOUD_SERVICE_ACCOUNT }} gcloud auth configure-docker ${{ vars.GOOGLE_CLOUD_REGION }}-docker.pkg.dev docker push ${{ vars.GOOGLE_CLOUD_REGION }}-docker.pkg.dev/$GCP_PROJECT/chore-manager/chore-manager:${{ github.sha }}