├── action.yml ├── package.json ├── LICENSE ├── index.js └── README.md /action.yml: -------------------------------------------------------------------------------- 1 | name: 'Replace Action' 2 | description: 'Replaces substrings in files. Useful to enrich configs with previous steps artifacts.' 3 | author: Data Monsters 4 | branding: 5 | icon: 'repeat' 6 | color: 'green' 7 | inputs: 8 | files: 9 | description: 'Files, comma-separated. Supposed PLACEHOLDER in them' 10 | required: true 11 | replacements: 12 | description: 'Comma-separated pairs PLACEHOLDER=value' 13 | required: true 14 | runs: 15 | using: 'node12' 16 | main: 'index.js' 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-world-javascript-action", 3 | "version": "1.0.0", 4 | "description": "This action prints \"Hello World\" or \"Hello\" + the name of a person to greet to the log.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/actions/hello-world-javascript-action.git" 12 | }, 13 | "keywords": [], 14 | "author": "", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/actions/hello-world-javascript-action/issues" 18 | }, 19 | "homepage": "https://github.com/actions/hello-world-javascript-action#readme", 20 | "dependencies": { 21 | "@actions/core": "^1.1.0", 22 | "@actions/github": "^1.1.0" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Data Monsters 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 all 13 | 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 THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const core = require('@actions/core'); 2 | 3 | try { 4 | const files = core.getInput('files'); 5 | const vars_string = core.getInput('replacements'); 6 | var filenames = files.replace(' ', '').split(',') 7 | var vars = vars_string.split(',') 8 | console.log('files l:'+ filenames.length) 9 | for(var fi = 0; fi < filenames.length; fi++) 10 | { 11 | var filename = filenames[fi] 12 | var fs = require('fs') 13 | console.log('file1: '+ fi + ' '+filename) 14 | fs.readFile(filename, 'utf8', function (err,data) { 15 | if (err) { 16 | console.log(err); 17 | } else { 18 | var result = data 19 | console.log(data) 20 | for(var i = 0; i < vars.length; i++) 21 | { 22 | var firstEqual = vars[i].indexOf('='); 23 | var key = vars[i].substr(0,firstEqual); 24 | var value = vars[i].substr(firstEqual+1); 25 | result = result.replace(key, value) 26 | } 27 | console.log('file2: '+filename) 28 | fs.writeFile(filename, result, 'utf8', function (err) { 29 | if (err) 30 | console.log(err) 31 | else 32 | console.log(result) 33 | }); 34 | } 35 | }); 36 | } 37 | } catch (error) { 38 | core.setFailed(error.message); 39 | } 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # replace-action 2 | 3 | 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. 4 | 5 | # Usage 6 | 7 | See [action.yml](action.yml) 8 | 9 | ```yaml 10 | 11 | uses: datamonsters/replace-action 12 | with: 13 | files: 'path1/file1,path2/file2' 14 | replacements: 'foo=bar,$FOO=Bar_Value' 15 | ``` 16 | 17 | # Example 18 | 19 | Consider you need to apply k8s deployment with container you've built on the previous step of you workflow. 20 | You have a `app-deployment.yaml` file like this: 21 | ```yaml 22 | apiVersion: apps/v1 23 | kind: Deployment 24 | metadata: 25 | namespace: default 26 | name: simple-app 27 | spec: 28 | selector: 29 | matchLabels: 30 | simple-app: "" 31 | template: 32 | metadata: 33 | labels: 34 | simple-app: "" 35 | spec: 36 | containers: 37 | - name: simple-app 38 | image: $IMAGE 39 | env: 40 | - name: HELLO_MSG 41 | value: stranger 42 | ``` 43 | Your workflow in this case will be like this: 44 | ```yaml 45 | name: Deploy app 46 | on: [push] 47 | jobs: 48 | deploy: 49 | runs-on: ubuntu-latest 50 | steps: 51 | 52 | - name: Build and push docker 53 | id: build_step 54 | run: | 55 | docker build . -t ${{ secrets.AWS_ACC_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/demo:${{ github.sha }} 56 | docker push ${{ secrets.AWS_ACC_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/demo:${{ github.sha }} 57 | 58 | - name: Replace image in config 59 | uses: ./.github/actions/replace 60 | with: 61 | files: app-deployment.yaml 62 | replacements: '$IMAGE=${{ secrets.AWS_ACC_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/demo:${{ github.sha }}' 63 | 64 | - name: Apply centraldashboard config 65 | uses: steebchen/kubectl@master 66 | env: 67 | KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }} 68 | KUBECTL_VERSION: "1.14" 69 | with: 70 | args: apply -f app-deployment.yaml 71 | 72 | ``` 73 | 74 | # License 75 | 76 | The scripts and documentation in this project are released under the [MIT License](LICENSE) --------------------------------------------------------------------------------