Skip to content

Commit a0ba3cd

Browse files
authored
Build new Lambda extension (getsentry#1383)
* ci(lambda-extension): Use new GitHub action for creating Lambda layer zip. * ci(lambda-extension): Include new Lambda extension from release registry in Lambda layer.
1 parent 91436cd commit a0ba3cd

File tree

5 files changed

+148
-179
lines changed

5 files changed

+148
-179
lines changed

.github/workflows/ci.yml

+69-51
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: ci
1+
name: CI
22

33
on:
44
push:
@@ -9,54 +9,10 @@ on:
99
pull_request:
1010

1111
jobs:
12-
dist:
13-
name: distribution packages
14-
timeout-minutes: 10
15-
runs-on: ubuntu-latest
16-
17-
steps:
18-
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
20-
- uses: actions/setup-python@v2
21-
with:
22-
python-version: 3.9
23-
24-
- run: |
25-
pip install virtualenv
26-
make aws-lambda-layer-build
27-
28-
- uses: actions/upload-artifact@v2
29-
with:
30-
name: ${{ github.sha }}
31-
path: |
32-
dist/*
33-
dist-serverless/*
34-
35-
docs:
36-
timeout-minutes: 10
37-
name: build documentation
38-
runs-on: ubuntu-latest
39-
40-
steps:
41-
- uses: actions/checkout@v2
42-
- uses: actions/setup-node@v1
43-
- uses: actions/setup-python@v2
44-
with:
45-
python-version: 3.9
46-
47-
- run: |
48-
pip install virtualenv
49-
make apidocs
50-
cd docs/_build && zip -r gh-pages ./
51-
52-
- uses: actions/upload-artifact@v2
53-
with:
54-
name: ${{ github.sha }}
55-
path: docs/_build/gh-pages.zip
56-
5712
lint:
58-
timeout-minutes: 10
13+
name: Lint Sources
5914
runs-on: ubuntu-latest
15+
timeout-minutes: 10
6016

6117
steps:
6218
- uses: actions/checkout@v2
@@ -69,9 +25,10 @@ jobs:
6925
tox -e linters
7026
7127
test:
72-
continue-on-error: true
73-
timeout-minutes: 45
28+
name: Run Tests
7429
runs-on: ${{ matrix.linux-version }}
30+
timeout-minutes: 45
31+
continue-on-error: true
7532
strategy:
7633
matrix:
7734
linux-version: [ubuntu-latest]
@@ -125,7 +82,7 @@ jobs:
12582
with:
12683
python-version: ${{ matrix.python-version }}
12784

128-
- name: setup
85+
- name: Setup Test Env
12986
env:
13087
PGHOST: localhost
13188
PGPASSWORD: sentry
@@ -134,7 +91,7 @@ jobs:
13491
psql -c 'create database test_travis_ci_test;' -U postgres
13592
pip install codecov tox
13693
137-
- name: run tests
94+
- name: Run Tests
13895
env:
13996
CI_PYTHON_VERSION: ${{ matrix.python-version }}
14097
timeout-minutes: 45
@@ -144,3 +101,64 @@ jobs:
144101
coverage combine .coverage*
145102
coverage xml -i
146103
codecov --file coverage.xml
104+
105+
dist:
106+
name: Build AWS Lambda Layer
107+
runs-on: ubuntu-latest
108+
timeout-minutes: 10
109+
110+
steps:
111+
- uses: actions/checkout@v2
112+
- uses: actions/setup-node@v1
113+
- uses: actions/setup-python@v2
114+
with:
115+
python-version: 3.9
116+
117+
- run: |
118+
echo "Creating directory containing Python SDK Lambda Layer"
119+
pip install virtualenv
120+
make aws-lambda-layer
121+
122+
echo "Saving SDK_VERSION for later"
123+
export SDK_VERSION=$(grep "VERSION = " sentry_sdk/consts.py | cut -f3 -d' ' | tr -d '"')
124+
echo "SDK_VERSION=$SDK_VERSION"
125+
echo "SDK_VERSION=$SDK_VERSION" >> $GITHUB_ENV
126+
127+
- uses: actions/upload-artifact@v3
128+
with:
129+
name: ${{ github.sha }}
130+
path: |
131+
dist-serverless/*
132+
133+
- uses: getsentry/action-build-aws-lambda-extension@v1
134+
with:
135+
artifact_name: ${{ github.sha }}
136+
zip_file_name: sentry-python-serverless-${{ env.SDK_VERSION }}.zip
137+
138+
- name: Upload Zip
139+
uses: actions/upload-artifact@v3
140+
with:
141+
name: ${{ github.sha }}
142+
path: sentry-python-serverless-${{ env.SDK_VERSION }}.zip
143+
144+
docs:
145+
name: Build SDK API Doc
146+
runs-on: ubuntu-latest
147+
timeout-minutes: 10
148+
149+
steps:
150+
- uses: actions/checkout@v2
151+
- uses: actions/setup-node@v1
152+
- uses: actions/setup-python@v2
153+
with:
154+
python-version: 3.9
155+
156+
- run: |
157+
pip install virtualenv
158+
make apidocs
159+
cd docs/_build && zip -r gh-pages ./
160+
161+
- uses: actions/upload-artifact@v2
162+
with:
163+
name: ${{ github.sha }}
164+
path: docs/_build/gh-pages.zip

Makefile

+5-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ help:
99
@echo "make test: Run basic tests (not testing most integrations)"
1010
@echo "make test-all: Run ALL tests (slow, closest to CI)"
1111
@echo "make format: Run code formatters (destructive)"
12-
@echo "make aws-lambda-layer-build: Build serverless ZIP dist package"
12+
@echo "make aws-lambda-layer: Build AWS Lambda layer directory for serverless integration"
1313
@echo
1414
@echo "Also make sure to read ./CONTRIBUTING.md"
1515
@false
@@ -19,9 +19,8 @@ help:
1919
$(VENV_PATH)/bin/pip install tox
2020

2121
dist: .venv
22-
rm -rf dist build
22+
rm -rf dist dist-serverless build
2323
$(VENV_PATH)/bin/python setup.py sdist bdist_wheel
24-
2524
.PHONY: dist
2625

2726
format: .venv
@@ -46,7 +45,6 @@ lint: .venv
4645
echo "Bad formatting? Run: make format"; \
4746
echo "================================"; \
4847
false)
49-
5048
.PHONY: lint
5149

5250
apidocs: .venv
@@ -60,8 +58,8 @@ apidocs-hotfix: apidocs
6058
@$(VENV_PATH)/bin/ghp-import -pf docs/_build
6159
.PHONY: apidocs-hotfix
6260

63-
aws-lambda-layer-build: dist
61+
aws-lambda-layer: dist
6462
$(VENV_PATH)/bin/pip install urllib3
6563
$(VENV_PATH)/bin/pip install certifi
66-
$(VENV_PATH)/bin/python -m scripts.build_awslambda_layer
67-
.PHONY: aws-lambda-layer-build
64+
$(VENV_PATH)/bin/python -m scripts.build_aws_lambda_layer
65+
.PHONY: aws-lambda-layer

scripts/build_aws_lambda_layer.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import os
2+
import shutil
3+
import subprocess
4+
import tempfile
5+
6+
from sentry_sdk.consts import VERSION as SDK_VERSION
7+
8+
DIST_PATH = "dist" # created by "make dist" that is called by "make aws-lambda-layer"
9+
PYTHON_SITE_PACKAGES = "python" # see https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path
10+
11+
12+
class LayerBuilder:
13+
def __init__(
14+
self,
15+
base_dir, # type: str
16+
):
17+
# type: (...) -> None
18+
self.base_dir = base_dir
19+
self.python_site_packages = os.path.join(self.base_dir, PYTHON_SITE_PACKAGES)
20+
21+
def make_directories(self):
22+
# type: (...) -> None
23+
os.makedirs(self.python_site_packages)
24+
25+
def install_python_packages(self):
26+
# type: (...) -> None
27+
sentry_python_sdk = os.path.join(
28+
DIST_PATH,
29+
f"sentry_sdk-{SDK_VERSION}-py2.py3-none-any.whl", # this is generated by "make dist" that is called by "make aws-lamber-layer"
30+
)
31+
subprocess.run(
32+
[
33+
"pip",
34+
"install",
35+
"--no-cache-dir", # always access PyPI
36+
"--quiet",
37+
sentry_python_sdk,
38+
"--target",
39+
self.python_site_packages,
40+
],
41+
check=True,
42+
)
43+
44+
def create_init_serverless_sdk_package(self):
45+
# type: (...) -> None
46+
"""
47+
Method that creates the init_serverless_sdk pkg in the
48+
sentry-python-serverless zip
49+
"""
50+
serverless_sdk_path = (
51+
f"{self.python_site_packages}/sentry_sdk/"
52+
f"integrations/init_serverless_sdk"
53+
)
54+
if not os.path.exists(serverless_sdk_path):
55+
os.makedirs(serverless_sdk_path)
56+
shutil.copy(
57+
"scripts/init_serverless_sdk.py", f"{serverless_sdk_path}/__init__.py"
58+
)
59+
60+
61+
def build_layer_dir():
62+
with tempfile.TemporaryDirectory() as base_dir:
63+
layer_builder = LayerBuilder(base_dir)
64+
layer_builder.make_directories()
65+
layer_builder.install_python_packages()
66+
layer_builder.create_init_serverless_sdk_package()
67+
68+
shutil.copytree(base_dir, "dist-serverless")
69+
70+
71+
if __name__ == "__main__":
72+
build_layer_dir()

scripts/build_awslambda_layer.py

-117
This file was deleted.

0 commit comments

Comments
 (0)