Created
March 26, 2020 10:45
-
-
Save krzysztof-miemiec/a1a9e13d424ee71a1918e69af3334523 to your computer and use it in GitHub Desktop.
Some kind of stages stub to be able to use serverless-next.js in order to deploy to multiple envs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// serverless/stages/serverless.js | |
const { Component } = require('@serverless/core'); | |
const required = (name) => { | |
throw new Error(`An argument "${name}" is required.`); | |
}; | |
const processRef = (value, data) => { | |
for (const key in data) { | |
value = value.replace(new RegExp(`%\\{${key}}`, 'g'), data[key]); | |
} | |
return value; | |
}; | |
const resolveInputRefs = (input, data) => { | |
if (Array.isArray(input)) { | |
return input.map(i => resolveInputRefs(i, data)); | |
} | |
if (typeof input === 'object') { | |
const result = {}; | |
for (const key in input) { | |
if (input.hasOwnProperty(key)) { | |
result[key] = resolveInputRefs(input[key], data); | |
} | |
} | |
return result; | |
} | |
if (typeof input === 'string') { | |
return processRef(input, data); | |
} | |
return input; | |
}; | |
class StagesComponent extends Component { | |
async default({ | |
target: { | |
component: targetComponent = required('target.component'), | |
input: targetInput = {}, | |
} = required('target'), | |
branchEnv = required('branchEnv'), | |
stages: { | |
default: defaultStage = required('stages.default'), | |
...stages | |
} = required('branches'), | |
envs = required('envs'), | |
} = {}) { | |
this.context.log('Processing inputs'); | |
const stage = stages[process.env[branchEnv]] || defaultStage; | |
const data = envs[stage]; | |
const resolvedInput = resolveInputRefs(targetInput, data); | |
this.context.log('Loading target component'); | |
const target = await this.load(targetComponent, targetInput); | |
this.context.log('Executing target component'); | |
return target(resolvedInput); | |
} | |
} | |
module.exports = StagesComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: website | |
website: | |
component: './serverless/stages' | |
inputs: | |
target: | |
component: 'serverless-next.js' | |
input: | |
domain: | |
- www | |
- '%{domain}' # Note '%' instead of '$' | |
branchEnv: GITHUB_REF # Branch name is taken from "GITHUB_REF" variable - this example works in Github Actions | |
stages: # Usually in CI/CD you have to map branch name to environment, you can do it here | |
default: 'dev' # Default "stage" | |
'refs/heads/develop': 'dev' | |
'refs/heads/staging': 'staging' | |
'refs/heads/master': 'prod' | |
envs: | |
dev: | |
domain: dev.example.com | |
staging: | |
domain: staging.example.com | |
prod: | |
domain: example.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment