Skip to content
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
31 changes: 31 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# replace-action

This lightweight action replaces substrings in files. It is useful for CI process when you needto update your configs depending on the previous steps results.
This lightweight action replaces substrings in files. It is useful for CI process when you need to update your configs depending on the previous steps results.

# Usage

See [action.yml](action.yml)

```yaml

uses: datamonsters/replace-action
uses: TracPlus/replace-action
with:
files: 'path1/file1,path2/file2'
replacements: 'foo=bar,$FOO=Bar_Value'
replacements: 'foo=bar,@FOO=Bar_Value'
```

# Example
Expand All @@ -35,7 +35,7 @@ spec:
spec:
containers:
- name: simple-app
image: $IMAGE
image: @IMAGE
env:
- name: HELLO_MSG
value: stranger
Expand All @@ -59,7 +59,7 @@ jobs:
uses: ./.github/actions/replace
with:
files: app-deployment.yaml
replacements: '$IMAGE=${{ secrets.AWS_ACC_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/demo:${{ github.sha }}'
replacements: '@IMAGE=${{ secrets.AWS_ACC_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/demo:${{ github.sha }}'

- name: Apply centraldashboard config
uses: steebchen/kubectl@master
Expand All @@ -73,4 +73,4 @@ jobs:

# License

The scripts and documentation in this project are released under the [MIT License](LICENSE)
The scripts and documentation in this project are released under the [MIT License](LICENSE)
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: 'Replace Action'
description: 'Replaces substrings in files. Useful to enrich configs with previous steps artifacts.'
author: Data Monsters
branding:
icon: 'repeat'
icon: 'repeat'
color: 'green'
inputs:
files:
Expand All @@ -12,5 +12,5 @@ inputs:
description: 'Comma-separated pairs PLACEHOLDER=value'
required: true
runs:
using: 'node12'
using: node16
main: 'index.js'
46 changes: 11 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,15 @@
const core = require('@actions/core');
const {executeAction} = require("./replace");

try {
const files = core.getInput('files');
const vars_string = core.getInput('replacements');
var filenames = files.replace(' ', '').split(',')
var vars = vars_string.split(',')
console.log('files l:'+ filenames.length)
for(var fi = 0; fi < filenames.length; fi++)
{
var filename = filenames[fi]
var fs = require('fs')
console.log('file1: '+ fi + ' '+filename)
fs.readFile(filename, 'utf8', function (err,data) {
if (err) {
console.log(err);
} else {
var result = data
console.log(data)
for(var i = 0; i < vars.length; i++)
{
var firstEqual = vars[i].indexOf('=');
var key = vars[i].substr(0,firstEqual);
var value = vars[i].substr(firstEqual+1);
result = result.replace(key, value)
}
console.log('file2: '+filename)
fs.writeFile(filename, result, 'utf8', function (err) {
if (err)
console.log(err)
else
console.log(result)
});
}
});
async function run() {
try {
await executeAction();
}
catch (error) {
core.setFailed(error.message);
}
} catch (error) {
core.setFailed(error.message);
}

run().then(() => {
console.log("Completed");
})
23 changes: 23 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const assert = require("assert");
const {processReplacements} = require("./replace");
const fs = require("fs").promises;

// shows how the runner will run a javascript action with env / stdout protocol
test('Transforms Tokens Correctly', async () => {
const testFile = "env: flex\n" +
"runtime: gs://elixir-runtime/elixir.yaml\n" +
"runtime_config:\n" +
" release_app: test_app\n" +
"env_variables:\n" +
" REPLACE_OS_VARS: true\n" +
" MIX_ENV: @MIX_ENVIRONMENT\n" +
" SECRET_AUTH_TOKEN: @SECRET_TOKEN"
await fs.writeFile('test.yaml', testFile, "utf-8");
await processReplacements('test.yaml',"@MIX_ENVIRONMENT=staging,@SECRET_TOKEN=kB504q0fP/wIoMu9hlC1CJiQU9KAVtLk+Il6f4pOCnYfVI9/Z8q8S/EAXe2asDqnOZHrtF3EKjG6rhDKBT2PxdJAfkIErv6hWlwZPD9dIck==" )
const data = await fs.readFile("test.yaml", "utf-8");
assert(data.indexOf("staging") !== -1);
assert(data.indexOf("kB504q0fP/wIoMu9hlC1C") !== -1)
await fs.unlink('test.yaml');

console.log("Test data removed.");
})
1 change: 0 additions & 1 deletion node_modules/.bin/semver

This file was deleted.

1 change: 0 additions & 1 deletion node_modules/.bin/which

This file was deleted.

Loading