-
Notifications
You must be signed in to change notification settings - Fork 148
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
feat: Include Environment Variables in Task Definitions with JSON Fragments #73
base: master
Are you sure you want to change the base?
Changes from all commits
7395878
fb78f14
0ab86bc
973f34d
6b64c34
2843105
77ec5e7
7a6b7e8
07f05d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,26 @@ const path = require('path'); | |
const core = require('@actions/core'); | ||
const tmp = require('tmp'); | ||
const fs = require('fs'); | ||
const mergeWith = require('lodash.mergewith'); | ||
|
||
// Customizer for lodash mergeWith | ||
// allows arrays in the original task definition to contain | ||
// values as opposed to only in the mergeFiles (otherwise | ||
// they are overridden) | ||
// https://lodash.com/docs/4.17.15#mergeWith | ||
function customizer(objValue, srcValue) { | ||
if (Array.isArray(objValue)) { | ||
return objValue.concat(srcValue); | ||
} | ||
} | ||
|
||
async function run() { | ||
try { | ||
// Get inputs | ||
const taskDefinitionFile = core.getInput('task-definition', { required: true }); | ||
const containerName = core.getInput('container-name', { required: true }); | ||
const imageURI = core.getInput('image', { required: true }); | ||
const imageURI = core.getInput('image', { required: false }); | ||
const mergeFile = core.getInput('merge', { required: false }); | ||
|
||
// Parse the task definition | ||
const taskDefPath = path.isAbsolute(taskDefinitionFile) ? | ||
|
@@ -19,7 +32,7 @@ async function run() { | |
} | ||
const taskDefContents = require(taskDefPath); | ||
|
||
// Insert the image URI | ||
// Get containerDef with name `containerName` | ||
if (!Array.isArray(taskDefContents.containerDefinitions)) { | ||
throw new Error('Invalid task definition format: containerDefinitions section is not present or is not an array'); | ||
} | ||
|
@@ -29,7 +42,38 @@ async function run() { | |
if (!containerDef) { | ||
throw new Error('Invalid task definition: Could not find container definition with matching name'); | ||
} | ||
containerDef.image = imageURI; | ||
|
||
// Check for imageURI | ||
if(imageURI) { | ||
// Insert the image URI | ||
containerDef.image = imageURI; | ||
} | ||
|
||
// Check for mergeFile | ||
if (mergeFile) { | ||
// Parse the merge file | ||
const mergeFilePath = path.isAbsolute(mergeFile) ? | ||
mergeFile : | ||
path.join(process.env.GITHUB_WORKSPACE, mergeFile); | ||
if (!fs.existsSync(mergeFilePath)) { | ||
throw new Error(`Merge file does not exist: ${mergeFile}`); | ||
} | ||
const mergeContents = require(mergeFilePath); | ||
|
||
// Merge the merge file | ||
if (!Array.isArray(mergeContents.containerDefinitions)) { | ||
throw new Error('Invalid merge fragment definition: containerDefinitions section is not present or is not an array'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer we not require the presence of |
||
} | ||
const mergeDef = mergeContents.containerDefinitions.find(function(element) { | ||
return element.name == containerName; | ||
}); | ||
if (!mergeDef) { | ||
throw new Error('Invalid merge fragment definition: Could not find container definition with matching name'); | ||
} | ||
|
||
// mergeWith contents | ||
mergeWith(containerDef, mergeDef, customizer); | ||
} | ||
|
||
// Write out a new task definition file | ||
var updatedTaskDefFile = tmp.fileSync({ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since neither input is strictly required now, can we add some debug statements to indicate which course(s) of action the workflow is taking, and throw an error if neither is provided? That way if I wanted to see what was happening I could see:
(image specified) -------> "inserting container image [IMAGE]"
(merge file specified) ---> "merging task definition fragment [FILE_NAME]"
(nothing specified) -----> ERROR: "No image or merge file specified. Please specify one or both to use this action."