Skip to content
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

Changed printing of simulation results #397

Merged
merged 8 commits into from
Nov 5, 2024
71 changes: 47 additions & 24 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,25 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers
uses: actions/cache@v4
with:
path: |
/tmp/.buildx-cache/cache/test
/tmp/.buildx-cache/cache/latest
/tmp/.buildx-cache/cache-new/test
/tmp/.buildx-cache/cache-new/latest
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-

- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Create cache directory
run: |
mkdir -p ./.buildx-cache/cache
mkdir ./.buildx-cache/cache-new
mkdir ./.buildx-cache/cache/test
mkdir ./.buildx-cache/cache/latest
mkdir ./.buildx-cache/cache-new/test
mkdir ./.buildx-cache/cache-new/latest

- name: Test build
if: github.event_name == 'pull_request'
uses: docker/build-push-action@v3
Expand All @@ -59,8 +62,8 @@ jobs:
file: ./build/docker/agent/Dockerfile
load: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}:test
cache-from: type=local,src=./.buildx-cache/cache/test/
cache-to: type=local,dest=./.buildx-cache/cache-new/test/,mode=max
cache-from: type=local,src=/tmp/.buildx-cache/cache/test/
cache-to: type=local,dest=/tmp/.buildx-cache/cache-new/test/,mode=max
build-args: |
USERNAME=paf
USER_UID=1000
Expand All @@ -76,31 +79,51 @@ jobs:
push: true
# tag 'latest' and version on push to main, otherwise use the commit hash
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
cache-from: type=local,src=./.buildx-cache/cache/latest/
cache-to: type=local,dest=./.buildx-cache/cache-new/latest/,mode=max
cache-from: type=local,src=/tmp/.buildx-cache/cache/latest/
cache-to: type=local,dest=/tmp/.buildx-cache/cache-new/latest/,mode=max
build-args: |
USERNAME=paf
USER_UID=1000
USER_GID=1000

- name: Save PR ID
- name: Save pull request artifact
if: github.event_name == 'pull_request'
env:
PR_ID: ${{ github.event.number }}
run: |
mkdir -p ./pr
echo $PR_ID > ./pr/pr_id
mkdir -p ./artifact
printf '{
"is_pr": true,
"pr_id": $PR_ID
}' >> ./artifact/artifact.json
Comment on lines +89 to +98
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix shell script to properly expand environment variable.

The logic is correct, but there's a potential issue with variable expansion due to single quotes.

Apply this fix to ensure proper variable expansion:

       - name: Save pull request artifact
         if: github.event_name == 'pull_request'
         env:
           PR_ID: ${{ github.event.number }}
         run: |
           mkdir -p ./artifact
-          printf '{
-            "is_pr": true,
-            "pr_id": $PR_ID
-          }' >> ./artifact/artifact.json
+          cat << EOF > ./artifact/artifact.json
+          {
+            "is_pr": true,
+            "pr_id": $PR_ID
+          }
+          EOF
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- name: Save pull request artifact
if: github.event_name == 'pull_request'
env:
PR_ID: ${{ github.event.number }}
run: |
mkdir -p ./pr
echo $PR_ID > ./pr/pr_id
mkdir -p ./artifact
printf '{
"is_pr": true,
"pr_id": $PR_ID
}' >> ./artifact/artifact.json
- name: Save pull request artifact
if: github.event_name == 'pull_request'
env:
PR_ID: ${{ github.event.number }}
run: |
mkdir -p ./artifact
cat << EOF > ./artifact/artifact.json
{
"is_pr": true,
"pr_id": $PR_ID
}
EOF
🧰 Tools
🪛 actionlint

99-99: shellcheck reported issue in this script: SC2016:info:2:8: Expressions don't expand in single quotes, use double quotes for that

(shellcheck)


- name: Upload PR ID
if: github.event_name == 'pull_request'
- name: Save merge artifact
if: github.event_name != 'pull_request'
run: |
mkdir -p ./artifact
printf '{
"is_pr": false,
"pr_id": -1
}' >> ./artifact/artifact.json

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: pr_id
path: pr/
name: artifact.json
path: ./artifact/
retention-days: 1

- name: Clean up cache
- name: Clean up PR cache
if: github.event_name == 'pull_request'
run: |
rm -rf /tmp/.buildx-cache/cache/test
mv /tmp/.buildx-cache/cache-new/test /tmp/.buildx-cache/cache/test

- name: Clean up merge cache
if: github.event_name != 'pull_request'
run: |
rm -rf ./.buildx-cache/cache/test
rm -rf ./.buildx-cache/cache/latest
mv ./.buildx-cache/cache-new ./.buildx-cache/cache
rm -rf /tmp/.buildx-cache/cache/latest
mv /tmp/.buildx-cache/cache-new/latest /tmp/.buildx-cache/cache/latest

- name: Prune all images older than 1 days from self-hosted runner
run: docker image prune -a --filter "until=24h"
53 changes: 30 additions & 23 deletions .github/workflows/drive.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ jobs:
run: |
echo "AGENT_VERSION=${AGENT_VERSION}"
echo "COMPOSE_FILE=${COMPOSE_FILE}"
- name: 'Download artifact (PR ID)'
if: github.event_name == 'pull_request'
- name: Download artifact
uses: actions/github-script@v6
with:
script: |
Expand All @@ -31,10 +30,10 @@ jobs:
run_id: context.payload.workflow_run.id,
});
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr_id"
return artifact.name == "artifact.json"
})[0];
if (!matchArtifact) {
core.setFailed('No pr_id artifact found from the build workflow');
core.setFailed('No artifact found from the build workflow');
return;
}
let download = await github.rest.actions.downloadArtifact({
Expand All @@ -44,23 +43,20 @@ jobs:
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr_id.zip`, Buffer.from(download.data));
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/artifact.zip`, Buffer.from(download.data));

- name: 'Unzip artifact (PR ID)'
if: github.event_name == 'pull_request'
- name: Unzip artifact
run: |
unzip pr_id.zip
unzip artifact.zip

- name: Check PR ID
if: github.event_name == 'pull_request'
- name: Return artifact JSON
id: return-artifact-json
uses: actions/github-script@v6
with:
script: |
let issue_number = fs.readFileSync('./pr_id');
if (!issue_number || isNaN(Number(issue_number))) {
core.setFailed(`Invalid PR ID: ${prIdContent}`);
return;
}
let fs = require('fs');
let data = JSON.parse(fs.readFileSync('./artifacts/artifacts.json'));
return data;

- name: Run docker-compose
run: |
Expand All @@ -73,12 +69,10 @@ jobs:
if: always()
run: docker compose down -v
# add rendered JSON as comment to the pull request
- name: Add simulation results as comment
if: github.event_name == 'pull_request'
- name: Create simulation results table
id: simulation-results
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
# this script reads the simulation_results.json and creates a comment on the pull request with the results.
script: |
const fs = require('fs');
// read the simulation results
Expand All @@ -94,13 +88,26 @@ jobs:
let resultsTableDivider = `| --- | --- |`;
// add everything to the resultsTable
resultsTable = resultsTableHeader + '\n' + resultsTableDivider + '\n' + resultsTable.join('\n');
return resultsTable;
result-encoding: string
- name: Print simulation results
run: |
echo "Simulation results\n"
echo "${{ steps.simulation-results.outputs.result }}"
Comment on lines +94 to +96
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix echo command to properly handle escape sequences.

The current echo command may not properly expand the newline character.

Update the command to use printf:

-  echo "Simulation results\n"
-  echo "${{ steps.simulation-results.outputs.result }}"
+  printf "Simulation results\n\n%s\n" "${{ steps.simulation-results.outputs.result }}"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
run: |
echo "Simulation results\n"
echo "${{ steps.simulation-results.outputs.result }}"
run: |
printf "Simulation results\n\n%s\n" "${{ steps.simulation-results.outputs.result }}"
🧰 Tools
🪛 actionlint

94-94: shellcheck reported issue in this script: SC2028:info:1:6: echo may not expand escape sequences. Use printf

(shellcheck)

- name: Add simulation results as comment
if: ${{ steps.return-artifact-json.outputs.result.is_pr }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
# this script reads the simulation_results.json and creates a comment on the pull request with the results.
script: |
let issue_number = Number(${{ steps.return-artifact-json.outputs.pr_id }});
// add the results as a comment to the pull request
let issue_number = Number(fs.readFileSync('./pr_id'));
github.rest.issues.createComment({
issue_number: issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "## Simulation results\n" + resultsTable
body: "## Simulation results\n" + ${{ steps.simulation-results.outputs.result }}
});
- name: Prune all images older than 30 days from self-hosted runner
run: docker image prune -a --force --filter "until=720h"
- name: Prune all images older than 1 days from self-hosted runner
run: docker image prune -a --filter "until=24h"
Loading