Skip to content

Commit 0abac69

Browse files
committed
Initial commit
0 parents  commit 0abac69

File tree

13 files changed

+400
-0
lines changed

13 files changed

+400
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
*.iml
3+
.DS_Store
4+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 Night Story Ltd
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Replace environment variabels in files in a GitHub Action
2+
3+
## Inputs
4+
5+
- `input_file`
6+
- Path to file with the placeholders to replace.
7+
- `input_file`
8+
- Path to output file.
9+
- Optional, defaults to `input_file`
10+
- `pattern`
11+
- Which placeholders to replace.
12+
- Options:
13+
- `single_dollar_brackets` -> `${VARIABLE}`
14+
- `double_dollar_brackets` -> `${{VARIABLE}}`
15+
- `double_underscore` -> `__VARIABLE__`
16+
- Optional, defaults to `single_dollar_brackets`
17+
- `fail_on_missing_env`
18+
- If true, will fail if a key required in the input file is missing in the environment.
19+
- default: `false`
20+
21+
## Example usage
22+
23+
The `input_file`:
24+
25+
```json
26+
{
27+
"foo": "${FOO_BAR_123}"
28+
}
29+
```
30+
31+
⚠️ no spaces around the key.
32+
33+
The workflow:
34+
35+
```yaml
36+
name: whatever
37+
38+
env:
39+
FOO_BAR_123: 'literally whatever'
40+
```
41+
42+
The result:
43+
44+
```json
45+
{
46+
"foo": "literally whatever"
47+
}
48+
```
49+
50+
## License
51+
52+
Licensed under MIT license.<br/>
53+
Please also see [licenses.txt](lib/licenses.txt)

action.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: 'replace-env'
2+
description: 'Replace environment variables in files'
3+
branding:
4+
icon: briefcase
5+
color: blue
6+
inputs:
7+
input_file:
8+
description: 'Input file path'
9+
required: true
10+
output_file:
11+
description: 'Output file path'
12+
default: ''
13+
required: false
14+
fail_on_missing_env:
15+
description: 'Whether to fail if an env var mentioned in the input file does not exist'
16+
default: 'false'
17+
required: false
18+
pattern:
19+
description: 'Pattern to replace the variables: single_dollar_brackets, double_dollar_brackets, double_underscore'
20+
default: 'single_dollar_brackets'
21+
required: false
22+
runs:
23+
using: 'node16'
24+
main: 'lib/index.js'

lib/index.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/licenses.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@actions/core
2+
MIT
3+
The MIT License (MIT)
4+
5+
Copyright 2019 GitHub
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12+
13+
@actions/http-client
14+
MIT
15+
Actions Http Client for Node.js
16+
17+
Copyright (c) GitHub, Inc.
18+
19+
All rights reserved.
20+
21+
MIT License
22+
23+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
24+
associated documentation files (the "Software"), to deal in the Software without restriction,
25+
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
26+
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
27+
subject to the following conditions:
28+
29+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
30+
31+
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
32+
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
33+
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
34+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36+
37+
38+
tunnel
39+
MIT
40+
The MIT License (MIT)
41+
42+
Copyright (c) 2012 Koichi Kobayashi
43+
44+
Permission is hereby granted, free of charge, to any person obtaining a copy
45+
of this software and associated documentation files (the "Software"), to deal
46+
in the Software without restriction, including without limitation the rights
47+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
48+
copies of the Software, and to permit persons to whom the Software is
49+
furnished to do so, subject to the following conditions:
50+
51+
The above copyright notice and this permission notice shall be included in
52+
all copies or substantial portions of the Software.
53+
54+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
55+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
56+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
57+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
58+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
59+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
60+
THE SOFTWARE.

lib/sourcemap-register.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "replace-env",
3+
"description": "Replace environment variables in files",
4+
"version": "1.0.0",
5+
"keywords": [
6+
"github",
7+
"actions",
8+
"github actions",
9+
"replace",
10+
"environment variables"
11+
],
12+
"license": "MIT",
13+
"main": "index.js",
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/nightstory/replace-env.git"
17+
},
18+
"bugs": {
19+
"url": "https://github.com/nightstory/replace-env/issues"
20+
},
21+
"homepage": "https://github.com/nightstory/replace-env#readme",
22+
"author": "nightstory",
23+
"dependencies": {
24+
"@actions/core": "^1.8.2"
25+
},
26+
"devDependencies": {
27+
"@types/node": "^17.0.17",
28+
"@vercel/ncc": "^0.33.3",
29+
"typescript": "^4.5.5"
30+
},
31+
"scripts": {
32+
"build": "node_modules/.bin/ncc build --license licenses.txt --source-map -m -q src/main.ts -o lib"
33+
}
34+
}

src/main.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import * as core from '@actions/core'
2+
import fs from 'fs';
3+
4+
import getOptions, {Options, ReplacementPattern, validateOptions} from './options'
5+
6+
const main = async () => {
7+
const options: Options = getOptions()
8+
9+
if (!validateOptions(options)) {
10+
process.exit(1)
11+
}
12+
13+
let pattern = /\${\w+}/gi;
14+
let matcher = /\${(?<var>\w+)}/i;
15+
16+
switch (options.pattern) {
17+
case ReplacementPattern.SingleDollarBrackets:
18+
pattern = /\${\w+}/gi;
19+
matcher = /\${(?<var>\w+)}/i;
20+
break;
21+
case ReplacementPattern.DoubleDollarBrackets:
22+
pattern = /\${{\w+}}/gi;
23+
matcher = /\${{(?<var>\w+)}}/i;
24+
break;
25+
case ReplacementPattern.DoubleUnderscore:
26+
pattern = /__\w+__/gi;
27+
matcher = /__(?<var>\w+)__/i;
28+
break;
29+
}
30+
31+
if (fs.existsSync(options.inputFile)) {
32+
const data = fs.readFileSync(options.inputFile, 'utf8');
33+
const res = data.replace(pattern, (c) => {
34+
const match = c.match(matcher);
35+
36+
if (match === null) {
37+
core.warning(`[replace-env] error happened, invalid match for ${c}: ${match}`)
38+
return c;
39+
}
40+
41+
let env = process.env[match[1]];
42+
43+
if (typeof env === 'undefined') {
44+
if (options.failOnMissingEnv) {
45+
core.error(`[replace-env] Environment Variable ${match[1]} not found!`);
46+
throw new Error('[replace-env] Environment Variable ${match[1]} not found!');
47+
} else {
48+
core.warning(`[replace-env] Environment Variable ${match[1]} not found!`);
49+
env = c;
50+
}
51+
} else {
52+
core.info(`[replace-env] Replacing Environment Variable ${match[1]}.`);
53+
}
54+
55+
return env;
56+
});
57+
58+
let resultFile = options.inputFile;
59+
if (options.outputFile !== null) {
60+
resultFile = options.outputFile;
61+
}
62+
63+
fs.writeFileSync(resultFile, res);
64+
core.info(`[replace-env] File ${resultFile} saved.`);
65+
} else {
66+
throw new Error('[replace-env] input_file is missing')
67+
}
68+
}
69+
70+
try {
71+
main()
72+
} catch (error) {
73+
core.setFailed(`${error}`)
74+
}

0 commit comments

Comments
 (0)