Skip to content
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

support file only #1

Merged
merged 2 commits into from
Nov 26, 2023
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
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Render yml/yaml files action

This action retrieves secrets and variables from the GitHub context and replaces with values in files (yml/yaml extensions) stated in target-folder parameter if it finds a string with *ENV_* prefix that is *ENV_<variable/secret name>*.
This action retrieves secrets and variables from the GitHub context and replaces with values in file(s) (yml/yaml extensions) stated in target parameter if it finds a string with *ENV_* prefix that is *ENV_<variable/secret name>*. If file parameter is given true then it processes single file provided in target, if not it processes files in folder given in target parameter.

## Inputs

### `target-folder`
### `target`

**Required** The name of folder contains files to be edited which has *ENV_* words in it.
**Required** The name of folder/file contains file(s) to be edited which has *ENV_* words in it.

### `secrets-context`

Expand All @@ -27,7 +27,8 @@ uses: ftasbasi/renderfile@v1.8
with:
secrets-context: ${{ toJson(secrets) }}
variables-context: ${{ toJson(vars) }}
target-folder: foldername
target: foldername/filename
file: true # default false
env:
VAR1: "sample value" # for changing ENV_VAR1 in files
```
Expand Down
7 changes: 5 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ name: 'Render File'
description: 'Render files in the folder with yml/yaml extensions'
author: 'Furkan Tasbasi'
inputs:
target-folder:
description: 'Target folder to change files in it.'
file:
description: 'Target is file or not. Default false (folder)'
required: true
target:
description: 'Target folder/file to change file(s) in it.'
required: true
secrets-context:
description: 'Secret context of the current workflow.'
Expand Down
39 changes: 30 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
const fs = require('fs');
const path = require('path');
const core = require('@actions/core');

try {
const targetFolder = core.getInput('target-folder');
const target = core.getInput('target');
const processFile = core.getInput('file') === 'true' || false; // Set to false by default
const secretsContext = JSON.parse(core.getInput('secrets-context'));
const varsContext = JSON.parse(core.getInput('variables-context'));

const targetFiles = fs.readdirSync(targetFolder).filter(file => file.endsWith('.yml') || file.endsWith('.yaml'));

targetFiles.forEach(targetFile => {
const filePath = path.join(targetFolder, targetFile);
if (processFile) {
// Process a single file specified in 'target'
processSingleFile(target, secretsContext, varsContext);
} else {
// Process all files in the folder specified in 'target'
processAllFiles(target, secretsContext, varsContext);
}
} catch (error) {
console.error(error.message);
process.exit(1);
}

function processSingleFile(filePath, secretsContext, varsContext) {
try {
let data = fs.readFileSync(filePath, 'utf8');

// Replace the template variables with their values if exist in secrets
Expand All @@ -35,10 +46,20 @@ try {

// Overwrite the result to the file
fs.writeFileSync(filePath, data);

console.log(`File processed successfully: ${filePath}`);
} catch (error) {
console.error(`Error processing file ${filePath}: ${error.message}`);
}
}

function processAllFiles(folderPath, secretsContext, varsContext) {
const targetFiles = fs.readdirSync(folderPath).filter(file => file.endsWith('.yml') || file.endsWith('.yaml'));

targetFiles.forEach(targetFile => {
const filePath = path.join(folderPath, targetFile);
processSingleFile(filePath, secretsContext, varsContext);
});

// Now you can use 'targetFolder', 'secretsContext', and 'varsContext' in your script logic
} catch (error) {
console.error(error.message);
process.exit(1);
console.log(`All files in ${folderPath} processed successfully.`);
}
Loading