Skip to content

Commit 6ea6485

Browse files
authored
CodeBuild GitHub Actions Runner Project (#2704)
### Description of changes: Sets up a CodeBuild project to be able to leverage self-hosted GitHub Action Runners in AWS CodeBuild. ### Testing: Follow-up PRs will migrate various omnibus jobs and demonstrate this working. For now this just stages the CDK code needed to get it setup so we can phase roll it out. A demonstration of this code in action can be found here: https://github.com/skmcgrail/aws-lc/actions/runs/17870473934 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.
1 parent 4874b2e commit 6ea6485

File tree

11 files changed

+239
-12
lines changed

11 files changed

+239
-12
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: 'codebuild-docker-run'
2+
description: 'Run one or more commands inside a docker container'
3+
inputs:
4+
image:
5+
description: 'Docker image to pull'
6+
required: true
7+
options:
8+
description: 'Additional docker run configuration options'
9+
required: false
10+
run:
11+
description: 'Run command in container'
12+
required: false
13+
shell:
14+
description: 'Use a specific shell that must be available in the image'
15+
required: false
16+
default: bash
17+
env:
18+
description: 'Environment variables to set or pass to the container'
19+
required: false
20+
default: ''
21+
runs:
22+
using: 'composite'
23+
steps:
24+
- name: Run Docker Container (${{ inputs.image }})
25+
shell: bash
26+
env:
27+
INPUT_IMAGE: ${{ inputs.image }}
28+
INPUT_OPTIONS: ${{ inputs.options }}
29+
INPUT_RUN: ${{ inputs.run }}
30+
INPUT_SHELL: ${{ inputs.shell }}
31+
INPUT_ENV: ${{ inputs.env }}
32+
run: ${{ github.action_path }}/codebuild-docker-run.sh
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
3+
set -ex
4+
5+
# Function to parse INPUT_ENV and convert to -e flags
6+
parse_env_vars() {
7+
local env_string="$1"
8+
local env_flags=""
9+
10+
# Return empty if INPUT_ENV is not set or empty
11+
if [[ -z "$env_string" ]]; then
12+
echo ""
13+
return
14+
fi
15+
16+
# Process each line as a single key=value pair or just key
17+
while IFS= read -r line; do
18+
# Skip empty lines
19+
[[ -z "$line" ]] && continue
20+
21+
# Check if line contains an equals sign
22+
if [[ "$line" == *"="* ]]; then
23+
# Extract key and value
24+
key="${line%%=*}"
25+
value="${line#*=}"
26+
27+
# Skip if key is empty
28+
[[ -z "$key" ]] && continue
29+
30+
# Add -e flag with proper quoting
31+
env_flags="$env_flags -e $key=\"$value\""
32+
else
33+
# Line is just a key name, pass current environment value
34+
key="$line"
35+
36+
# Skip if key is empty
37+
[[ -z "$key" ]] && continue
38+
39+
# Add -e flag without value (Docker will use current environment)
40+
env_flags="$env_flags -e $key"
41+
fi
42+
done <<< "$env_string"
43+
44+
echo "$env_flags"
45+
}
46+
47+
# Parse environment variables from INPUT_ENV
48+
ENV_FLAGS=$(parse_env_vars "$INPUT_ENV")
49+
50+
exec docker run -v /var/run/docker.sock:/var/run/docker.sock \
51+
-v ${GITHUB_WORKSPACE}:${GITHUB_WORKSPACE} \
52+
-w ${GITHUB_WORKSPACE} \
53+
${INPUT_OPTIONS:-} \
54+
-e GOPROXY \
55+
${ENV_FLAGS} \
56+
--entrypoint=${INPUT_SHELL} ${INPUT_IMAGE} \
57+
-c "${INPUT_RUN//$'\n'/;}"

.github/workflows/actions-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ env:
1313
SDE_VERSION_TAG: sde-external-9.44.0-2024-08-22-win
1414
PACKAGE_NAME: aws-lc
1515
# Used to enable ASAN test dimension.
16-
AWSLC_NO_ASM_FIPS: 1
16+
AWSLC_ENABLE_FIPS_ASAN: 1
1717
DEBIAN_FRONTEND: noninteractive
1818

1919
jobs:
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 OR ISC
3+
import typing
4+
5+
from aws_cdk import (
6+
Duration,
7+
Stack,
8+
aws_codebuild as codebuild,
9+
aws_iam as iam,
10+
aws_s3_assets,
11+
aws_logs as logs,
12+
Environment,
13+
)
14+
from constructs import Construct
15+
16+
from cdk.aws_lc_base_ci_stack import AwsLcBaseCiStack
17+
from cdk.components import PruneStaleGitHubBuilds
18+
from util.iam_policies import (
19+
code_build_publish_metrics_in_json,
20+
)
21+
from util.metadata import LINUX_X86_ECR_REPO, LINUX_AARCH_ECR_REPO, WINDOWS_X86_ECR_REPO
22+
23+
class AwsLcGitHubActionsStack(AwsLcBaseCiStack):
24+
"""Define a stack used to execute AWS-LC self-hosted GitHub Actions Runners."""
25+
26+
def __init__(
27+
self,
28+
scope: Construct,
29+
id: str,
30+
env: typing.Union[Environment, typing.Dict[str, typing.Any]],
31+
**kwargs
32+
) -> None:
33+
super().__init__(scope, id, env=env, timeout=180, **kwargs)
34+
35+
# Define a IAM role for this stack.
36+
metrics_policy = iam.PolicyDocument.from_json(
37+
code_build_publish_metrics_in_json(env)
38+
)
39+
40+
inline_policies = {
41+
"metrics_policy": metrics_policy,
42+
"ecr": iam.PolicyDocument(
43+
statements=[
44+
iam.PolicyStatement(
45+
effect=iam.Effect.ALLOW,
46+
actions=[
47+
"ecr:GetAuthorizationToken",
48+
],
49+
resources=["*"],
50+
),
51+
iam.PolicyStatement(
52+
effect=iam.Effect.ALLOW,
53+
actions=[
54+
"ecr:BatchGetImage",
55+
"ecr:BatchCheckLayerAvailability",
56+
"ecr:GetDownloadUrlForLayer",
57+
],
58+
resources=[
59+
"arn:aws:ecr:{}:{}:repository/{}"
60+
.format(env.region, env.account, repo) for repo in [LINUX_X86_ECR_REPO,
61+
LINUX_AARCH_ECR_REPO,
62+
WINDOWS_X86_ECR_REPO]
63+
],
64+
),
65+
],
66+
)
67+
}
68+
role = iam.Role(
69+
scope=self,
70+
id="{}-role".format(id),
71+
assumed_by=iam.ServicePrincipal("codebuild.amazonaws.com"),
72+
inline_policies=inline_policies,
73+
)
74+
75+
logging_options = codebuild.LoggingOptions(
76+
cloud_watch=codebuild.CloudWatchLoggingOptions(log_group=logs.LogGroup(
77+
self, id="{}-logs".format(id)))
78+
)
79+
80+
# Override base class provided configuration
81+
self.git_hub_source = codebuild.Source.git_hub(
82+
owner=self.github_repo_owner,
83+
repo=self.github_repo_name,
84+
webhook=True,
85+
webhook_filters=[
86+
codebuild.FilterGroup.in_event_of(
87+
codebuild.EventAction.WORKFLOW_JOB_QUEUED
88+
),
89+
],
90+
)
91+
92+
# Define CodeBuild.
93+
project = codebuild.Project(
94+
scope=self,
95+
id=id,
96+
project_name=id,
97+
source=self.git_hub_source,
98+
role=role,
99+
timeout=Duration.minutes(self.timeout),
100+
logging=logging_options,
101+
environment=codebuild.BuildEnvironment(
102+
compute_type=codebuild.ComputeType.SMALL,
103+
privileged=True,
104+
build_image=codebuild.LinuxBuildImage.STANDARD_7_0,
105+
environment_variables={
106+
"AWS_ACCOUNT_ID": codebuild.BuildEnvironmentVariable(value=env.account),
107+
},
108+
),
109+
build_spec=codebuild.BuildSpec.from_object({
110+
"version": 0.2,
111+
"phases": {
112+
"pre_build": {
113+
"commands": [
114+
"mkdir -p /root/.docker",
115+
"""\
116+
cat <<EOF > /root/.docker/config.json
117+
{
118+
"credHelpers": {
119+
"public.ecr.aws": "ecr-login",
120+
"$AWS_ACCOUNT_ID.dkr.ecr.us-west-2.amazonaws.com": "ecr-login"
121+
}
122+
}
123+
EOF
124+
"""
125+
]
126+
}
127+
},
128+
}),
129+
)
130+
131+
cfn_project = project.node.default_child
132+
cfn_project.add_property_override("Triggers.PullRequestBuildPolicy", self.pull_request_policy)

tests/ci/cdk/cdk/codebuild/github_ci_linux_arm_omnibus.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ batch:
106106
variables:
107107
# AWS_LC_GO_TEST_TIMEOUT is needed on aarch when ASAN is enabled because the ASAN is very slow.
108108
AWS_LC_GO_TEST_TIMEOUT: 120m
109-
AWSLC_NO_ASM_FIPS: 1
109+
AWSLC_ENABLE_FIPS_ASAN: 1
110110
AWS_LC_CI_TARGET: "tests/ci/run_fips_tests.sh"
111111

112112
- identifier: ubuntu2004_clang7x_aarch_minimal
@@ -239,7 +239,6 @@ batch:
239239
variables:
240240
# AL2 Clang-7 does not support AddressSanitizer. Related ticket is linked in CryptoAlg-694.
241241
# https://github.com/aws/aws-lc/pull/120#issuecomment-808439279
242-
AWSLC_NO_ASM_FIPS: 0
243242
AWS_LC_CI_TARGET: "tests/ci/run_fips_tests.sh"
244243

245244
- identifier: amazonlinux2023_gcc11x_aarch
@@ -270,9 +269,6 @@ batch:
270269
compute-type: BUILD_GENERAL1_LARGE
271270
image: 620771051181.dkr.ecr.us-west-2.amazonaws.com/aws-lc-docker-images-linux-aarch:amazonlinux-2_gcc-7x_latest
272271
variables:
273-
# AL2 Clang-7 does not support AddressSanitizer. Related ticket is linked in CryptoAlg-694.
274-
# https://github.com/aws/aws-lc/pull/120#issuecomment-808439279
275-
AWSLC_NO_ASM_FIPS: 0
276272
AWS_LC_CI_TARGET: "tests/ci/run_fips_callback_tests.sh"
277273

278274
- identifier: amazonlinux2023_gcc11x_aarch_fips_callback

tests/ci/cdk/cdk/codebuild/github_ci_linux_x86_omnibus.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ batch:
223223
compute-type: BUILD_GENERAL1_LARGE
224224
image: 620771051181.dkr.ecr.us-west-2.amazonaws.com/aws-lc-docker-images-linux-x86:ubuntu-20.04_clang-7x_latest
225225
variables:
226-
AWSLC_NO_ASM_FIPS: 1
226+
AWSLC_ENABLE_FIPS_ASAN: 1
227227
AWS_LC_CI_TARGET: "tests/ci/run_fips_tests.sh"
228228

229229
- identifier: ubuntu2004_clang8x_x86_64
@@ -378,7 +378,6 @@ batch:
378378
variables:
379379
# AL2 Clang-7 does not support AddressSanitizer. Related ticket is linked in CryptoAlg-694.
380380
# https://github.com/aws/aws-lc/pull/120#issuecomment-808439279
381-
AWSLC_NO_ASM_FIPS: 0
382381
AWS_LC_CI_TARGET: "tests/ci/run_fips_tests.sh"
383382

384383
- identifier: amazonlinux2_clang7x_x86_64_prefix

tests/ci/cdk/pipeline/ci_stage.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from constructs import Construct
1616

1717
from cdk.aws_lc_base_ci_stack import AwsLcBaseCiStack
18+
from cdk.aws_lc_github_actions_stack import AwsLcGitHubActionsStack
1819
from pipeline.ci_util import add_ci_stacks
1920
from pipeline.codebuild_batch_step import CodeBuildBatchStep
2021
from util.metadata import (
@@ -47,7 +48,8 @@ def __init__(
4748
@property
4849
def stacks(self) -> typing.List[AwsLcBaseCiStack]:
4950
return [
50-
child for child in self.node.children if isinstance(child, AwsLcBaseCiStack)
51+
child for child in self.node.children if isinstance(child, AwsLcBaseCiStack) and
52+
not isinstance(child, AwsLcGitHubActionsStack)
5153
]
5254

5355
def add_stage_to_pipeline(

tests/ci/cdk/pipeline/ci_util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from cdk.aws_lc_analytics_stack import AwsLcGitHubAnalyticsStack
77
from cdk.aws_lc_android_ci_stack import AwsLcAndroidCIStack
88
from cdk.aws_lc_ec2_test_framework_ci_stack import AwsLcEC2TestingCIStack
9+
from cdk.aws_lc_github_actions_stack import AwsLcGitHubActionsStack
910
from cdk.aws_lc_github_ci_stack import AwsLcGitHubCIStack
1011
from cdk.aws_lc_github_ci_x509_stack import AwsLcGitHubX509CIStack
1112
from cdk.aws_lc_github_fuzz_ci_stack import AwsLcGitHubFuzzCIStack
@@ -19,6 +20,14 @@ def add_ci_stacks(
1920
# define customized settings to run CodeBuild jobs from CodePipeline
2021
build_options = []
2122

23+
AwsLcGitHubActionsStack(
24+
scope,
25+
"aws-lc-ci-github-actions",
26+
env=env,
27+
ignore_failure=False,
28+
stack_name="aws-lc-ci-github-actions",
29+
)
30+
2231
x86_build_spec_file = "cdk/codebuild/github_ci_linux_x86_omnibus.yaml"
2332
AwsLcGitHubCIStack(
2433
scope,

tests/ci/common_posix_setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0 OR ISC
33

44
SRC_ROOT="$(pwd)"
5-
if [ -v CODEBUILD_SRC_DIR ]; then
5+
if [ -v CODEBUILD_SRC_DIR && ! -v CODEBUILD_WEBHOOK_JOB_ID ]; then
66
SRC_ROOT="$CODEBUILD_SRC_DIR"
77
elif [ "$(basename "${SRC_ROOT}")" != 'aws-lc' ]; then
88
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

tests/ci/run_fips_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ if static_linux_supported || static_openbsd_supported; then
6868
fi
6969

7070
# The AL2 version of Clang does not have all of the required artifacts for address sanitizer, see P45594051
71-
if [[ "${AWSLC_NO_ASM_FIPS}" == "1" ]]; then
71+
if [[ "${AWSLC_ENABLE_FIPS_ASAN:-0}" == "1" ]]; then
7272
if [[ ("$(uname -p)" == 'x86_64'*) ]]; then
7373
echo "Building with Clang and testing AWS-LC in FIPS Release mode with address sanitizer."
7474
fips_build_and_test -DASAN=1 -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=1

0 commit comments

Comments
 (0)