Skip to content

feat: support filtering secrets by branch #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ package = "@netlify/plugin-secrets-manager"
## Context based secrets

The plugin has support for context based secrets, to allow injecting AWS secrets only to builds with a specific deploy context.
To configure the context for a secret, add a secret tag via AWS secrets manager with a name of `NETLIFY_CONTEXT` and value of `production`, `deploy-preview` or `branch-deploy`.
To configure the context for a secret, add a secret tag via AWS secrets manager with a name of `NETLIFY_CONTEXT` and value of `production`, `deploy-preview`, `branch-deploy` or any branch name in your `git` repository.

>To learn more about deploy contexts, visit [Netlify's documentation](https://docs.netlify.com/site-deploys/overview/#deploy-contexts)

As a result, the plugin will inject the AWS secret only to builds with the matching deploy context.

Expand Down
14 changes: 9 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ module.exports = {
NETLIFY_AWS_SECRET_ACCESS_KEY: secretAccessKey,
NETLIFY_AWS_DEFAULT_REGION: region = 'us-east-1',
CONTEXT,
HEAD,
} = process.env

if (!accessKeyId) {
return utils.build.failBuild(`Missing environment variable NETLIFY_AWS_ACCESS_KEY_ID`)
}
Expand All @@ -95,12 +97,14 @@ module.exports = {
return
}

// inject only to matching context
if (CONTEXT === context) {
// inject only to matching context/branch
const matchedContext = CONTEXT === context
const matchedBranch = HEAD === context
if (matchedContext || matchedBranch) {
console.log(
`${chalk.bold('Injecting AWS secret')} ${chalk.magenta(`${key}`)} as ${chalk.green(
prefixedKey,
)} to context ${chalk.yellow(context)}`,
`${chalk.bold('Injecting AWS secret')} ${chalk.magenta(`${key}`)} as ${chalk.green(prefixedKey)} to ${
matchedContext ? 'context' : 'branch'
} ${chalk.yellow(context)}`,
)
/* eslint-disable-next-line no-param-reassign */
netlifyConfig.build.environment[prefixedKey] = value
Expand Down