Skip to content

Commit

Permalink
Fix and improve deployment workflow using environments (#773)
Browse files Browse the repository at this point in the history
* Fix and improve deployment workflow using environments

* Fix workflow

* Try to fix path to deployment

* Fix database reset

* Fix slot name

* Remove debugging output from workflow

* Fix apollo link

* Fix apollo link

* Only import redis-mock for local dev

* Fix import

* Also make env availabe in client runtime config
  • Loading branch information
tobiasdiez authored Jan 10, 2022
1 parent d505726 commit f328638
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 54 deletions.
116 changes: 69 additions & 47 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ on:
workflow_dispatch:

jobs:
deploy:
name: Deploy
build:
name: Build

runs-on: ubuntu-latest

Expand All @@ -36,6 +36,7 @@ jobs:
- name: Setup Node.js
uses: actions/[email protected]
with:
# Should be kept in sync with version configured in Azure
node-version: 14.x
cache: 'yarn'

Expand All @@ -48,70 +49,91 @@ jobs:
- name: Prepare artifact
run: |
mkdir artifact
cp -r .nuxt nuxt.config.ts package.json config.ts ./api ./static ./node_modules artifact
cp -r .output ecosystem.config.js artifact
- name: Upload build artifacts
uses: actions/upload-artifact@v2
with:
name: server-build
path: artifact

deploy:
runs-on: ubuntu-latest

needs: build

strategy:
matrix:
include:
- environment: Test
url: https://jabref-dev.azurewebsites.net/
- environment: Staging
url: https://jabref-staging.azurewebsites.net/

name: Deploy to ${{ matrix.environment }}

environment:
name: ${{ matrix.environment }}
url: ${{ matrix.url }}

steps:
- name: Download build artifacts
uses: actions/download-artifact@v2
with:
name: server-build

- name: Login to Azure
uses: Azure/login@v1
with:
creds: ${{secrets.AZURE_CREDENTIALS}}

- name: Start deployment (Test server)
uses: bobheadxi/[email protected]
id: deployment-test
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: Test

- name: Deploy to Azure WebApp (Test server)
- name: Deploy to Azure WebApp
uses: azure/webapps-deploy@v2
with:
app-name: JabRef
publish-profile: ${{ secrets.AZURE_TEST_PUBLISH_PROFILE }}
package: ./artifact
publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}

- name: Checkout
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/[email protected]
with:
node-version: 14.x
cache: 'yarn'

- name: Install dependencies
run: yarn install

- name: Reset database on Azure (Test database)
if: matrix.environment == 'Test'
run: yarn prisma:migrate:reset --force
env:
DATABASE_URL: ${{ secrets.AZURE_TEST_DATABASE_URL }}

- name: Update deployment status (Test server)
uses: bobheadxi/[email protected]
if: always()
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment-test.outputs.deployment_id }}
env_url: https://jabref-dev.azurewebsites.net/

- name: Start deployment (Staging server)
uses: bobheadxi/[email protected]
id: deployment-staging
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: Staging

- name: Deploy to Azure WebApp (Staging server)
uses: azure/webapps-deploy@v2
with:
app-name: JabRef
publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}
package: ./artifact

- name: Update database on Azure (Production database)
if: matrix.environment == 'Staging'
run: yarn prisma:migrate:deploy
env:
DATABASE_URL: ${{ secrets.AZURE_DATABASE_URL }}

- name: Update deployment status (Staging server)
uses: bobheadxi/[email protected]
if: always()
deploy-prod:
name: Deploy to Production

runs-on: ubuntu-latest

needs: deploy

environment:
name: Production
url: https://jabref.azurewebsites.net/

steps:
- name: Login to Azure
uses: Azure/login@v1
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment-staging.outputs.deployment_id }}
env_url: https://jabref-staging.azurewebsites.net/
creds: ${{secrets.AZURE_CREDENTIALS}}

- name: Swap staging and production deployment slots
run: az webapp deployment slot swap -g JabRefOnline -n JabRef --slot staging --target-slot production

2 changes: 1 addition & 1 deletion api/utils/services.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ // TODO: Remove once redis-mock is updated
import { promisify } from 'util'
import redis, { RedisClientType } from 'redis'
import redisMock from 'redis-mock'
import { Environment } from '~/config'
import config from '#config'

export async function createRedisClient(): Promise<RedisClientType<any, any>> {
if (config.environment === Environment.LocalDevelopment) {
const redisMock = (await import('redis-mock')).default
const mockRedis = redisMock.createClient()
// Workaround for redis-mock being not compatible with redis@4
// TODO: Remove this workaround once https://github.com/yeahoffline/redis-mock/issues/195 is fixed
Expand Down
8 changes: 7 additions & 1 deletion config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PrivateRuntimeConfig } from '@nuxt/schema'
import type { PrivateRuntimeConfig, PublicRuntimeConfig } from '@nuxt/schema'

export enum Environment {
/**
Expand Down Expand Up @@ -47,3 +47,9 @@ export function constructPrivateConfig(): PrivateRuntimeConfig {
},
}
}

export function constructPublicConfig(): PublicRuntimeConfig {
return {
environment: getEnvironment(),
}
}
5 changes: 4 additions & 1 deletion config.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Environment } from './config'
// TODO: Remove this as soon as https://github.com/nuxt/framework/issues/1785 is resolved.
declare module '@nuxt/schema' {
interface PrivateRuntimeConfig {
environment: Environment
redis: {
port: number
host: string
Expand All @@ -15,5 +14,9 @@ declare module '@nuxt/schema' {
secondarySecret: string
}
}

interface PublicRuntimeConfig {
environment: Environment
}
}
export {}
11 changes: 11 additions & 0 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
apps: [
{
name: 'JabRefOnline',
// https://pm2.keymetrics.io/docs/usage/cluster-mode/
exec_mode: 'cluster',
instances: 'max',
script: './.output/server/index.mjs',
},
],
}
8 changes: 7 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineNuxtConfig } from '@nuxt/bridge'
import typescript from 'rollup-plugin-typescript2'
import { constructPrivateConfig } from './config'
import { constructPrivateConfig, constructPublicConfig } from './config'

export default defineNuxtConfig({
/*
Expand Down Expand Up @@ -148,6 +148,12 @@ export default defineNuxtConfig({
*/
privateRuntimeConfig: constructPrivateConfig(),

/*
** Client and server-side configuration
** See https://nuxtjs.org/docs/directory-structure/nuxt-config#runtimeconfig
*/
publicRuntimeConfig: constructPublicConfig(),

/*
** Build configuration
** See https://nuxtjs.org/api/configuration-build/
Expand Down
6 changes: 3 additions & 3 deletions plugins/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export default defineNuxtPlugin((nuxtApp) => {

const config = useRuntimeConfig()
let httpLink
if (config.environment === Environment.Production) {
httpLink = new HttpLink({ uri: '/api', fetch })
} else {
if (config.environment === Environment.LocalDevelopment) {
httpLink = new HttpLink({ uri: 'http://localhost:3000/api', fetch })
} else {
httpLink = new HttpLink({ uri: 'http://0.0.0.0:8080/api', fetch })
}

// Print errors
Expand Down

0 comments on commit f328638

Please sign in to comment.