Skip to content

Commit

Permalink
Merge branch 'main' into artifact-size
Browse files Browse the repository at this point in the history
Change-Id: I14793250ac2cb4a0ed824ee173cd13602189aec5
  • Loading branch information
haozheng-cobalt committed Aug 9, 2024
2 parents 63d835e + d4ec2d6 commit 31a6339
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 49 deletions.
116 changes: 90 additions & 26 deletions .github/actions/check_artifact_size/action.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
name: Check Artifact Size
description: Check if the size of the newly created artifact specified by path is larger than the size of the previously uploaded artifact specified by name by the threshold.
description: Check if the increase in artifact size exceeds the threshold, and if so, apply a label to the pull request.
inputs:
workflow:
description: "Workflow to check artifact binary size for."
required: true
name:
description: "Name of the latest uploaded artifact."
description: "Name of the uploaded artifact, artifact is a zip file that can contain more than one binary"
required: true
path:
description: "Path to the artifact being checked."
description: "Path to the newly created binary artifacts being checked."
required: true
threshold:
description: "Threshold for artifact binary size increase percentage."
thresholds:
description: "Thresholds is a JSON-formatted string that specifies the maximum permissible percentage increase in the size of each respective binary artifact."
required: true
token:
description: "Github token needed for downloading artifacts."
required: true
runs:
using: "composite"
steps:
- name: Check Size
- name: 'Download artifact from main branch'
id: download-artifact
uses: actions/github-script@v6
with:
github-token: ${{inputs.token}}
script: |
// Get the latest successful workflow run.
const fs = require('fs');
const path = require('path');
// Get the latest successful workflow run on the main branch.
const workflowRuns = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -32,7 +40,7 @@ runs:
const latestRun = workflowRuns.data.workflow_runs[0].id;
// Get the artifact uploaded on the latest successful workflow run.
// Get the artifact uploaded on the latest successful workflow run on the main branch.
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -44,30 +52,86 @@ runs:
});
if (matchArtifacts.length == 1) {
console.log(`Checking artifact size of ${{ inputs.name }} on workflow ${{ inputs.workflow }}.`);
console.log(`Found the latest uploaded artifact ${{ inputs.name }} on the main branch.`);
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifacts[0].id,
archive_format: 'zip',
});
const downloadDir = path.join(process.env.GITHUB_WORKSPACE, 'artifact_tmp');
fs.mkdirSync(downloadDir);
fs.writeFileSync(path.join(downloadDir, `${{ inputs.name }}.zip`), Buffer.from(download.data));
core.setOutput("downloadDir", downloadDir);
} else {
core.setFailed(`Expected one artifact with name ${{ inputs.name }}. Found ${matchArtifacts.length}.`);
}
- name: 'Unzip artifact from main branch'
id: unzip-downloaded-artifact
shell: bash
run: |
unzip "${{ steps.download-artifact.outputs.downloadDir }}/${{ inputs.name }}.zip" -d "${{ steps.download-artifact.outputs.downloadDir }}"
- name: 'Check new artifact size against main branch'
id: check-artifact-size
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const path = require('path');
const fileSizeThresholds = JSON.parse('${{ inputs.thresholds }}');
for (let file in fileSizeThresholds) {
console.log(`Checking file size of ${file}.`);
const cur_size = matchArtifacts[0].size_in_bytes;
const downloadFilePath = path.join('${{ steps.download-artifact.outputs.downloadDir }}', file);
if (!fs.existsSync(downloadFilePath)) {
console.error(`File ${file} was not uploaded to the main branch.`);
continue;
}
const filePath = path.join(process.env.GITHUB_WORKSPACE, '${{ inputs.path }}', file);
if (!fs.existsSync(filePath)) {
console.error(`File ${filePath} was not created in the current workflow run.`);
continue;
}
const fs = require('fs');
const path = require('path');
const filePath = path.join(process.env.GITHUB_WORKSPACE, '${{ inputs.path }}');
const stats = fs.statSync(filePath);
const new_size = stats.size;
const oldStats = fs.statSync(downloadFilePath);
const oldSize = oldStats.size;
const newStats = fs.statSync(filePath);
const newSize = newStats.size;
console.log(`Artifact size on the main branch is ${cur_size / 1024 /1024}MB, new artifact size to be uploaded is ${new_size / 1024 / 1024}MB.`);
console.log(`Latest uploaded artifact size on the main branch is ${oldSize / 1024}kB, new artifact size generated in this PR is ${newSize / 1024}kB.`);
const delta_size = new_size - cur_size;
const delta_threshold = (Math.abs(delta_size) / cur_size * 100).toFixed(4);
const deltaSize = newSize - oldSize;
const deltaThreshold = (Math.abs(deltaSize) / oldSize * 100).toFixed(4);
if (delta_size < 0) {
console.log(`Artifact size is decreased by ${delta_threshold}%.`);
if (deltaSize < 0) {
console.log(`Artifact size is decreased by ${Math.abs(deltaSize)} (${deltaThreshold}%).`);
} else {
console.log(`Artifact size is increased by ${delta_threshold}%.`);
if (delta_threshold > ${{ inputs.threshold }}) {
const threshold = (${{ inputs.threshold }} * 100).toFixed(4);
core.setFailed(`Artifact size increase is over threshold ${threshold}%.`);
console.log(`Artifact size is increased by ${deltaSize} (${deltaThreshold}%).`);
if (deltaThreshold > fileSizeThresholds[file]) {
const threshold = (fileSizeThresholds[file] * 100).toFixed(4);
console.error(`Artifact size increase exceeds threshold ${threshold}%.`);
core.setOutput("addLabel", true);
}
}
} else {
core.setFailed(`Expected one artifact with name ${{ inputs.name }}. Found ${matchArtifacts.length}.`);
}
- name: 'Remove downloaded artifact'
id: remove-downloaded-artifact
shell: bash
run: rm -r "${{ steps.download-artifact.outputs.downloadDir }}"
- name: 'Add label for artifact size increase violation'
id: add-label
if: ${{ steps.check-artifact-size.outputs.addLabel }}
shell: bash
run: |
curl -s -X POST -H "Authorization: token ${{ inputs.token }}" \
-H "Accept: application/vnd.github.v3+json" \
-d '["artifact size increase violation"]' \
"https://api.github.com/repos/${{ github.event.repository.full_name }}/issues/${{ github.event.number }}/labels"
16 changes: 8 additions & 8 deletions .github/workflows/evergreen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ jobs:
platform: evergreen-x64
nightly: ${{ github.event.inputs.nightly }}
run_api_leak_detector: true
check_artifact_size: ${{ github.workflow }}
keep_artifacts: libcobalt.so
keep_artifacts: install/lib/libcobalt.*
artifact_size_increase_percentage_thresholds: '{"install/lib/libcobalt.so": 0.02, "install/lib/libcobalt.lz4": 0.02}'
evergreen-arm-hardfp:
uses: ./.github/workflows/main.yaml
permissions:
Expand All @@ -42,8 +42,8 @@ jobs:
platform: evergreen-arm-hardfp
nightly: ${{ github.event.inputs.nightly }}
run_api_leak_detector: true
check_artifact_size: ${{ github.workflow }}
keep_artifacts: libcobalt.so
keep_artifacts: install/lib/libcobalt.*
artifact_size_increase_percentage_thresholds: '{"install/lib/libcobalt.so": 0.02, "install/lib/libcobalt.lz4": 0.02}'
evergreen-arm-softfp:
uses: ./.github/workflows/main.yaml
permissions:
Expand All @@ -53,8 +53,8 @@ jobs:
platform: evergreen-arm-softfp
nightly: ${{ github.event.inputs.nightly }}
run_api_leak_detector: true
check_artifact_size: ${{ github.workflow }}
keep_artifacts: libcobalt.so
keep_artifacts: install/lib/libcobalt.*
artifact_size_increase_percentage_thresholds: '{"install/lib/libcobalt.so": 0.02, "install/lib/libcobalt.lz4": 0.02}'
evergreen-arm64:
uses: ./.github/workflows/main.yaml
permissions:
Expand All @@ -64,5 +64,5 @@ jobs:
platform: evergreen-arm64
nightly: ${{ github.event.inputs.nightly }}
run_api_leak_detector: true
check_artifact_size: ${{ github.workflow }}
keep_artifacts: libcobalt.so
keep_artifacts: install/lib/libcobalt.*
artifact_size_increase_percentage_thresholds: '{"install/lib/libcobalt.so": 0.02, "install/lib/libcobalt.lz4": 0.02}'
27 changes: 12 additions & 15 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,15 @@ on:
type: boolean
default: false
keep_artifacts:
description: 'Which artifacts to keep for releases'
description: 'Which artifacts to keep for releases.'
required: false
type: string
default: ''
check_artifact_size:
description: 'Which workflow to check artifact binary size'
artifact_size_increase_percentage_thresholds:
description: 'Threshold for artifact binary size increase percentage.'
required: false
type: string
default: ''
artifact_size_increase_percentage_threshold:
description: 'Threshold for artifact binary size increase percentage'
required: false
type: number
default: 0.02
default: ""

# Global env vars.
env:
Expand Down Expand Up @@ -269,18 +264,20 @@ jobs:
uses: ./.github/actions/build
- name: 'Check Artifact Size'
uses: ./.github/actions/check_artifact_size
if: ${{ (inputs.check_artifact_size && needs.initialize.outputs.evergreen_loader == 'null') || (inputs.check_artifact_size && needs.initialize.outputs.evergreen_loader != 'null' && matrix.sb_api_version == '') }}
if: ${{ inputs.artifact_size_increase_percentage_thresholds }}
continue-on-error: true # Ignore this step if check artifact size failed.
with:
workflow: ${{ inputs.check_artifact_size }}
workflow: ${{ github.workflow }}
name: ${{ matrix.platform }}-${{ matrix.config }}
path: out/${{ matrix.platform }}_${{ matrix.config }}/${{ inputs.keep_artifacts }}
threshold: ${{ inputs.artifact_size_increase_percentage_threshold }}
path: out/${{ matrix.target_platform }}_${{ matrix.config }}
thresholds: ${{ inputs.artifact_size_increase_percentage_thresholds }}
token: ${{ secrets.GITHUB_TOKEN }}
- name: 'Upload Artifact'
uses: actions/upload-artifact@v4
if: ${{ (inputs.keep_artifacts && needs.initialize.outputs.evergreen_loader == 'null') || (inputs.keep_artifacts && needs.initialize.outputs.evergreen_loader != 'null' && matrix.sb_api_version == '') }}
if: ${{ inputs.keep_artifacts }}
with:
name: ${{ matrix.platform }}-${{ matrix.config }}
path: out/${{ matrix.platform }}_${{ matrix.config }}/${{ inputs.keep_artifacts }}
path: out/${{ matrix.target_platform }}_${{ matrix.config }}/${{ inputs.keep_artifacts }}
retention-days: 7
compression-level: 0 # We expect kept artifacts to be already compressed
if-no-files-found: error
Expand Down
3 changes: 3 additions & 0 deletions cobalt/loader/cors_preflight.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ bool CORSPreflight::IsPreflightNeeded() {
if (force_preflight_) {
return true;
}
if (cors_policy_ == network::kCORSOptional) {
return false;
}
// Preflight is not needed if the request method is CORS-safelisted request
// method and all headers are CORS-safelisted request-header.
std::vector<std::string> unsafe_headers;
Expand Down

0 comments on commit 31a6339

Please sign in to comment.