Skip to content

Commit 2c51199

Browse files
authored
feat: artifact size metrics (#1290)
1 parent c0099ac commit 2c51199

File tree

5 files changed

+195
-16
lines changed

5 files changed

+195
-16
lines changed

.github/actions/setup-build/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ runs:
1010
with:
1111
path: 'aws-kotlin-repo-tools'
1212
repository: 'awslabs/aws-kotlin-repo-tools'
13-
ref: '0.2.3'
13+
ref: '0.4.2'
1414
sparse-checkout: |
1515
.github
1616
- name: Checkout smithy-kotlin

.github/scripts/run-codebuild-batch-job.sh

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ error_exit() {
88
PROJECT_NAME=gh-aws-sdk-kotlin-svc-check-batch
99
# get the source version to be built (defaults to main branch if not specified)
1010
SOURCE_VERSION=main
11-
SDK_PR=""
12-
SMITHY_PR=""
11+
GITHUB_RELEASE=""
12+
GITHUB_PULL_REQUEST_NUMBER=""
13+
GITHUB_REPOSITORY_NO_ORG=""
14+
EXTERNAL_CONTRIBUTOR_SDK_PR=""
15+
EXTERNAL_CONTRIBUTOR_SMITHY_PR=""
1316

1417
while [[ $# -gt 0 ]]; do
1518
case "$1" in
@@ -29,19 +32,43 @@ while [[ $# -gt 0 ]]; do
2932
shift 2
3033
fi
3134
;;
32-
--sdk-pr)
35+
--release)
3336
if [[ "$2" == --* ]]; then
3437
shift 1
3538
else
36-
SDK_PR="$2"
39+
GITHUB_RELEASE="$2"
3740
shift 2
3841
fi
3942
;;
40-
--smithy-pr)
43+
--pr-number)
44+
if [[ "$2" == --* ]]; then
45+
shift 1
46+
else
47+
GITHUB_PULL_REQUEST_NUMBER="$2"
48+
shift 2
49+
fi
50+
;;
51+
--repository)
52+
if [[ "$2" == --* ]]; then
53+
shift 1
54+
else
55+
GITHUB_REPOSITORY_NO_ORG="$2"
56+
shift 2
57+
fi
58+
;;
59+
--external-contributor-sdk-pr-number)
60+
if [[ "$2" == --* ]]; then
61+
shift 1
62+
else
63+
EXTERNAL_CONTRIBUTOR_SDK_PR="$2"
64+
shift 2
65+
fi
66+
;;
67+
--external-contributor-smithy-pr-number)
4168
if [[ "$2" == --* || -z "$2" ]]; then
4269
shift 1
4370
else
44-
SMITHY_PR="$2"
71+
EXTERNAL_CONTRIBUTOR_SMITHY_PR="$2"
4572
shift 2
4673
fi
4774
;;
@@ -52,8 +79,11 @@ while [[ $# -gt 0 ]]; do
5279
esac
5380
done
5481

55-
export SDK_PR
56-
export SMITHY_PR
82+
export GITHUB_RELEASE
83+
export GITHUB_PULL_REQUEST_NUMBER
84+
export GITHUB_REPOSITORY_NO_ORG
85+
export EXTERNAL_CONTRIBUTOR_SDK_PR
86+
export EXTERNAL_CONTRIBUTOR_SMITHY_PR
5787

5888
echo "Starting CodeBuild project ${PROJECT_NAME}"
5989

.github/workflows/codebuild-ci.yml

Lines changed: 146 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
name: AWS CodeBuild CI
22

33
on:
4+
release:
5+
types: [ published ]
46
pull_request:
5-
branches:
6-
- main
7+
types: [ opened, synchronize, reopened, labeled, unlabeled ]
8+
branches: [ main ]
79
workflow_dispatch:
810
inputs:
911
aws-sdk-kotlin-pr:
@@ -27,6 +29,7 @@ env:
2729
permissions:
2830
id-token: write
2931
contents: read
32+
pull-requests: write
3033

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

3639
jobs:
3740
e2e-tests:
41+
if: github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch'
3842
runs-on: ubuntu-latest
3943
steps:
4044
- name: Verify PRs are not running malicious code
@@ -63,7 +67,8 @@ jobs:
6367
aws codebuild stop-build --id $BUILD_ID
6468
fi
6569
66-
service-check-batch:
70+
service-check-batch-and-artifact-size-metrics:
71+
if: github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch'
6772
runs-on: ubuntu-latest
6873
steps:
6974
- name: Verify PRs are not running malicious code
@@ -78,14 +83,143 @@ jobs:
7883
with:
7984
role-to-assume: ${{ secrets.CI_AWS_ROLE_ARN }}
8085
aws-region: us-west-2
81-
- name: Run Service Check Batch
86+
- name: Run Service Check Batch and Calculate Artifact Size Metrics
87+
id: svc-check-batch
88+
run: |
89+
REPOSITORY=$(echo ${{ github.repository }} | cut -d '/' -f 2)
90+
PULL_REQUEST=""
91+
INTERNAL=${{ github.event.number }}
92+
EXTERNAL=${{ inputs.aws-sdk-kotlin-pr }}
93+
94+
if [ ! -z "$INTERNAL" ]; then
95+
PULL_REQUEST=${{ github.event.number }}
96+
echo "Identified this an internal pull request build: $PULL_REQUEST"
97+
elif [ ! -z "$EXTERNAL" ]; then
98+
PULL_REQUEST=${{ inputs.aws-sdk-kotlin-pr }}
99+
echo "Identified this as an external pull request build: $PULL_REQUEST"
100+
else
101+
echo "Couldn't identify this as an internal or external pull request"
102+
exit 1
103+
fi
104+
105+
.github/scripts/run-codebuild-batch-job.sh \
106+
--project gh-aws-sdk-kotlin-svc-check-batch \
107+
--source ${{ github.event.pull_request.head.sha }} \
108+
--pr-number $PULL_REQUEST \
109+
--repository $REPOSITORY \
110+
--external-contributor-sdk-pr-number ${{ inputs.aws-sdk-kotlin-pr }} \
111+
--external-contributor-smithy-pr-number ${{ inputs.smithy-kotlin-pr }}
112+
- name: Cancel build
113+
if: ${{ cancelled() }}
114+
env:
115+
BUILD_ID: ${{ steps.svc-check-batch.outputs.aws-build-id }}
116+
run: |
117+
if [ ! -z "$BUILD_ID" ]; then
118+
echo "cancelling in-progress batch build: id=$BUILD_ID"
119+
aws codebuild stop-build --id $BUILD_ID
120+
fi
121+
- name: Collect Artifact Size Metrics
122+
run: |
123+
PULL_REQUEST=""
124+
INTERNAL=${{ github.event.number }}
125+
EXTERNAL=${{ inputs.aws-sdk-kotlin-pr }}
126+
127+
if [ ! -z "$INTERNAL" ]; then
128+
PULL_REQUEST=${{ github.event.number }}
129+
echo "Identified this an internal pull request build: $PULL_REQUEST"
130+
elif [ ! -z "$EXTERNAL" ]; then
131+
PULL_REQUEST=${{ inputs.aws-sdk-kotlin-pr }}
132+
echo "Identified this as an external pull request build: $PULL_REQUEST"
133+
else
134+
echo "Couldn't identify this as an internal or external pull request"
135+
exit 1
136+
fi
137+
138+
./gradlew collectDelegatedArtifactSizeMetrics -PpullRequest=$PULL_REQUEST
139+
- name: Analyze Artifact Size Metrics
140+
run: ./gradlew analyzeArtifactSizeMetrics
141+
- name: Show Results
142+
uses: actions/github-script@v7
143+
with:
144+
script: |
145+
const getComments =
146+
`query {
147+
repository(owner:"${context.repo.owner}", name:"${context.repo.repo}"){
148+
pullRequest(number: ${context.issue.number}) {
149+
id
150+
comments(last:100) {
151+
nodes {
152+
id
153+
body
154+
author {
155+
login
156+
}
157+
isMinimized
158+
}
159+
}
160+
}
161+
}
162+
}`
163+
164+
const response = await github.graphql(getComments)
165+
const comments = response.repository.pullRequest.comments.nodes
166+
167+
const mutations = comments
168+
.filter(comment => comment.author.login == 'github-actions' && !comment.isMinimized && comment.body.startsWith('Affected Artifacts'))
169+
.map(comment =>
170+
github.graphql(
171+
`mutation {
172+
minimizeComment(input:{subjectId:"${comment.id}", classifier:OUTDATED}){
173+
clientMutationId
174+
}
175+
}`
176+
)
177+
)
178+
await Promise.all(mutations)
179+
180+
const fs = require('node:fs')
181+
const comment = fs.readFileSync('build/reports/metrics/artifact-analysis.md', 'utf8')
182+
183+
const writeComment =
184+
`mutation {
185+
addComment(input:{body:"""${comment}""", subjectId:"${response.repository.pullRequest.id}"}){
186+
clientMutationId
187+
}
188+
}`
189+
190+
await github.graphql(writeComment)
191+
192+
- name: Evaluate Result
193+
if: ${{ !contains(github.event.pull_request.labels.*.name, 'acknowledge-artifact-size-increase') }}
194+
run: |
195+
cd build/reports/metrics
196+
cat has-significant-change.txt | grep false || {
197+
echo An artifact increased in size by more than allowed or a new artifact was created.
198+
echo If this is expected please add the 'acknowledge-artifact-size-increase' label to this pull request.
199+
exit 1
200+
}
201+
202+
release-artifact-size-metrics:
203+
if: github.event_name == 'release'
204+
runs-on: ubuntu-latest
205+
steps:
206+
- name: Checkout sources
207+
uses: actions/checkout@v2
208+
- name: Configure AWS Credentials
209+
uses: aws-actions/configure-aws-credentials@v4
210+
with:
211+
role-to-assume: ${{ secrets.CI_AWS_ROLE_ARN }}
212+
aws-region: us-west-2
213+
- name: Calculate Artifact Size Metrics
82214
id: svc-check-batch
83215
run: |
216+
REPOSITORY=$(echo ${{ github.repository }} | cut -d '/' -f 2)
217+
84218
.github/scripts/run-codebuild-batch-job.sh \
85219
--project gh-aws-sdk-kotlin-svc-check-batch \
86220
--source ${{ github.event.pull_request.head.sha }} \
87-
--sdk-pr ${{ inputs.aws-sdk-kotlin-pr }} \
88-
--smithy-pr ${{ inputs.smithy-kotlin-pr }}
221+
--repository $REPOSITORY \
222+
--release ${{ github.event.release.tag_name }}
89223
- name: Cancel build
90224
if: ${{ cancelled() }}
91225
env:
@@ -95,3 +229,9 @@ jobs:
95229
echo "cancelling in-progress batch build: id=$BUILD_ID"
96230
aws codebuild stop-build --id $BUILD_ID
97231
fi
232+
- name: Collect Artifact Size Metrics
233+
run: ./gradlew collectDelegatedArtifactSizeMetrics -Prelease=${{ github.event.release.tag_name }}
234+
- name: Save Artifact Size Metrics
235+
run: ./gradlew saveArtifactSizeMetrics
236+
- name: Put Artifact Size Metrics in CloudWatch
237+
run: ./gradlew putArtifactSizeMetricsInCloudWatch -Prelease=${{ github.event.release.tag_name }}

build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ plugins {
2222
// ensure the correct version of KGP ends up on our buildscript classpath
2323
alias(libs.plugins.kotlin.multiplatform) apply false
2424
alias(libs.plugins.kotlin.jvm) apply false
25+
alias(libs.plugins.aws.kotlin.repo.tools.artifactsizemetrics)
26+
}
27+
28+
artifactSizeMetrics {
29+
artifactPrefixes = setOf(":services", ":aws-runtime")
30+
closurePrefixes = setOf(":services")
31+
significantChangeThresholdPercentage = 5.0
32+
projectRepositoryName = "aws-sdk-kotlin"
2533
}
2634

2735
val testJavaVersion = typedProp<String>("test.java.version")?.let {

gradle/libs.versions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
kotlin-version = "1.9.23"
33
dokka-version = "1.9.10"
44

5-
aws-kotlin-repo-tools-version = "0.4.0"
5+
aws-kotlin-repo-tools-version = "0.4.4"
66

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

0 commit comments

Comments
 (0)