Skip to content

Commit

Permalink
feat: artifact size metrics (#1290)
Browse files Browse the repository at this point in the history
  • Loading branch information
0marperez authored Apr 29, 2024
1 parent c0099ac commit 2c51199
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup-build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ runs:
with:
path: 'aws-kotlin-repo-tools'
repository: 'awslabs/aws-kotlin-repo-tools'
ref: '0.2.3'
ref: '0.4.2'
sparse-checkout: |
.github
- name: Checkout smithy-kotlin
Expand Down
46 changes: 38 additions & 8 deletions .github/scripts/run-codebuild-batch-job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ error_exit() {
PROJECT_NAME=gh-aws-sdk-kotlin-svc-check-batch
# get the source version to be built (defaults to main branch if not specified)
SOURCE_VERSION=main
SDK_PR=""
SMITHY_PR=""
GITHUB_RELEASE=""
GITHUB_PULL_REQUEST_NUMBER=""
GITHUB_REPOSITORY_NO_ORG=""
EXTERNAL_CONTRIBUTOR_SDK_PR=""
EXTERNAL_CONTRIBUTOR_SMITHY_PR=""

while [[ $# -gt 0 ]]; do
case "$1" in
Expand All @@ -29,19 +32,43 @@ while [[ $# -gt 0 ]]; do
shift 2
fi
;;
--sdk-pr)
--release)
if [[ "$2" == --* ]]; then
shift 1
else
SDK_PR="$2"
GITHUB_RELEASE="$2"
shift 2
fi
;;
--smithy-pr)
--pr-number)
if [[ "$2" == --* ]]; then
shift 1
else
GITHUB_PULL_REQUEST_NUMBER="$2"
shift 2
fi
;;
--repository)
if [[ "$2" == --* ]]; then
shift 1
else
GITHUB_REPOSITORY_NO_ORG="$2"
shift 2
fi
;;
--external-contributor-sdk-pr-number)
if [[ "$2" == --* ]]; then
shift 1
else
EXTERNAL_CONTRIBUTOR_SDK_PR="$2"
shift 2
fi
;;
--external-contributor-smithy-pr-number)
if [[ "$2" == --* || -z "$2" ]]; then
shift 1
else
SMITHY_PR="$2"
EXTERNAL_CONTRIBUTOR_SMITHY_PR="$2"
shift 2
fi
;;
Expand All @@ -52,8 +79,11 @@ while [[ $# -gt 0 ]]; do
esac
done

export SDK_PR
export SMITHY_PR
export GITHUB_RELEASE
export GITHUB_PULL_REQUEST_NUMBER
export GITHUB_REPOSITORY_NO_ORG
export EXTERNAL_CONTRIBUTOR_SDK_PR
export EXTERNAL_CONTRIBUTOR_SMITHY_PR

echo "Starting CodeBuild project ${PROJECT_NAME}"

Expand Down
152 changes: 146 additions & 6 deletions .github/workflows/codebuild-ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: AWS CodeBuild CI

on:
release:
types: [ published ]
pull_request:
branches:
- main
types: [ opened, synchronize, reopened, labeled, unlabeled ]
branches: [ main ]
workflow_dispatch:
inputs:
aws-sdk-kotlin-pr:
Expand All @@ -27,6 +29,7 @@ env:
permissions:
id-token: write
contents: read
pull-requests: write

# Allow one instance of this workflow per pull request, and cancel older runs when new changes are pushed
concurrency:
Expand All @@ -35,6 +38,7 @@ concurrency:

jobs:
e2e-tests:
if: github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- name: Verify PRs are not running malicious code
Expand Down Expand Up @@ -63,7 +67,8 @@ jobs:
aws codebuild stop-build --id $BUILD_ID
fi
service-check-batch:
service-check-batch-and-artifact-size-metrics:
if: github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- name: Verify PRs are not running malicious code
Expand All @@ -78,14 +83,143 @@ jobs:
with:
role-to-assume: ${{ secrets.CI_AWS_ROLE_ARN }}
aws-region: us-west-2
- name: Run Service Check Batch
- name: Run Service Check Batch and Calculate Artifact Size Metrics
id: svc-check-batch
run: |
REPOSITORY=$(echo ${{ github.repository }} | cut -d '/' -f 2)
PULL_REQUEST=""
INTERNAL=${{ github.event.number }}
EXTERNAL=${{ inputs.aws-sdk-kotlin-pr }}
if [ ! -z "$INTERNAL" ]; then
PULL_REQUEST=${{ github.event.number }}
echo "Identified this an internal pull request build: $PULL_REQUEST"
elif [ ! -z "$EXTERNAL" ]; then
PULL_REQUEST=${{ inputs.aws-sdk-kotlin-pr }}
echo "Identified this as an external pull request build: $PULL_REQUEST"
else
echo "Couldn't identify this as an internal or external pull request"
exit 1
fi
.github/scripts/run-codebuild-batch-job.sh \
--project gh-aws-sdk-kotlin-svc-check-batch \
--source ${{ github.event.pull_request.head.sha }} \
--pr-number $PULL_REQUEST \
--repository $REPOSITORY \
--external-contributor-sdk-pr-number ${{ inputs.aws-sdk-kotlin-pr }} \
--external-contributor-smithy-pr-number ${{ inputs.smithy-kotlin-pr }}
- name: Cancel build
if: ${{ cancelled() }}
env:
BUILD_ID: ${{ steps.svc-check-batch.outputs.aws-build-id }}
run: |
if [ ! -z "$BUILD_ID" ]; then
echo "cancelling in-progress batch build: id=$BUILD_ID"
aws codebuild stop-build --id $BUILD_ID
fi
- name: Collect Artifact Size Metrics
run: |
PULL_REQUEST=""
INTERNAL=${{ github.event.number }}
EXTERNAL=${{ inputs.aws-sdk-kotlin-pr }}
if [ ! -z "$INTERNAL" ]; then
PULL_REQUEST=${{ github.event.number }}
echo "Identified this an internal pull request build: $PULL_REQUEST"
elif [ ! -z "$EXTERNAL" ]; then
PULL_REQUEST=${{ inputs.aws-sdk-kotlin-pr }}
echo "Identified this as an external pull request build: $PULL_REQUEST"
else
echo "Couldn't identify this as an internal or external pull request"
exit 1
fi
./gradlew collectDelegatedArtifactSizeMetrics -PpullRequest=$PULL_REQUEST
- name: Analyze Artifact Size Metrics
run: ./gradlew analyzeArtifactSizeMetrics
- name: Show Results
uses: actions/github-script@v7
with:
script: |
const getComments =
`query {
repository(owner:"${context.repo.owner}", name:"${context.repo.repo}"){
pullRequest(number: ${context.issue.number}) {
id
comments(last:100) {
nodes {
id
body
author {
login
}
isMinimized
}
}
}
}
}`
const response = await github.graphql(getComments)
const comments = response.repository.pullRequest.comments.nodes
const mutations = comments
.filter(comment => comment.author.login == 'github-actions' && !comment.isMinimized && comment.body.startsWith('Affected Artifacts'))
.map(comment =>
github.graphql(
`mutation {
minimizeComment(input:{subjectId:"${comment.id}", classifier:OUTDATED}){
clientMutationId
}
}`
)
)
await Promise.all(mutations)
const fs = require('node:fs')
const comment = fs.readFileSync('build/reports/metrics/artifact-analysis.md', 'utf8')
const writeComment =
`mutation {
addComment(input:{body:"""${comment}""", subjectId:"${response.repository.pullRequest.id}"}){
clientMutationId
}
}`
await github.graphql(writeComment)
- name: Evaluate Result
if: ${{ !contains(github.event.pull_request.labels.*.name, 'acknowledge-artifact-size-increase') }}
run: |
cd build/reports/metrics
cat has-significant-change.txt | grep false || {
echo An artifact increased in size by more than allowed or a new artifact was created.
echo If this is expected please add the 'acknowledge-artifact-size-increase' label to this pull request.
exit 1
}
release-artifact-size-metrics:
if: github.event_name == 'release'
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.CI_AWS_ROLE_ARN }}
aws-region: us-west-2
- name: Calculate Artifact Size Metrics
id: svc-check-batch
run: |
REPOSITORY=$(echo ${{ github.repository }} | cut -d '/' -f 2)
.github/scripts/run-codebuild-batch-job.sh \
--project gh-aws-sdk-kotlin-svc-check-batch \
--source ${{ github.event.pull_request.head.sha }} \
--sdk-pr ${{ inputs.aws-sdk-kotlin-pr }} \
--smithy-pr ${{ inputs.smithy-kotlin-pr }}
--repository $REPOSITORY \
--release ${{ github.event.release.tag_name }}
- name: Cancel build
if: ${{ cancelled() }}
env:
Expand All @@ -95,3 +229,9 @@ jobs:
echo "cancelling in-progress batch build: id=$BUILD_ID"
aws codebuild stop-build --id $BUILD_ID
fi
- name: Collect Artifact Size Metrics
run: ./gradlew collectDelegatedArtifactSizeMetrics -Prelease=${{ github.event.release.tag_name }}
- name: Save Artifact Size Metrics
run: ./gradlew saveArtifactSizeMetrics
- name: Put Artifact Size Metrics in CloudWatch
run: ./gradlew putArtifactSizeMetricsInCloudWatch -Prelease=${{ github.event.release.tag_name }}
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ plugins {
// ensure the correct version of KGP ends up on our buildscript classpath
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.aws.kotlin.repo.tools.artifactsizemetrics)
}

artifactSizeMetrics {
artifactPrefixes = setOf(":services", ":aws-runtime")
closurePrefixes = setOf(":services")
significantChangeThresholdPercentage = 5.0
projectRepositoryName = "aws-sdk-kotlin"
}

val testJavaVersion = typedProp<String>("test.java.version")?.let {
Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
kotlin-version = "1.9.23"
dokka-version = "1.9.10"

aws-kotlin-repo-tools-version = "0.4.0"
aws-kotlin-repo-tools-version = "0.4.4"

# libs
coroutines-version = "1.7.3"
Expand Down Expand Up @@ -135,3 +135,4 @@ kotlinx-binary-compatibility-validator = { id = "org.jetbrains.kotlinx.binary-co
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin-version"}
aws-kotlin-repo-tools-kmp = { id = "aws.sdk.kotlin.gradle.kmp", version.ref = "aws-kotlin-repo-tools-version" }
aws-kotlin-repo-tools-smithybuild = { id = "aws.sdk.kotlin.gradle.smithybuild", version.ref = "aws-kotlin-repo-tools-version" }
aws-kotlin-repo-tools-artifactsizemetrics = { id = "aws.sdk.kotlin.gradle.artifactsizemetrics", version.ref = "aws-kotlin-repo-tools-version" }

0 comments on commit 2c51199

Please sign in to comment.