diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index eb1056f..e5690f0 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -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: @@ -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 }} diff --git a/pyproject.toml b/pyproject.toml index 54f0c48..56e7bae 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", @@ -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] diff --git a/scripts/github/purge_deployments.py b/scripts/github/purge_deployments.py new file mode 100644 index 0000000..4c4a9c7 --- /dev/null +++ b/scripts/github/purge_deployments.py @@ -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()