Skip to content

Commit 9a972a0

Browse files
authored
Merge pull request #48 from neurostuff/ref/aws_stack
refactor aws stack
2 parents a23f1da + fd45d5d commit 9a972a0

7 files changed

Lines changed: 317 additions & 63 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ permissions:
1111
jobs:
1212
deploy:
1313
runs-on: ubuntu-latest
14+
env:
15+
ECS_IMAGE_REPOSITORY: compose-runner-ecs
16+
LAMBDA_IMAGE_REPOSITORY: compose-runner-lambda
1417

1518
steps:
1619
- name: Checkout
@@ -35,25 +38,85 @@ jobs:
3538
pip install -r requirements.txt
3639
3740
- name: Install AWS CDK CLI
38-
run: npm install -g aws-cdk@2.130.0 --registry=https://registry.npmjs.org
41+
run: npm install -g aws-cdk@2.1107.0 --registry=https://registry.npmjs.org
3942

4043
- name: Configure AWS credentials
4144
uses: aws-actions/configure-aws-credentials@v4
4245
with:
4346
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
4447
aws-region: ${{ secrets.AWS_REGION }}
4548

49+
- name: Deploy image repository stack
50+
working-directory: infra/cdk
51+
run: |
52+
source .venv/bin/activate
53+
cdk deploy \
54+
ComposeRunnerImageRepositoriesStack \
55+
--require-approval never
56+
57+
- name: Verify ComposeRunnerStack does not synthesize CDK Docker assets
58+
working-directory: infra/cdk
59+
env:
60+
VERSION: ${{ github.event.release.tag_name }}
61+
run: |
62+
source .venv/bin/activate
63+
rm -rf /tmp/compose-runner-cdk-verify
64+
cdk synth \
65+
ComposeRunnerStack \
66+
--output /tmp/compose-runner-cdk-verify \
67+
-c composeRunnerVersion=${VERSION} >/dev/null
68+
jq -e '(.dockerImages // {}) | length == 0' \
69+
/tmp/compose-runner-cdk-verify/ComposeRunnerStack.assets.json >/dev/null || {
70+
echo "ComposeRunnerStack synthesized Docker image assets; deployment would require the CDK bootstrap ECR repository."
71+
cat /tmp/compose-runner-cdk-verify/ComposeRunnerStack.assets.json
72+
exit 1
73+
}
74+
75+
- name: Log in to Amazon ECR
76+
run: |
77+
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
78+
ECR_REGISTRY="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
79+
echo "ECR_REGISTRY=${ECR_REGISTRY}" >> "$GITHUB_ENV"
80+
aws ecr get-login-password | docker login --username AWS --password-stdin "$ECR_REGISTRY"
81+
82+
- name: Build and push release images
83+
env:
84+
VERSION: ${{ github.event.release.tag_name }}
85+
run: |
86+
if [ -z "$VERSION" ]; then
87+
echo "Release tag name is required to publish ECR images"
88+
exit 1
89+
fi
90+
91+
ECS_IMAGE_URI="${ECR_REGISTRY}/${ECS_IMAGE_REPOSITORY}:${VERSION}"
92+
LAMBDA_IMAGE_URI="${ECR_REGISTRY}/${LAMBDA_IMAGE_REPOSITORY}:${VERSION}"
93+
94+
docker build \
95+
--file Dockerfile \
96+
--build-arg COMPOSE_RUNNER_VERSION="${VERSION}" \
97+
--tag "${ECS_IMAGE_URI}" \
98+
.
99+
docker push "${ECS_IMAGE_URI}"
100+
101+
docker build \
102+
--file aws_lambda/Dockerfile \
103+
--build-arg COMPOSE_RUNNER_VERSION="${VERSION}" \
104+
--tag "${LAMBDA_IMAGE_URI}" \
105+
.
106+
docker push "${LAMBDA_IMAGE_URI}"
107+
46108
- name: Deploy CDK stack
47109
working-directory: infra/cdk
48110
env:
49111
RESULTS_PREFIX: compose-runner/results
50112
TASK_CPU: 4096
51113
TASK_MEMORY_MIB: 30720
52114
STATE_MACHINE_TIMEOUT_SECONDS: 7200
115+
VERSION: ${{ github.event.release.tag_name }}
53116
run: |
54117
source .venv/bin/activate
55-
VERSION=${GITHUB_REF_NAME}
56118
cdk deploy \
119+
ComposeRunnerStack \
57120
--require-approval never \
58121
--outputs-file cdk-outputs.json \
59122
-c composeRunnerVersion=${VERSION} \
@@ -112,3 +175,17 @@ jobs:
112175
echo "Status response missing status: $status_json"
113176
exit 1
114177
fi
178+
179+
- name: Garbage collect unused CDK bootstrap S3 assets
180+
working-directory: infra/cdk
181+
run: |
182+
source .venv/bin/activate
183+
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
184+
cdk gc \
185+
"aws://${AWS_ACCOUNT_ID}/${AWS_REGION}" \
186+
--unstable=gc \
187+
--type=s3 \
188+
--action=full \
189+
--created-buffer-days=7 \
190+
--rollback-buffer-days=30 \
191+
--confirm false

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ FROM public.ecr.aws/docker/library/python:3.13-slim
22

33
ARG COMPOSE_RUNNER_VERSION
44
ENV COMPOSE_RUNNER_VERSION=${COMPOSE_RUNNER_VERSION}
5+
ENV HATCH_BUILD_VERSION=${COMPOSE_RUNNER_VERSION}
6+
ENV SETUPTOOLS_SCM_PRETEND_VERSION=${COMPOSE_RUNNER_VERSION}
7+
ENV SETUPTOOLS_SCM_PRETEND_VERSION_FOR_COMPOSE_RUNNER=${COMPOSE_RUNNER_VERSION}
58
LABEL org.opencontainers.image.title="compose-runner ecs task"
69
LABEL org.opencontainers.image.version=${COMPOSE_RUNNER_VERSION}
710

infra/cdk/app.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,31 @@
66

77
import aws_cdk as cdk
88

9-
from stacks.compose_runner_stack import ComposeRunnerStack
9+
from stacks import ComposeRunnerImageRepositoriesStack, ComposeRunnerStack
1010

1111

1212
def main() -> None:
1313
app = cdk.App()
14+
env = cdk.Environment(
15+
account=os.getenv("CDK_DEFAULT_ACCOUNT"),
16+
region=os.getenv("CDK_DEFAULT_REGION"),
17+
)
1418

15-
ComposeRunnerStack(
19+
image_repositories_stack = ComposeRunnerImageRepositoriesStack(
1620
app,
17-
"ComposeRunnerStack",
18-
env=cdk.Environment(
19-
account=os.getenv("CDK_DEFAULT_ACCOUNT"),
20-
region=os.getenv("CDK_DEFAULT_REGION"),
21-
),
21+
"ComposeRunnerImageRepositoriesStack",
22+
env=env,
2223
)
2324

25+
if app.node.try_get_context("composeRunnerVersion"):
26+
ComposeRunnerStack(
27+
app,
28+
"ComposeRunnerStack",
29+
ecs_image_repository=image_repositories_stack.ecs_image_repository,
30+
lambda_image_repository=image_repositories_stack.lambda_image_repository,
31+
env=env,
32+
)
33+
2434
app.synth()
2535

2636

infra/cdk/stacks/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .image_repositories_stack import ComposeRunnerImageRepositoriesStack
12
from .compose_runner_stack import ComposeRunnerStack
23

3-
__all__ = ["ComposeRunnerStack"]
4+
__all__ = ["ComposeRunnerImageRepositoriesStack", "ComposeRunnerStack"]

0 commit comments

Comments
 (0)