Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mcascone committed Aug 16, 2024
1 parent 7366957 commit 264b0f0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 33 deletions.
36 changes: 23 additions & 13 deletions .github/workflows/configtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,31 @@ jobs:
- name: init
run: echo 'starting ${{ github.job }}'

- name: checkout
- name: checkout app repo
uses: actions/checkout@v4

# should only be needed in this POC
- name: stash action dir
uses: actions/upload-artifact@v4
with:
name: actions
path: actions

- name: create config
# creates implied output: artifactId
- name: create the example config object
id: create
uses: ./actions/example-config-updater
with:
mode: create

- name: test using env var
run: |
echo "config from env: $CONFIG" >> $GITHUB_STEP_SUMMARY
- name: test using step outputs
run: |
echo "config from step: ${{ steps.create.outputs.config }}" >> $GITHUB_STEP_SUMMARY
read:
name: Read
runs-on: ubuntu-latest
Expand All @@ -48,17 +58,17 @@ jobs:
name: actions
path: actions

# - name: read config
# uses: ./actions/example-config-updater
# with:
# mode: read
# artifactId: ${{ needs.init.outputs.artifactId }}

- name: download config
id: download
uses: actions/download-artifact@v4
- name: read config
uses: ./actions/example-config-updater
with:
name: config
mode: read
artifactId: ${{ needs.init.outputs.artifactId }}

# - name: download config
# id: download
# uses: actions/download-artifact@v4
# with:
# name: config

- name: print config to summary
run: |
Expand All @@ -77,7 +87,7 @@ jobs:
- name: unstash action dir
uses: actions/download-artifact@v4
with:
name: actions
name: actions
path: actions

- name: download config
Expand Down
10 changes: 7 additions & 3 deletions actions/example-config-updater/action.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: example-config-updater

description: an example of using upload/download to pass the config object around
description: An example of using upload/download to pass the config object around.

inputs:
mode:
Expand All @@ -9,12 +9,16 @@ inputs:
default: read

artifactId:
description: the artifact id to read the config object from.
description: The artifact id to read the config object from.
required: false

config:
description: The config object to update, in JSON string format.
required: false

outputs:
config:
description: the updated config object.
description: the updated config object, in JSON string format.

artifactId:
description: the updated config object as an artifact.
Expand Down
28 changes: 22 additions & 6 deletions actions/example-config-updater/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,46 @@ function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const mode = core.getInput("mode");
const config = core.getInput("config");
const artifactClient = new artifact_1.DefaultArtifactClient();
const configFileName = "config.json";
const artifactName = "config";
const rootDir = './';
let artID = 0;
let artID;
///////// CREATE ///////////////
// create a config.json file
if (mode == Mode.CREATE) {
console.log("Creating config.json file");
const content = {
max: "cascone",
};
let content = {};
// if the config input is not null or not empty, use it
// otherwise, use a default value
if (config && config.length > 0) {
const content = JSON.parse(config);
}
else {
const content = {
max: "cascone",
};
}
console.log('content: ', content);
// write the file
// write the file to the filesystem
fs.writeFileSync(configFileName, JSON.stringify(content, 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 the json string as an env var
core.exportVariable('CONFIG', JSON.stringify(content));
// set the json string as an output
core.setOutput('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.log("reading config.json file, id: ", id);
console.debug("reading config.json file, id: ", id);
// download the artifact
// parse the file into an object
const downloadResponse = yield artifactClient.downloadArtifact(Number(id));
Expand All @@ -97,6 +112,7 @@ function run() {
const config = JSON.parse(data);
console.log('config: ', config);
}
///////////// WRITE ///////////////////
else if (mode == Mode.WRITE) {
console.log("writing config.json file");
}
Expand Down
40 changes: 29 additions & 11 deletions actions/example-config-updater/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,56 @@ enum Mode {

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

const artifactClient = new DefaultArtifactClient();
const configFileName = "config.json";
const artifactName = "config";
const rootDir = './';
let artID: number = 0;
let artID: number;

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

const content = {
max: "cascone",
};

// if the config input is not null or not empty, use it
// otherwise, use a default value
if (config && config.length > 0) {
const content = JSON.parse(config);
}
else {
const content = {
max: "cascone",
};
}
console.log('content: ', content);
// write the file

// write the file to the filesystem
fs.writeFileSync(configFileName, JSON.stringify(content, 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})`);

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

// set the json string as an output
core.setOutput('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.log("reading config.json file, id: ", id);

console.debug("reading config.json file, id: ", id);

// download the artifact
// parse the file into an object
Expand All @@ -69,6 +85,8 @@ export async function run() {


}

///////////// WRITE ///////////////////
else if (mode == Mode.WRITE) {
console.log("writing config.json file");
}
Expand Down

0 comments on commit 264b0f0

Please sign in to comment.