Skip to content

Commit ed43e33

Browse files
committed
Init
0 parents  commit ed43e33

27 files changed

+951
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
indent_size = 4
10+
indent_style = space
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[Makefile]
15+
indent_style = tab

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.github/ export-ignore
2+
export/ export-ignore
3+
layers/ export-ignore
4+
src/ export-ignore
5+
tests/ export-ignore
6+
.editorconfig export-ignore
7+
.gitattributes export-ignore
8+
.gitignore export-ignore
9+
config.json export-ignore
10+
Makefile export-ignore
11+
bref-layer export-ignore

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
github: Nyholm, mnapoli

.github/workflows/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*.yml]
2+
indent_size = 2

.github/workflows/bin/refresh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$rootDir = __DIR__ . '/../../..';
5+
if (!file_exists($rootDir . '/config.json')) {
6+
echo "Unable to find the `config.json` script in `../../..`.\n";
7+
exit(1);
8+
}
9+
10+
function console_log(string $message): void
11+
{
12+
echo $message.PHP_EOL;
13+
}
14+
15+
/**
16+
* FETCH last SDK version
17+
*/
18+
console_log('Fetching last Bref version');
19+
$package = json_decode(file_get_contents('https://repo.packagist.org/p2/bref/bref.json'), true);
20+
$versions = $package['packages']['bref/bref'];
21+
usort($versions, static function (array $a, array $b) {
22+
return version_compare($b['version_normalized'], $a['version_normalized']);
23+
});
24+
$lastVersion = $versions[0]['version'];
25+
console_log('Last Bref version is '.$lastVersion);
26+
27+
/**
28+
* FETCH current generated version
29+
*/
30+
$config = json_decode(file_get_contents($rootDir . '/config.json'), true);
31+
$currentVersion = $config['latest_version'];
32+
33+
console_log('Current generated version is '.$currentVersion);
34+
if (version_compare($currentVersion, $lastVersion, '>=')) {
35+
console_log('Nothing to do.');
36+
echo PHP_EOL, '::set-output name=last::same', PHP_EOL;
37+
38+
exit;
39+
}
40+
41+
$config['latest_version'] = $lastVersion;
42+
\file_put_contents($rootDir . '/config.json', \json_encode($config, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
43+
44+
echo PHP_EOL, '::set-output name=last::'.$lastVersion, PHP_EOL;

.github/workflows/ci.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- layers/**
7+
8+
jobs:
9+
matrix:
10+
name: Find layers
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
with:
16+
fetch-depth: 0
17+
18+
- name: Fetch master from where the PR started
19+
run: git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
20+
21+
- name: Find layers
22+
id: find-layers
23+
run: echo "::set-output name=list::$(git diff --name-only origin/${{ github.base_ref }} HEAD | grep layers/ | cut -d / -f 2 | sort | uniq | jq -R -s -c 'split("\n")[:-1]')"
24+
25+
outputs:
26+
# Make the outputs accessible outside this job
27+
list: ${{ steps.find-layers.outputs.list }}
28+
29+
build:
30+
needs: matrix
31+
name: Build and test layer
32+
runs-on: ubuntu-latest
33+
env:
34+
DOCKER_BUILDKIT: '1'
35+
strategy:
36+
fail-fast: false
37+
matrix:
38+
layer: ${{ fromJson(needs.matrix.outputs.list) }}
39+
40+
steps:
41+
- name: Checkout code
42+
uses: actions/checkout@v2
43+
44+
- name: Build docker images
45+
run: layer=${{ matrix.layer }} make docker-images
46+
47+
- name: Test images
48+
run: layer=${{ matrix.layer }} make test

.github/workflows/watch.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Watch for changes
2+
3+
on:
4+
schedule:
5+
- cron: '0 0,6,12,18 * * *'
6+
7+
jobs:
8+
bref:
9+
name: Update to latest Bref
10+
runs-on: ubuntu-20.04
11+
12+
steps:
13+
- name: Set up PHP
14+
uses: shivammathur/setup-php@v2
15+
with:
16+
php-version: 8.0
17+
coverage: none
18+
19+
- name: Checkout code
20+
uses: actions/checkout@v2
21+
with:
22+
ref: main
23+
fetch-depth: 0
24+
25+
- name: Get last version
26+
id: fetch_version
27+
run: .github/workflows/bin/refresh
28+
29+
- name: Download dependencies
30+
if: ${{ steps.fetch_version.outputs.last != 'same' }}
31+
uses: ramsey/composer-install@v1
32+
33+
- name: Build images with latest version
34+
if: ${{ steps.fetch_version.outputs.last != 'same' }}
35+
run: |
36+
make layers
37+
make test
38+
39+
- name: Publish layers
40+
if: ${{ steps.fetch_version.outputs.last != 'same' }}
41+
env:
42+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }}
43+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_ACCESS_SECRET }}
44+
run: |
45+
php ./bref-layer publish
46+
php ./bref-layer list
47+
48+
- name: Commit changes
49+
if: ${{ steps.fetch_version.outputs.last != 'same' }}
50+
run: |
51+
git config --local user.email "[email protected]"
52+
git config --local user.name "GitHub Actions"
53+
git add layers.json config.json
54+
git commit -m "Updated to Bref ${{ steps.fetch_version.outputs.last }}"
55+
56+
- name: Push changes
57+
if: ${{ steps.fetch_version.outputs.last != 'same' }}
58+
run: |
59+
git push origin main
60+
git tag -a ${{ steps.fetch_version.outputs.last }} -m "Bref ${{ steps.fetch_version.outputs.last }}"
61+
git push origin ${{ steps.fetch_version.outputs.last }}
62+

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
composer.lock
3+
.php_cs.cache

Makefile

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
SHELL := /bin/bash
2+
layer ?= *
3+
resolve_php_versions = $(or $(php_versions),`jq -r '.php | join(" ")' ${1}/config.json`)
4+
5+
define build_docker_image
6+
docker build -t php-runtime/${1}-php-${2} --build-arg PHP_VERSION=${2} ${DOCKER_BUILD_FLAGS} ${1}
7+
endef
8+
9+
docker-images:
10+
if [ "${layer}" != "*" ]; then test -d layers/${layer}; fi
11+
set -e; \
12+
for dir in layers/${layer}; do \
13+
for php_version in $(call resolve_php_versions,$${dir}); do \
14+
echo "###############################################"; \
15+
echo "###############################################"; \
16+
echo "### Building $${dir} PHP$${php_version}"; \
17+
echo "###"; \
18+
$(call build_docker_image,$${dir},$${php_version}) ; \
19+
echo ""; \
20+
done \
21+
done
22+
23+
test: docker-images
24+
if [ "${layer}" != "*" ]; then test -d layers/${layer}; fi
25+
set -e; \
26+
for dir in layers/${layer}; do \
27+
for php_version in $(call resolve_php_versions,$${dir}); do \
28+
echo "###############################################"; \
29+
echo "###############################################"; \
30+
echo "### Testing $${dir} PHP$${php_version}"; \
31+
echo "###"; \
32+
docker run --entrypoint= --rm -v $$(pwd)/$${dir}:/var/task php-runtime/$${dir}-php-$${php_version} /opt/bin/php /var/task/test.php ; \
33+
if docker run --entrypoint= --rm -v $$(pwd)/$${dir}:/var/task php-runtime/$${dir}-php-$${php_version} /opt/bin/php -v 2>&1 >/dev/null | grep -q 'Unable\|Warning'; then exit 1; fi ; \
34+
echo ""; \
35+
echo " - Test passed"; \
36+
echo ""; \
37+
done \
38+
done;
39+
40+
# The PHP runtimes
41+
layers: docker-images
42+
if [ "${layer}" != "*" ]; then test -d layers/${layer}; fi
43+
PWD=pwd
44+
rm -rf export/layer-${layer}.zip || true
45+
mkdir -p export/tmp
46+
set -e; \
47+
for dir in layers/${layer}; do \
48+
for php_version in $(call resolve_php_versions,${PWD}/$${dir}); do \
49+
echo "###############################################"; \
50+
echo "###############################################"; \
51+
echo "### Exporting $${dir} PHP$${php_version}"; \
52+
echo "###"; \
53+
cd ${PWD} ; rm -rf export/tmp/${layer} || true ; cd export/tmp ; \
54+
CID=$$(docker create --entrypoint=scratch php-runtime/$${dir}-php-$${php_version}) ; \
55+
docker cp $${CID}:/opt . ; \
56+
docker rm $${CID} ; \
57+
cd ./opt ; \
58+
zip --quiet -X --recurse-paths ../../`echo "$${dir}-php-$${php_version}" | sed -e "s/layers\//layer-/g"`.zip . ; \
59+
echo ""; \
60+
done \
61+
done
62+
rm -rf export/tmp
63+
64+
clean:
65+
rm -f export/layer-*
66+
67+
publish: layers
68+
php ./bref-layer publish
69+
php ./bref-layer list

Readme.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Bref layers for PHP Runtime
2+
3+
This repository provides Bref layers to run Symfony Runtime applications on
4+
AWS Lambda. It is automatically updated to sync with the changes in [Bref](https://github.com/brefphp/bref).
5+
These layers are just replacing Bref's "function layer" with a new boostrap file.
6+
See the [Dockerfile](https://github.com/php-runtime/bref-layer/blob/main/layers/bref/Dockerfile).
7+
8+
You will find more information how to run your applicaiton with the runtime component
9+
at https://github.com/php-runtime/bref
10+
11+
## Install and configure
12+
13+
```cli
14+
composer require runtime/bref-layer
15+
```
16+
17+
```yaml
18+
# serverless.yml
19+
service: appgit init
20+
21+
provider:
22+
name: aws
23+
region: us-east-1
24+
runtime: provided.al2
25+
26+
plugins:
27+
- ./vendor/runtime/bref-layer # <----- Add the extra Serverless plugin
28+
29+
functions:
30+
website:
31+
handler: public/index.php
32+
layers:
33+
- ${runtime-bref:php-80}
34+
events:
35+
- httpApi: '*'
36+
```
37+
38+
You will use the same layer for console applications, PSR-11, PSR-15, Laravel or
39+
Symfony applications. Anything the Runtime component supports.
40+
41+
## FAQ
42+
43+
#### Can I use custom php extensions?
44+
45+
Yes, you can. See the [`bref/extra-php-extensions`](https://github.com/brefphp/extra-php-extensions)
46+
package.
47+
48+
#### Do I need to install bref/bref?
49+
50+
The [`bref/bref`](https://github.com/brefphp/bref) package includes both layers
51+
and a lot of features to make it easier to write applications on AWS Lambda.
52+
You do not **have to** include `bref/bref` if you only want the layers. But most
53+
HTTP applications probably will.
54+
55+
#### Why not include this package in bref/bref?
56+
57+
There is [an open PR](https://github.com/brefphp/bref/pull/889) to do just that.
58+
There is also [an open PR](https://github.com/brefphp/bref/pull/1034) to make the
59+
function layer support the runtime component natively.
60+
61+
Until any of these PRs are merged, we have this layer to help making a good experience
62+
as possible for the runtime users.
63+
64+
#### Why not use the layer from bref/extra-php-extensions?
65+
66+
The [bref/extra-php-extensions](https://github.com/brefphp/extra-php-extensions)
67+
has a layer called `${bref-extra:symfony-runtime-php-74}`. It adds the custom bootstrap
68+
file needed for the Runtime component.
69+
70+
AWS has a hard limit on max 5 layers per function. So instead of using two layers
71+
(`${bref:layer.php-80}` + `${bref-extra:symfony-runtime-php-74}`) one can use only
72+
`${runtime-bref:php-80}`.
73+
74+
#### What is your relation to Bref?
75+
76+
We **LOVE** Bref. They are the best ever. Consider sponsoring [Matthieu Napoli
77+
](https://github.com/mnapoli) for his work.
78+
79+
## Maintainer notes
80+
81+
### Testing the layer
82+
83+
```
84+
# Test all layers and PHP versions
85+
make test
86+
87+
# Test only a single layer
88+
layer=bref make test
89+
90+
# Test a single layer on a single PHP version
91+
layer=bref php_versions=74 make test
92+
```
93+
94+
### Deploy new versions
95+
96+
#### The manual way
97+
98+
```
99+
export AWS_PROFILE=my_profile
100+
make publish
101+
git add layers.json
102+
git commit -m "New version of layers"
103+
git push
104+
```
105+
106+
## Lambda layers in details
107+
108+
> **Notice:** this section is only useful if you want to learn more.
109+
110+
The lambda layers follow this pattern:
111+
112+
```
113+
arn:aws:lambda:<region>:403367587399:layer:bref-<layer-name>:<layer-version>
114+
```
115+
116+
See the [latest layer versions](https://raw.githubusercontent.com/php-runtime/bref-layer/main/layers.json).

0 commit comments

Comments
 (0)