Skip to content

Commit

Permalink
feat: introduce next and preview deployments (#46)
Browse files Browse the repository at this point in the history
Deployments for the main branch and pull-requests. Bootstraps API Project with NestJS.
  • Loading branch information
timonmasberg authored Feb 24, 2023
1 parent fa0c415 commit 5c3af8a
Show file tree
Hide file tree
Showing 66 changed files with 14,992 additions and 9,728 deletions.
7 changes: 0 additions & 7 deletions .browserslistrc

This file was deleted.

30 changes: 30 additions & 0 deletions .github/actions/build-and-deploy-api/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: 'build-and-deploy-api'
description: 'Builds API Project for Production and Deploys it to a given WA Slot'

inputs:
slot:
required: true
description: "Slot Identifier"
publishProfile:
required: true
description: "Azure Web App Publisher Profile"
outputs:
url:
description: "API URL"
value: ${{ steps.wa-deployment.outputs.webapp-url }}

runs:
using: "composite"
steps:
- run: |
npx nx build api --prod
cd dist/apps/api
npm i --omit=dev --ignore-scripts
shell: bash
- name: Deploy API
id: wa-deployment
uses: azure/webapps-deploy@v2
with:
app-name: 'kordis-api'
slot-name: ${{ inputs.slot }}
package: dist/apps/api/
46 changes: 46 additions & 0 deletions .github/actions/build-and-deploy-spa/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: 'build-and-deploy-spa'
description: 'Builds SPA Project for Production and Deploys it to a given SWA Environment'

inputs:
apiUrl:
required: true
description: "Base URL of the API"
deploymentName:
required: true
description: "Unique identifier for the Deployment"
deploymentEnv:
required: true
description: "Azure SWA Deployment Environment"
angularEnvironment:
required: true
description: "Angular Environment to use from the project.json"
publishToken:
required: true
description: "Azure Static Web App Deployment Token"
outputs:
url:
description: "SPA URL"
value: ${{ steps.spa-deployment.outputs.static_web_app_url }}

runs:
using: "composite"
steps:
- run: |
envsubst < apps/spa/src/environments/environment.template > apps/spa/src/environments/environment.${{ inputs.angularEnvironment }}.ts
npx nx build spa --prod --configuration=${{ inputs.angularEnvironment }}
shell: bash
env:
IS_PRODUCTION: true
DEPLOYMENT_NAME: ${{ inputs.deploymentName }}
API_URL: ${{ inputs.apiUrl }}
- name: Deploy SPA
id: spa-deployment
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ inputs.publishToken }}
action: "upload"
app_location: "dist/apps/spa/"
output_location: ''
skip_app_build: true
skip_api_build: true
deployment_environment: ${{ inputs.deploymentEnv }}
28 changes: 22 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,36 @@ on:
branches:
- main
pull_request:
types:
- opened
- edited
- synchronize

jobs:
check-pr-title:
if: github.event_name == 'pull_request' && (github.event.action == 'edited' || github.event.action == 'opened')
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

main:
if: github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'synchronize')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Ensure Conventional Commits
uses: webiny/[email protected]
- name: Setup node
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'npm'
- uses: nrwl/nx-set-shas@v3
- run: npm ci
- run: npx nx workspace-lint
- uses: nrwl/nx-set-shas@v3
- name: Check Formatting
run: npx nx format:check
- name: Lint
Expand All @@ -28,12 +42,14 @@ jobs:
run: npx nx affected --target=test --parallel=3 --ci --coverage --coverageReporters=lcov
- name: Merge Coverage files
run: '[ -d "./coverage/" ] && ./node_modules/.bin/lcov-result-merger ./coverage/**/lcov.info ./coverage/lcov.info || exit 0'
- name: Build
run: |
touch apps/spa/src/environments/environment.prod.ts
npx nx affected --target=build --parallel=3
- name: Run e2es
run: npx nx affected --target=e2e
- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- name: Build
run: npx nx affected --target=build --parallel=3
- name: Run e2es
run: npx nx affected --target=e2e
40 changes: 40 additions & 0 deletions .github/workflows/next-deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Next Deployment
concurrency: next_environment

on:
push:
branches:
- main

jobs:
deployment:
runs-on: ubuntu-latest
environment:
name: Next
url: ${{ steps.spa-deployment.outputs.url }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'npm'
- run: npm ci --ignore-scripts
- name: Azure Login
uses: Azure/[email protected]
with:
creds: '${{ secrets.AZURE_CREDENTIALS }}'
- name: Build and Deploy API
id: api-deployment
uses: ./.github/actions/build-and-deploy-api
with:
slot: "next"
publishProfile: ${{ secrets.AZURE_WEBAPP_API_PUBLISH_PROFILE }}
- name: Build and Deploy SPA
id: spa-deployment
uses: ./.github/actions/build-and-deploy-spa
with:
apiUrl: ${{ steps.api-deployment.outputs.url }}
deploymentName: "main.${{ github.sha }}"
angularEnvironment: "production"
deploymentEnv: "next"
publishToken: ${{ secrets.AZURE_STATIC_WEB_APP_TOKEN }}
204 changes: 204 additions & 0 deletions .github/workflows/preview-deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
name: Preview Deployment
concurrency: ${{ github.ref }}_environment

on:
pull_request:
types: [ closed, synchronize ]
issue_comment:
types: [ created ]

permissions:
pull-requests: write

jobs:
has-deployment:
if: github.event.issue.pull_request || github.event_name == 'pull_request'
outputs:
has-swa: ${{ steps.has-swa.outputs.has-swa }}
has-wa: ${{ steps.has-wa.outputs.has-wa }}
runs-on: ubuntu-latest
steps:
- name: Azure Login
uses: Azure/[email protected]
with:
creds: '${{ secrets.AZURE_CREDENTIALS }}'
- name: Check if Webapp Environment exists
id: has-swa
run: >
az staticwebapp environment list --name kordis-spa | grep -q '"name": "pr${{ github.event.pull_request.number || github.event.issue.number }}"'
&& echo "has-swa=true" >> $GITHUB_OUTPUT
|| echo "has-swa=false" >> $GITHUB_OUTPUT
- name: Check if API Environment exists
id: has-wa
run: >
az webapp deployment slot list -n kordis-api --resource-group KORDIS | grep -q '"name": "pr${{ github.event.pull_request.number || github.event.issue.number }}"'
&& echo "has-wa=true" >> $GITHUB_OUTPUT
|| echo "has-wa=false" >> $GITHUB_OUTPUT
comment-handler:
if: |
(github.event_name == 'issue_comment' && github.event.issue.pull_request && startsWith(github.event.comment.body, '/deploy-preview')) &&
(needs.has-deployment.outputs.has-swa == 'false' && needs.has-deployment.outputs.has-wa == 'false')
needs: has-deployment
runs-on: ubuntu-latest
outputs:
is-admin: ${{ steps.is-admin-check.outputs.require-result }}
steps:
- name: Check Permission
uses: actions-cool/check-user-permission@v2
id: is-admin-check
with:
require: 'admin'
- name: Remove Comment
uses: actions/github-script@v6
with:
script: 'github.rest.issues.deleteComment({ comment_id: context.payload.comment.id, owner: context.repo.owner, repo: context.repo.repo })'

deployment:
needs: comment-handler
runs-on: ubuntu-latest
if: |
(github.event_name == 'issue_comment' && github.event.issue.pull_request && startsWith(github.event.comment.body, '/deploy-preview')) &&
(needs.comment-handler.outputs.is-admin == 'true')
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'npm'
- run: npm ci --ignore-scripts
- name: Initial Deployment Preview Comment
uses: peter-evans/[email protected]
id: pr-preview-comment
with:
issue-number: ${{ github.event.issue.number }}
body: |
### 🚧 Building Deployment Preview..
A preview of this Pull Request is being created. Hold tight while it's building ⚒️
This comment will be automatically updated when the preview is ready.
- name: Azure Login
uses: Azure/[email protected]
with:
creds: '${{ secrets.AZURE_CREDENTIALS }}'
- name: Create PR Deployment Slot
run: az webapp deployment slot create --name kordis-api --resource-group KORDIS --configuration-source next --slot pr${{ github.event.issue.number }}
- name: Build and Deploy API
id: api-deployment
uses: ./.github/actions/build-and-deploy-api
with:
slot: "pr${{ github.event.issue.number }}"
publishProfile: ${{ secrets.AZURE_WEBAPP_API_PUBLISH_PROFILE }}
- name: Build and Deploy SPA
id: spa-deployment
uses: ./.github/actions/build-and-deploy-spa
with:
apiUrl: ${{ steps.api-deployment.outputs.url }}
deploymentName: "PR-${{ github.event.issue.number }}.{{ github.sha }}"
deploymentEnv: "pr${{github.event.issue.number }}"
angularEnvironment: "production"
publishToken: ${{ secrets.AZURE_STATIC_WEB_APP_TOKEN }}
- name: Update PR Preview Comment
uses: peter-evans/[email protected]
with:
comment-id: ${{ steps.pr-preview-comment.outputs.comment-id }}
edit-mode: replace
body: |
### 🚀 Deployment Preview
SPA: ${{ steps.spa-deployment.outputs.url }}
API: ${{ steps.api-deployment.outputs.url }}
Commit SHA: ${{ github.sha }}
reactions: "rocket"

update-deployment:
needs: has-deployment
runs-on: ubuntu-latest
if: |
(github.event_name == 'pull_request' && github.event.action == 'synchronize') &&
(needs.has-deployment.outputs.has-swa == 'true' || needs.has-deployment.outputs.has-wa == 'true')
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'npm'
- run: npm ci --ignore-scripts
- name: Find PR Preview Comment
uses: peter-evans/find-comment@v2
id: deploy-preview-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: Deployment Preview
- name: Update PR Preview Comment
if: steps.deploy-preview-comment.outputs.comment-id != ''
uses: peter-evans/[email protected]
with:
comment-id: ${{ steps.deploy-preview-comment.outputs.comment-id }}
edit-mode: replace
body: |
### 🚧 Building Deployment Preview..
The Pull Request preview is being updated. Hold tight while it's building ⚒️
This comment will be automatically updated when the new version is ready.
- name: Azure Login
uses: Azure/[email protected]
with:
creds: '${{ secrets.AZURE_CREDENTIALS }}'
- name: Build and Deploy API
id: api-deployment
uses: ./.github/actions/build-and-deploy-api
with:
slot: "pr${{ github.event.pull_request.number }}"
publishProfile: ${{ secrets.AZURE_WEBAPP_API_PUBLISH_PROFILE }}
- name: Build and Deploy SPA
id: spa-deployment
uses: ./.github/actions/build-and-deploy-spa
with:
apiUrl: ${{ steps.api-deployment.outputs.url }}
deploymentName: "PR-${{ github.event.pull_request.number }}.{{ github.sha }}"
deploymentEnv: "pr${{ github.event.pull_request.number }}"
angularEnvironment: "production"
publishToken: ${{ secrets.AZURE_STATIC_WEB_APP_TOKEN }}
- name: Update PR Preview Comment
uses: peter-evans/[email protected]
with:
comment-id: ${{ steps.deploy-preview-comment.outputs.comment-id }}
edit-mode: replace
body: |
### 🚀 Deployment Preview
SPA: ${{ steps.spa-deployment.outputs.url }}
API: ${{ steps.api-deployment.outputs.url }}
Commit SHA: ${{ github.sha }}
reactions: "rocket"

delete-deployment:
needs: has-deployment
runs-on: ubuntu-latest
if: |
(github.event_name == 'pull_request' && github.event.action == 'closed') &&
(needs.has-deployment.outputs.has-swa == 'true' || needs.has-deployment.outputs.has-wa == 'true')
steps:
- name: Azure Login
uses: Azure/[email protected]
with:
creds: '${{ secrets.AZURE_CREDENTIALS }}'
- name: Delete API Deployment and Resources
run: az webapp deployment slot delete --name kordis-api --resource-group KORDIS --slot pr${{ github.event.pull_request.number }}
- name: Delete SPA Deployment and Resources
run: az staticwebapp environment delete --name kordis-spa --resource-group KORDIS --environment-name pr${{ github.event.pull_request.number }} --yes
- name: Find PR Preview Comment
uses: peter-evans/find-comment@v2
id: deploy-preview-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: Deployment Preview
- name: Update PR Preview Comment
if: steps.deploy-preview-comment.outputs.comment-id != ''
uses: peter-evans/[email protected]
with:
comment-id: ${{ steps.deploy-preview-comment.outputs.comment-id }}
edit-mode: replace
body: |
### Deployment Preview
🏁 This PR has been closed. No deployment preview is available.
reactions: "hooray"
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16
18
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/dist
/coverage
package-lock.json
.github
Loading

0 comments on commit 5c3af8a

Please sign in to comment.