Skip to content

Commit

Permalink
full implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mcascone committed Aug 16, 2024
1 parent e3a3180 commit 25467da
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 54 deletions.
31 changes: 15 additions & 16 deletions .github/workflows/configtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
path: actions

- name: read config
id: read
uses: ./actions/example-config-updater
with:
mode: read
Expand All @@ -75,7 +76,7 @@ jobs:
run: |
echo 'config:' >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
cat config.json >> $GITHUB_STEP_SUMMARY
echo $CONFIG >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
Expand All @@ -91,24 +92,22 @@ jobs:
name: actions
path: actions

- name: download config
id: download
uses: actions/download-artifact@v4
- name: update config
id: read
uses: ./actions/example-config-updater
with:
name: config

- name: get the config outputs
run: |
configPath=${{ needs.read.outputs.configPath }}
configFile=$configPath/config.json
echo 'configFile: ' $configFile
mode: write
artifactId: ${{ needs.init.outputs.artifactId }}
config: '{"neo": "anderson"}'

ls -R >> $GITHUB_STEP_SUMMARY
- name: print config to summary
run: |
echo 'config:' >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo $CONFIG >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
cat $configFile >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
# See here for deleting artifacts: https://github.com/actions/upload-artifact/issues/550
56 changes: 40 additions & 16 deletions actions/example-config-updater/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var Mode;
(function (Mode) {
Mode["CREATE"] = "create";
Mode["READ"] = "read";
Mode["UPDATE"] = "update";
Mode["WRITE"] = "write";
})(Mode || (Mode = {}));
;
Expand All @@ -55,16 +56,17 @@ function run() {
try {
const mode = core.getInput("mode");
const config = core.getInput("config");
const id = core.getInput("artifactId");
const artifactClient = new artifact_1.DefaultArtifactClient();
const artifactName = "config";
const configFileName = artifactName + ".json";
const rootDir = './';
let artID;
let content = {};
///////// CREATE ///////////////
// create a config.json file
if (mode == Mode.CREATE) {
console.log("Creating config.json file");
let content = {};
// if the config input is not null or not empty, use it
// otherwise, use a default value
if (config && config.length > 0) {
Expand All @@ -91,32 +93,54 @@ function run() {
// set the json string as an env var
core.exportVariable('CONFIG', JSON.stringify(content));
}
///////////// READ ///////////////////
// READ the artifact into a file and create an object
else if (mode == Mode.READ) {
const id = core.getInput("artifactId");
console.debug("reading config.json file, id: ", id);
///////////// READ/WRITE/UPDATE ////////////////////////////
// first, READ the artifact into a file and create an object
else if ([Mode.READ, Mode.WRITE, Mode.UPDATE].includes(mode)) {
// download the artifact
// parse the file into an object
// downloadResponse is the downloadPath (the parent path, not the file itself)
const downloadResponse = yield artifactClient.downloadArtifact(Number(id));
console.log('downloadResponse: ', downloadResponse);
const file = downloadResponse.downloadPath + '/' + configFileName;
console.log('file: ', file);
let data;
console.log('file path: ', file);
// read the file
let data;
if (file) {
data = fs.readFileSync(file, 'utf8'); // Assign a value to the variable inside the if block
data = fs.readFileSync(file, 'utf8');
}
else {
throw new Error("File path is undefined.");
}
// parse the file into an object
const config = JSON.parse(data);
console.log('config: ', config);
}
///////////// WRITE ///////////////////
else if (mode == Mode.WRITE) {
console.log("writing config.json file");
const content = JSON.parse(data);
const contentStr = JSON.stringify(config, null, 2);
console.log('config from file: ', contentStr);
// set the json string as an output
core.setOutput('content', contentStr);
// set the json string as an env var
core.exportVariable('CONFIG', contentStr);
///////////// WRITE/UPDATE ///////////////////
// if mode is write or update
if (mode == Mode.WRITE || mode == Mode.UPDATE) {
console.log("updating and writing config");
// create an object with the contents of the input config
const newContent = JSON.parse(config);
// update the existing object with the new inputs
const updatedContent = Object.assign(Object.assign({}, content), newContent);
// write the file to the filesystem
fs.writeFileSync(configFileName, JSON.stringify(updatedContent, null, 2));
// upload file 'config.json' as an artifact named 'config'
const files = [configFileName];
const { id, size } = yield artifactClient.uploadArtifact(artifactName, files, rootDir);
artID = id !== null && id !== void 0 ? id : 0;
core.setOutput("artifactId", artID);
console.log(`Created artifact with id: ${artID} (bytes: ${size})`);
// set artifact id as an output
core.setOutput("artifactId", artID);
// set the json string as an output
core.setOutput('config', JSON.stringify(updatedContent));
// set the json string as an env var
core.exportVariable('CONFIG', JSON.stringify(updatedContent));
}
}
else {
throw new Error("Invalid mode: " + mode);
Expand Down
71 changes: 49 additions & 22 deletions actions/example-config-updater/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@ import { DefaultArtifactClient } from "@actions/artifact";
enum Mode {
CREATE = "create",
READ = "read",
WRITE = "write"
UPDATE = "update",
WRITE = "write",
};

export async function run() {
try {
const mode = core.getInput("mode");
const config = core.getInput("config");
const id = core.getInput("artifactId");

const artifactClient = new DefaultArtifactClient();
const artifactName = "config";
const configFileName = artifactName + ".json";
const rootDir = './';

let artID: number;
let content = {};

///////// CREATE ///////////////
// create a config.json file
if (mode == Mode.CREATE) {
console.log("Creating config.json file");
let content = {};


// if the config input is not null or not empty, use it
// otherwise, use a default value
if (config && config.length > 0) {
Expand Down Expand Up @@ -57,46 +60,70 @@ export async function run() {
core.exportVariable('CONFIG', JSON.stringify(content));
}

///////////// READ ///////////////////
// READ the artifact into a file and create an object
else if (mode == Mode.READ) {
const id = core.getInput("artifactId");

console.debug("reading config.json file, id: ", id);
///////////// READ/WRITE/UPDATE ////////////////////////////
// first, READ the artifact into a file and create an object
else if ([Mode.READ, Mode.WRITE, Mode.UPDATE].includes(mode as Mode)) {

// download the artifact
// parse the file into an object
// downloadResponse is the downloadPath (the parent path, not the file itself)
const downloadResponse = await artifactClient.downloadArtifact(Number(id));

console.log('downloadResponse: ', downloadResponse);
const file = downloadResponse.downloadPath + '/' + configFileName;
console.log('file: ', file);
console.log('file path: ', file);

let data: string;

// read the file
let data: string;
if (file) {
data = fs.readFileSync(file, 'utf8'); // Assign a value to the variable inside the if block
data = fs.readFileSync(file, 'utf8');
} else {
throw new Error("File path is undefined.");
}

// parse the file into an object
const config = JSON.parse(data);
const content = JSON.parse(data);
const contentStr = JSON.stringify(config, null, 2);
console.log('config from file: ', contentStr);

console.log('config: ', config);
// set the json string as an output
core.setOutput('content', contentStr);

// set the json string as an env var
core.exportVariable('CONFIG', contentStr);

}
///////////// WRITE/UPDATE ///////////////////
// if mode is write or update
if (mode == Mode.WRITE || mode == Mode.UPDATE) {
console.log("updating and writing config");

// create an object with the contents of the input config
const newContent = JSON.parse(config);

// update the existing object with the new inputs
const updatedContent = { ...content, ...newContent };

// write the file to the filesystem
fs.writeFileSync(configFileName, JSON.stringify(updatedContent, null, 2));

// upload file 'config.json' as an artifact named 'config'
const files = [configFileName];
const { id, size } = await artifactClient.uploadArtifact(artifactName, files, rootDir);
artID = id ?? 0;
core.setOutput("artifactId", artID);
console.log(`Created artifact with id: ${artID} (bytes: ${size})`);

///////////// WRITE ///////////////////
else if (mode == Mode.WRITE) {
console.log("writing config.json file");
// set artifact id as an output
core.setOutput("artifactId", artID);

// set the json string as an output
core.setOutput('config', JSON.stringify(updatedContent));

// set the json string as an env var
core.exportVariable('CONFIG', JSON.stringify(updatedContent));
}
}
else {
throw new Error("Invalid mode: " + mode);
}

} catch (error) {
core.setFailed(error.message);
}
Expand Down

0 comments on commit 25467da

Please sign in to comment.