Skip to content

Commit

Permalink
Add script to purge old deployment bundles from S3
Browse files Browse the repository at this point in the history
  • Loading branch information
object-Object committed Jan 3, 2024
1 parent 7297cda commit bd31bee
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/setup-python@v4
with:
python-version: "3.11"
cache: pip

- uses: aws-actions/configure-aws-credentials@v4
with:
Expand Down Expand Up @@ -165,3 +169,9 @@ jobs:
run: |
aws deploy wait deployment-successful \
--deployment-id ${{ steps.create-deployment.outputs.deployment-id }}
- name: Install Python packages
run: pip install .[codedeploy]

- name: Purge old deployment bundles
run: python scripts/github/purge_deployments.py ${{ env.S3_BUCKET }} ${{ env.STACK_NAME }}
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ aws-cdk = [
"aws-cdk-lib==2.102.0",
"aws-cdk-github-oidc==2.4.0",
]
codedeploy = [
"boto3~=1.34",
"boto3-stubs-lite[s3]",
"typer~=0.9",
]
scripts = [
"discord.py~=2.3.2",
"jproperties~=2.1",
Expand Down Expand Up @@ -47,7 +52,7 @@ target-linux = [ # x86_64-unknown-linux-gnu
"hexnumgen @ {root:uri}/vendor/hexnumgen-0.2.0-cp311-cp311-manylinux_2_28_x86_64.whl",
]
dev = [
"HexBug[aws-cdk,scripts,runtime,target-any]",
"HexBug[aws-cdk,codedeploy,scripts,runtime,target-any]",
]

[project.urls]
Expand Down
36 changes: 36 additions & 0 deletions scripts/github/purge_deployments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import Optional

import boto3
from mypy_boto3_s3 import S3ServiceResource
from typer import Typer

app = Typer(
pretty_exceptions_enable=False,
)


@app.command()
def main(
bucket_name: str,
prefix: str,
*,
keep: int = 5,
profile: Optional[str] = None,
):
session = boto3.Session(profile_name=profile)
s3: S3ServiceResource = session.resource("s3")

bucket = s3.Bucket(bucket_name)

objects = sorted(
bucket.objects.filter(Prefix=prefix),
key=lambda o: o.last_modified,
reverse=True,
)
for obj in objects[keep:]:
print(f"Deleting object: {obj.key}")
obj.delete()


if __name__ == "__main__":
app()

0 comments on commit bd31bee

Please sign in to comment.