Skip to content

Commit

Permalink
fix(actions/matrix-outputs): make it works on window
Browse files Browse the repository at this point in the history
  • Loading branch information
neilime committed Oct 16, 2023
1 parent 43352da commit 548ac56
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 36 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/__test-action-matrix-outputs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ on:

jobs:
tests:
name: Tests for set/get matrix outputs
runs-on: ubuntu-latest
name: Tests for set/get matrix outputs
strategy:
matrix:
os:
- ubuntu-latest
- windows-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/greetings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ on:
required: false
secrets:
github-token:
description: 'Token for the repository. See https://github.com/actions/first-interaction#usage'
description: "Token for the repository. See https://github.com/actions/first-interaction#usage"

jobs:
greeting:
runs-on: ${{ fromJson(inputs.runs-on) }}
runs-on: ${{ inputs.runs-on && fromJson(inputs.runs-on) || 'ubuntu-latest' }}
steps:
- uses: actions/first-interaction@v1
with:
Expand Down
21 changes: 14 additions & 7 deletions actions/get-matrix-outputs/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@ runs:
path: ${{ steps.prepare-download.outputs.artifact-path }}

- id: read-artifacts
shell: bash
run: |
RESULT=$(jq -sc "." ${{ steps.prepare-download.outputs.artifact-path }}/*.json)
echo "result<<EOF" >> "$GITHUB_OUTPUT" && echo "$RESULT" >> "$GITHUB_OUTPUT" && echo "EOF" >> "$GITHUB_OUTPUT"
uses: actions/github-script@v6
with:
script: |
const { readFileSync } = require('fs');
- if: ${{ inputs.remove-artifact == 'true' }}
shell: bash
run: rm -rf ${{ steps.prepare-download.outputs.artifact-path }}
const artifactPath = `${{ steps.prepare-download.outputs.artifact-path }}`;
const globber = await glob.create(`${artifactPath}/*.json`, {followSymbolicLinks: false});
const artifactFiles = await globber.glob();
const result = artifactFiles.map(file => JSON.parse(readFileSync(file, 'utf8')));
core.setOutput("result", JSON.stringify(result));
if(core.getBooleanInput('remove-artifact')) {
await io.rmRF(artifactPath);
}
- if: ${{ inputs.remove-artifact == 'true' }}
uses: geekyeggo/delete-artifact@v2
Expand Down
55 changes: 30 additions & 25 deletions actions/set-matrix-output/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,36 @@ runs:
using: "composite"
steps:
- id: prepare-upload
shell: bash
run: |
# Define a unique artifact name for the current workflow
ARTIFACT_NAME="${{ github.run_id }}-${{ github.run_number }}-${{ inputs.artifact-name }}"
echo "artifact-name=$ARTIFACT_NAME" >> "$GITHUB_OUTPUT"
ARTIFACT_PATH="/tmp/$ARTIFACT_NAME"
echo "artifact-path=$ARTIFACT_PATH" >> "$GITHUB_OUTPUT"
mkdir -p "$ARTIFACT_PATH"
MAX_ATTEMPTS=10
MATRIX_OUTPUT_FILE=""
for i in $(seq 1 $MAX_ATTEMPTS); do
MATRIX_OUTPUT_FILE="$ARTIFACT_PATH/${{ inputs.artifact-name }}-$(uuidgen).json"
if [ ! -f "$MATRIX_OUTPUT_FILE" ]; then
break
fi
MATRIX_OUTPUT_FILE=""
done
if [ -z "$MATRIX_OUTPUT_FILE" ]; then
echo "Failed to find unique file name after $MAX_ATTEMPTS attempts"
exit 1
fi
echo '${{ inputs.value }}' > "$MATRIX_OUTPUT_FILE"
uses: actions/github-script@v6
with:
script: |
const { join } = require('path');
const { existsSync, writeFileSync } = require('fs');
const { randomUUID } = require('crypto');
const artifactName = `${{ github.run_id }}-${{ github.run_number }}-${{ inputs.artifact-name }}`;
core.setOutput("artifact-name", artifactName);
const artifactPath = join("/tmp",artifactName);
core.setOutput("artifact-path", artifactPath);
await io.mkdirP(artifactPath);
const maxAttempts = 10;
let matrixOutputFile = '';
for (let i = 1; i <= maxAttempts; i++) {
const uniquid = randomUUID();
matrixOutputFile = join(artifactPath, `${artifactName}-${uniquid}.json`);
if (!existsSync(matrixOutputFile)) {
break;
}
matrixOutputFile = '';
}
if (!matrixOutputFile) {
core.setFailed(`Failed to find unique file name after ${maxAttempts} attempts`);
}
writeFileSync(matrixOutputFile, `${{ inputs.value }}`);
- uses: actions/upload-artifact@v3
with:
Expand Down

0 comments on commit 548ac56

Please sign in to comment.