diff --git a/README.md b/README.md index 5e975a4..8d33bfa 100644 --- a/README.md +++ b/README.md @@ -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_*. +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_*. 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` @@ -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 ``` diff --git a/action.yml b/action.yml index f7d80f2..9f0dc1a 100644 --- a/action.yml +++ b/action.yml @@ -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.' diff --git a/index.js b/index.js index 0b79c45..11fe521 100644 --- a/index.js +++ b/index.js @@ -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'; // Check if 'file' is set to 'true' 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 @@ -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.`); }