From a2aa45c804eb962dad429cb946517751882d5694 Mon Sep 17 00:00:00 2001 From: Stefan Reimer Date: Wed, 9 Feb 2022 23:24:12 +0100 Subject: [PATCH] ci: various build improvements --- Dockerfile | 15 +++++++++++---- Makefile | 8 ++------ README.md | 2 +- app/app.py | 2 +- dev-requirements.txt | 2 ++ requirements-dev.txt | 5 ----- requirements.txt | 1 + test.py | 2 +- 8 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 dev-requirements.txt delete mode 100644 requirements-dev.txt diff --git a/Dockerfile b/Dockerfile index f36ceaa..6590c78 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,7 @@ RUN apk upgrade -U --available --no-cache && \ apk add --no-cache \ libstdc++ + # Stage 2 - build function and dependencies FROM python-alpine AS build-image # Install aws-lambda-cpp build dependencies @@ -38,14 +39,12 @@ COPY app/* ${FUNCTION_DIR} # Install requirements COPY requirements.txt requirements.txt -RUN python${RUNTIME_VERSION} -m pip install -r requirements.txt --target ${FUNCTION_DIR} +RUN export MAKEFLAGS="-j$(nproc)"; pip install -r requirements.txt --target ${FUNCTION_DIR} -# Install Lambda Runtime Interface Client for Python -RUN python${RUNTIME_VERSION} -m pip install awslambdaric --target ${FUNCTION_DIR} # Stage 3 - final runtime image # Grab a fresh copy of the Python image -FROM python-alpine +FROM python-alpine as release # Include global arg in this stage of the build ARG FUNCTION_DIR # Set working directory to function root directory @@ -55,3 +54,11 @@ COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR} ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ] CMD [ "app.handler" ] + + +# Tests +FROM release as test +ARG FUNCTION_DIR + +COPY test.py . +RUN ./test.py diff --git a/Makefile b/Makefile index 424b7e0..b823391 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ endif all: build build: - podman build --rm --squash-all -t $(REPOSITORY):$(TAG) -t $(REPOSITORY):latest . + podman build --target release --rm -t $(REPOSITORY):$(TAG) -t $(REPOSITORY):latest . push: aws ecr-public get-login-password --region $(REGION) | podman login --username AWS --password-stdin $(REGISTRY) @@ -31,13 +31,9 @@ scan: trivy $(TRIVY_OPTS) $(REPOSITORY):$(TAG) test: - @echo "Not implemented (yet)" + podman build --target test --rm -t $(REPOSITORY):$(TAG) -t $(REPOSITORY):latest . -pytest: - flake8 --ignore=E501 app/app.py - ./test.py - aws_lambda: @[ -z $(AWS_LAMBDA) ] && { echo "Missing aws-lambda-rie in PATH"; exit 1;} || true diff --git a/README.md b/README.md index 73ea12b..0039e68 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,4 @@ AWS SNS/Lambda central alert hub taking SNS messages, parsing and formatting the ## Resources - https://gallery.ecr.aws/zero-downtime/sns-alert-hub -- https://github.com/caronc/apprise \ No newline at end of file +- https://github.com/caronc/apprise diff --git a/app/app.py b/app/app.py index 4b97ae7..f7aed73 100644 --- a/app/app.py +++ b/app/app.py @@ -228,4 +228,4 @@ def handler(event, context): else: body = sns["Message"] - apobj.notify(body=body, title="Unknow message type", notify_type=apprise.NotifyType.WARNING) + apobj.notify(body=body, title="Unknown message type", notify_type=apprise.NotifyType.WARNING) diff --git a/dev-requirements.txt b/dev-requirements.txt new file mode 100644 index 0000000..872f4a5 --- /dev/null +++ b/dev-requirements.txt @@ -0,0 +1,2 @@ +flake8 +codespell diff --git a/requirements-dev.txt b/requirements-dev.txt deleted file mode 100644 index efa1806..0000000 --- a/requirements-dev.txt +++ /dev/null @@ -1,5 +0,0 @@ -boto3 -apprise -humanize -awslambdaric -flake8 diff --git a/requirements.txt b/requirements.txt index 424e27c..c374b53 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ boto3 apprise humanize +awslambdaric diff --git a/test.py b/test.py index 097d856..2ba9ada 100755 --- a/test.py +++ b/test.py @@ -9,7 +9,7 @@ def send_event(event): requests.post('http://localhost:8080/2015-03-31/functions/function/invocations', json=event) -p = subprocess.Popen('cd app && aws-lambda-rie /usr/bin/python -m awslambdaric app.handler', shell=True) +p = subprocess.Popen('aws-lambda-rie /usr/bin/python -m awslambdaric app.handler', shell=True) time.sleep(3) # Cloudwatch Alarm