From 4feb130f46afcfa7b7bb215031c00f412c2a14c2 Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sat, 13 Jul 2024 08:04:07 +0100 Subject: [PATCH 01/11] chore: setup continuous deployment on firebase hosting --- .github/workflows/firebase-hosting-merge.yml | 20 ++++++++++++++++++ .../firebase-hosting-pull-request.yml | 21 +++++++++++++++++++ .gitignore | 1 + firebase.json | 16 ++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 .github/workflows/firebase-hosting-merge.yml create mode 100644 .github/workflows/firebase-hosting-pull-request.yml create mode 100644 firebase.json diff --git a/.github/workflows/firebase-hosting-merge.yml b/.github/workflows/firebase-hosting-merge.yml new file mode 100644 index 0000000..6fc7d11 --- /dev/null +++ b/.github/workflows/firebase-hosting-merge.yml @@ -0,0 +1,20 @@ +# This file was auto-generated by the Firebase CLI +# https://github.com/firebase/firebase-tools + +name: Deploy to Firebase Hosting on merge +on: + push: + branches: + - master +jobs: + build_and_deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: yarn nx run website:build + - uses: FirebaseExtended/action-hosting-deploy@v0 + with: + repoToken: ${{ secrets.GITHUB_TOKEN }} + firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }} + channelId: live + projectId: ${{ secrets.FIREBASE_PROJECT_ID }} diff --git a/.github/workflows/firebase-hosting-pull-request.yml b/.github/workflows/firebase-hosting-pull-request.yml new file mode 100644 index 0000000..14a66d4 --- /dev/null +++ b/.github/workflows/firebase-hosting-pull-request.yml @@ -0,0 +1,21 @@ +# This file was auto-generated by the Firebase CLI +# https://github.com/firebase/firebase-tools + +name: Deploy to Firebase Hosting on PR +on: pull_request +permissions: + checks: write + contents: read + pull-requests: write +jobs: + build_and_preview: + if: ${{ github.event.pull_request.head.repo.full_name == github.repository }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: yarn nx run website:build + - uses: FirebaseExtended/action-hosting-deploy@v0 + with: + repoToken: ${{ secrets.GITHUB_TOKEN }} + firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }} + projectId: ${{ secrets.FIREBASE_PROJECT_ID }} diff --git a/.gitignore b/.gitignore index 43a37dc..695b983 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ Thumbs.db .nx/workspace-data .angular .env +.firebaserc diff --git a/firebase.json b/firebase.json new file mode 100644 index 0000000..1242ff0 --- /dev/null +++ b/firebase.json @@ -0,0 +1,16 @@ +{ + "hosting": { + "public": "dist/apps/website/browser", + "ignore": [ + "firebase.json", + "**/.*", + "**/node_modules/**" + ], + "rewrites": [ + { + "source": "**", + "destination": "/index.html" + } + ] + } +} From 64b546f8a7a0575c1e06d8b13c471af6888a4ec9 Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sat, 13 Jul 2024 14:01:20 +0100 Subject: [PATCH 02/11] chore: fix deployment pipeline --- .github/workflows/cd.yml | 29 +++++++++++++++++++ .github/workflows/ci.yml | 16 ++++++++++ .github/workflows/firebase-hosting-merge.yml | 20 ------------- .../firebase-hosting-pull-request.yml | 21 -------------- .../src/lib/blog-article.component.spec.ts | 13 +++++++++ .../src/lib/blog-home.component.spec.ts | 7 +++-- 6 files changed, 63 insertions(+), 43 deletions(-) create mode 100644 .github/workflows/cd.yml delete mode 100644 .github/workflows/firebase-hosting-merge.yml delete mode 100644 .github/workflows/firebase-hosting-pull-request.yml diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..77af054 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,29 @@ +on: + workflow_call: + inputs: + environment: + required: true + type: string + description: 'The environment to deploy to' + +jobs: + deploy: + runs-on: ubuntu-latest + environment: ${{ inputs.environment }} + steps: + - uses: actions/checkout@v4 + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 20 + cache: 'yarn' + + - run: yarn install + - run: npx nx run website:build + - uses: FirebaseExtended/action-hosting-deploy@v0 + with: + repoToken: ${{ secrets.GITHUB_TOKEN }} + firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }} + channelId: live + projectId: ${{ secrets.FIREBASE_PROJECT_ID }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 37f0f54..d361de3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,6 +13,8 @@ concurrency: permissions: actions: read contents: read + checks: write + pull-requests: write jobs: test: @@ -54,3 +56,17 @@ jobs: path: | .nx key: ${{ steps.nx-cache-restore.outputs.cache-primary-key }} + + deploy-preview: + if: github.ref == 'refs/heads/develop' or github.event.pull_request.head.repo.full_name == github.repository + uses: ./.github/workflows/cd.yml + secrets: inherit + with: + environment: staging + + deploy: + if: github.ref == 'refs/heads/master' + uses: ./.github/workflows/cd.yml + secrets: inherit + with: + environment: production diff --git a/.github/workflows/firebase-hosting-merge.yml b/.github/workflows/firebase-hosting-merge.yml deleted file mode 100644 index 6fc7d11..0000000 --- a/.github/workflows/firebase-hosting-merge.yml +++ /dev/null @@ -1,20 +0,0 @@ -# This file was auto-generated by the Firebase CLI -# https://github.com/firebase/firebase-tools - -name: Deploy to Firebase Hosting on merge -on: - push: - branches: - - master -jobs: - build_and_deploy: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - run: yarn nx run website:build - - uses: FirebaseExtended/action-hosting-deploy@v0 - with: - repoToken: ${{ secrets.GITHUB_TOKEN }} - firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }} - channelId: live - projectId: ${{ secrets.FIREBASE_PROJECT_ID }} diff --git a/.github/workflows/firebase-hosting-pull-request.yml b/.github/workflows/firebase-hosting-pull-request.yml deleted file mode 100644 index 14a66d4..0000000 --- a/.github/workflows/firebase-hosting-pull-request.yml +++ /dev/null @@ -1,21 +0,0 @@ -# This file was auto-generated by the Firebase CLI -# https://github.com/firebase/firebase-tools - -name: Deploy to Firebase Hosting on PR -on: pull_request -permissions: - checks: write - contents: read - pull-requests: write -jobs: - build_and_preview: - if: ${{ github.event.pull_request.head.repo.full_name == github.repository }} - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - run: yarn nx run website:build - - uses: FirebaseExtended/action-hosting-deploy@v0 - with: - repoToken: ${{ secrets.GITHUB_TOKEN }} - firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }} - projectId: ${{ secrets.FIREBASE_PROJECT_ID }} diff --git a/libs/blog/feature-article/src/lib/blog-article.component.spec.ts b/libs/blog/feature-article/src/lib/blog-article.component.spec.ts index a9746cf..f658f6d 100644 --- a/libs/blog/feature-article/src/lib/blog-article.component.spec.ts +++ b/libs/blog/feature-article/src/lib/blog-article.component.spec.ts @@ -1,5 +1,9 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideRouter } from '@angular/router'; + import { BlogArticleComponent } from './blog-article.component'; +import { ArticleService, Article } from '@valerymelou/blog/data-access'; +import { of } from 'rxjs'; describe('BlogArticleComponent', () => { let component: BlogArticleComponent; @@ -8,6 +12,15 @@ describe('BlogArticleComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ imports: [BlogArticleComponent], + providers: [ + provideRouter([]), + { + provide: ArticleService, + useValue: { + getOne: () => of(new Article()), + }, + }, + ], }).compileComponents(); fixture = TestBed.createComponent(BlogArticleComponent); diff --git a/libs/blog/feature-home/src/lib/blog-home.component.spec.ts b/libs/blog/feature-home/src/lib/blog-home.component.spec.ts index f83e407..c88cb99 100644 --- a/libs/blog/feature-home/src/lib/blog-home.component.spec.ts +++ b/libs/blog/feature-home/src/lib/blog-home.component.spec.ts @@ -1,8 +1,11 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { BlogHomeComponent } from './blog-home.component'; -import { Article, ArticleService } from '@valerymelou/blob/data-access'; + import { of } from 'rxjs'; +import { Article, ArticleService } from '@valerymelou/blog/data-access'; + +import { BlogHomeComponent } from './blog-home.component'; + describe('BlogHomeComponent', () => { let component: BlogHomeComponent; let fixture: ComponentFixture; From cb08bbc5e65e42a4b73095180a5a1638039a6fe5 Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sat, 13 Jul 2024 14:03:16 +0100 Subject: [PATCH 03/11] chore: fix logical expression in ci.yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d361de3..28d14e9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,7 +58,7 @@ jobs: key: ${{ steps.nx-cache-restore.outputs.cache-primary-key }} deploy-preview: - if: github.ref == 'refs/heads/develop' or github.event.pull_request.head.repo.full_name == github.repository + if: github.ref == 'refs/heads/develop' || github.event.pull_request.head.repo.full_name == github.repository uses: ./.github/workflows/cd.yml secrets: inherit with: From 094233b5741a27e51da718a1c38ecf03842de281 Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sat, 13 Jul 2024 14:11:00 +0100 Subject: [PATCH 04/11] chore: add dependency on test for deploy jobs in ci.yml --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 28d14e9..8df74a2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,7 +58,8 @@ jobs: key: ${{ steps.nx-cache-restore.outputs.cache-primary-key }} deploy-preview: - if: github.ref == 'refs/heads/develop' || github.event.pull_request.head.repo.full_name == github.repository + if: github.ref == 'refs/heads/develop' + needs: test uses: ./.github/workflows/cd.yml secrets: inherit with: @@ -66,6 +67,7 @@ jobs: deploy: if: github.ref == 'refs/heads/master' + needs: test uses: ./.github/workflows/cd.yml secrets: inherit with: From 7e93a925101826fe040cdf71108ef19efe31179f Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sat, 13 Jul 2024 14:56:16 +0100 Subject: [PATCH 05/11] chore: refactor workflows --- .github/workflows/cd.yml | 32 +++++++++++++++++++++++++------- .github/workflows/ci.yml | 11 ----------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 77af054..ed7dd57 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -1,15 +1,33 @@ on: workflow_call: - inputs: - environment: - required: true - type: string - description: 'The environment to deploy to' jobs: - deploy: + staging: + if: github.ref == 'refs/heads/develop' runs-on: ubuntu-latest - environment: ${{ inputs.environment }} + environment: staging + steps: + - uses: actions/checkout@v4 + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 20 + cache: 'yarn' + + - run: yarn install + - run: npx nx run website:build + - uses: FirebaseExtended/action-hosting-deploy@v0 + with: + repoToken: ${{ secrets.GITHUB_TOKEN }} + firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }} + channelId: preview + projectId: ${{ secrets.FIREBASE_PROJECT_ID }} + + production: + if: github.ref == 'refs/heads/develop' + runs-on: ubuntu-latest + environment: staging steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8df74a2..698e462 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -57,18 +57,7 @@ jobs: .nx key: ${{ steps.nx-cache-restore.outputs.cache-primary-key }} - deploy-preview: - if: github.ref == 'refs/heads/develop' - needs: test - uses: ./.github/workflows/cd.yml - secrets: inherit - with: - environment: staging - deploy: - if: github.ref == 'refs/heads/master' needs: test uses: ./.github/workflows/cd.yml secrets: inherit - with: - environment: production From 8971d35a022404892f5fb2f6dce3e34d84583958 Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sat, 13 Jul 2024 15:20:03 +0100 Subject: [PATCH 06/11] chore: decouple build from deploy in cd.yml --- .github/workflows/cd.yml | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index ed7dd57..2b30679 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -2,10 +2,9 @@ on: workflow_call: jobs: - staging: - if: github.ref == 'refs/heads/develop' + build: runs-on: ubuntu-latest - environment: staging + environment: ${{ github.ref == 'refs/heads/master' && 'production' || 'develop' }} steps: - uses: actions/checkout@v4 @@ -17,6 +16,27 @@ jobs: - run: yarn install - run: npx nx run website:build + + - name: Archive artifacts + uses: actions/upload-artifact@v4 + with: + name: build-output + path: | + dist + + staging: + if: github.ref == 'refs/heads/develop' + needs: build + runs-on: ubuntu-latest + environment: ${{ inputs.environment }} + steps: + - uses: actions/checkout@v4 + + - name: Download build result + uses: actions/download-artifact@v4 + with: + name: build-output + - uses: FirebaseExtended/action-hosting-deploy@v0 with: repoToken: ${{ secrets.GITHUB_TOKEN }} @@ -25,20 +45,18 @@ jobs: projectId: ${{ secrets.FIREBASE_PROJECT_ID }} production: - if: github.ref == 'refs/heads/develop' + if: github.ref == 'refs/heads/master' + needs: build runs-on: ubuntu-latest environment: staging steps: - uses: actions/checkout@v4 - - name: Setup Node - uses: actions/setup-node@v4 + - name: Download build result + uses: actions/download-artifact@v4 with: - node-version: 20 - cache: 'yarn' + name: build-output - - run: yarn install - - run: npx nx run website:build - uses: FirebaseExtended/action-hosting-deploy@v0 with: repoToken: ${{ secrets.GITHUB_TOKEN }} From 037e8e7ac9e1975cf7aab6abf2ddfc894a7016f3 Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sat, 13 Jul 2024 15:49:57 +0100 Subject: [PATCH 07/11] chore: add branch name in artifact name --- .github/workflows/cd.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 2b30679..9207143 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -20,7 +20,7 @@ jobs: - name: Archive artifacts uses: actions/upload-artifact@v4 with: - name: build-output + name: build-output-${{ github.ref_name }} path: | dist @@ -35,7 +35,8 @@ jobs: - name: Download build result uses: actions/download-artifact@v4 with: - name: build-output + path: dist + name: build-output-${{ github.ref_name }} - uses: FirebaseExtended/action-hosting-deploy@v0 with: @@ -55,7 +56,8 @@ jobs: - name: Download build result uses: actions/download-artifact@v4 with: - name: build-output + path: dist + name: build-output-${{ github.ref_name }} - uses: FirebaseExtended/action-hosting-deploy@v0 with: From 44277421ab7f918219be97880f560dceb440f47a Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sat, 13 Jul 2024 15:58:12 +0100 Subject: [PATCH 08/11] chore: run deploy workflow only if ref is master or develop --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 698e462..4d4dce3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,6 +58,7 @@ jobs: key: ${{ steps.nx-cache-restore.outputs.cache-primary-key }} deploy: + if: github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/master' needs: test uses: ./.github/workflows/cd.yml secrets: inherit From fb129174012fdfd7966523fae84c576add93f378 Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sat, 13 Jul 2024 20:23:40 +0100 Subject: [PATCH 09/11] chore: fix environment in build job --- .github/workflows/cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 9207143..2eb33c7 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -4,7 +4,7 @@ on: jobs: build: runs-on: ubuntu-latest - environment: ${{ github.ref == 'refs/heads/master' && 'production' || 'develop' }} + environment: ${{ github.ref == 'refs/heads/master' && 'production' || 'staging' }} steps: - uses: actions/checkout@v4 From feef047430b693ab90fa9f39ba749c93fdffd706 Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sun, 14 Jul 2024 02:33:27 +0100 Subject: [PATCH 10/11] chore: fix environment in ci.yml --- .github/workflows/cd.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 2eb33c7..550d1f4 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -28,7 +28,7 @@ jobs: if: github.ref == 'refs/heads/develop' needs: build runs-on: ubuntu-latest - environment: ${{ inputs.environment }} + environment: staging steps: - uses: actions/checkout@v4 @@ -49,7 +49,7 @@ jobs: if: github.ref == 'refs/heads/master' needs: build runs-on: ubuntu-latest - environment: staging + environment: production steps: - uses: actions/checkout@v4 From 52f1bea7d981325aeeec4b445f881428f40a8e46 Mon Sep 17 00:00:00 2001 From: Valery Melou Date: Sun, 14 Jul 2024 03:30:18 +0100 Subject: [PATCH 11/11] chore: set contentful environment variables from secrets --- .github/workflows/cd.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 550d1f4..0b4cdd0 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -5,6 +5,10 @@ jobs: build: runs-on: ubuntu-latest environment: ${{ github.ref == 'refs/heads/master' && 'production' || 'staging' }} + env: + VM_CONTENTFUL_SPACE: ${{ secrets.VM_CONTENTFUL_SPACE }} + VM_CONTENTFUL_ACCESS_TOKEN: ${{ secrets.VM_CONTENTFUL_ACCESS_TOKEN }} + VM_CONTENTFUL_ENVIRONMENT: ${{ github.ref == 'refs/heads/master' && 'production' || 'staging' }} steps: - uses: actions/checkout@v4