From 264b0f086c593ca8b41ecf2a9b93c84fc5f2f4e1 Mon Sep 17 00:00:00 2001 From: Max Cascone Date: Fri, 16 Aug 2024 12:09:09 -0500 Subject: [PATCH] wip --- .github/workflows/configtest.yml | 36 +++++++++++------- actions/example-config-updater/action.yaml | 10 +++-- actions/example-config-updater/dist/index.js | 28 +++++++++++--- actions/example-config-updater/src/index.ts | 40 ++++++++++++++------ 4 files changed, 81 insertions(+), 33 deletions(-) diff --git a/.github/workflows/configtest.yml b/.github/workflows/configtest.yml index 8902c14..f30d66b 100644 --- a/.github/workflows/configtest.yml +++ b/.github/workflows/configtest.yml @@ -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 @@ -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: | @@ -77,7 +87,7 @@ jobs: - name: unstash action dir uses: actions/download-artifact@v4 with: - name: actions + name: actions path: actions - name: download config diff --git a/actions/example-config-updater/action.yaml b/actions/example-config-updater/action.yaml index 403f6ff..5b2c2c2 100644 --- a/actions/example-config-updater/action.yaml +++ b/actions/example-config-updater/action.yaml @@ -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: @@ -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. diff --git a/actions/example-config-updater/dist/index.js b/actions/example-config-updater/dist/index.js index 86a5d8b..5d7b580 100644 --- a/actions/example-config-updater/dist/index.js +++ b/actions/example-config-updater/dist/index.js @@ -54,19 +54,29 @@ 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]; @@ -74,11 +84,16 @@ function run() { 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)); @@ -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"); } diff --git a/actions/example-config-updater/src/index.ts b/actions/example-config-updater/src/index.ts index 0f9101b..f6d28ae 100644 --- a/actions/example-config-updater/src/index.ts +++ b/actions/example-config-updater/src/index.ts @@ -10,25 +10,34 @@ 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' @@ -36,14 +45,21 @@ export async function run() { 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 @@ -69,6 +85,8 @@ export async function run() { } + + ///////////// WRITE /////////////////// else if (mode == Mode.WRITE) { console.log("writing config.json file"); }