Skip to content

Commit

Permalink
Support bundle code and monorepo (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize authored May 10, 2024
2 parents 9ea678b + 14acd56 commit 923971a
Show file tree
Hide file tree
Showing 56 changed files with 2,473 additions and 975 deletions.
146 changes: 146 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
name: 'E2E Tests'

on:
push:
branches: [v3.x]
paths-ignore:
- '**/*.md'
pull_request:
branches: [v3.x]
paths-ignore:
- '**/*.md'

env:
pnpm_version: 8
node_version: 20
debug_identifier: nammatham:*

RESOURCE_IDENTIFIER_NODE18_LINUX_X64: ${{ secrets.RESOURCE_IDENTIFIER_NODE18_LINUX_X64 }}
RESOURCE_IDENTIFIER_NODE18_WIN_X64: ${{ secrets.RESOURCE_IDENTIFIER_NODE18_WIN_X64 }}
RESOURCE_IDENTIFIER_BUN_LINUX_X64: ${{ secrets.RESOURCE_IDENTIFIER_BUN_LINUX_X64 }}
RESOURCE_IDENTIFIER_BUN_WIN_X64: ${{ secrets.RESOURCE_IDENTIFIER_BUN_WIN_X64 }}

jobs:
get-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.deploy-matrix.outputs.matrix }}
deployable_matrix: ${{ steps.deploy-matrix.outputs.deployable_matrix }}
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1

- name: Use Node.js ${{ env.node_version }}
uses: actions/setup-node@v4
with:
node-version: ${{env.node_version }}
- uses: pnpm/action-setup@v3
name: Install pnpm
with:
version: ${{ env.pnpm_version }}

- run: pnpm install
- name: Export the matrix for testing
id: deploy-matrix
run: pnpm exec nx run @infra/azure-functions:github-actions

build:
runs-on: ${{ matrix.os }}
needs: get-matrix
strategy:
matrix:
include: ${{fromJson(needs.get-matrix.outputs.matrix)}}

steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1

- name: Use Node.js ${{ matrix.version }}
if: matrix.runtime == 'node'
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.version }}

- uses: pnpm/action-setup@v3
name: Install pnpm
with:
version: ${{ env.pnpm_version }}
- run: pnpm install

- run: pnpm build

- name: pnpm install again for ensure the cli is available
run: pnpm install

- name: Build the project
run: pnpm exec nx run @examples/with-${{ matrix.runtime }}:build
env:
DEBUG: ${{ env.debug_identifier }}

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.resource_identifier_key }}
path: examples/with-${{ matrix.runtime }}/.nmt/dist
retention-days: 1

deploy:
runs-on: ubuntu-latest
needs:
- build
- get-matrix
timeout-minutes: 10
strategy:
matrix:
include: ${{fromJson(needs.get-matrix.outputs.deployable_matrix)}}

steps:
- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS_E2E_TESTS }}

- name: Download Artifact
uses: actions/download-artifact@v4
with:
name: ${{ matrix.resource_identifier_key }}

- name: Deploy to Azure Functions
uses: Azure/functions-action@v1
with:
app-name: nmt-e2e-${{ matrix.target }}-${{ secrets[matrix.resource_identifier_key] }}
package: .

e2e:
if: always()
runs-on: ubuntu-latest
needs:
- build
- deploy
- get-matrix
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
include: ${{fromJson(needs.get-matrix.outputs.deployable_matrix)}}

steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1

- name: Use Node.js ${{ env.node_version }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.node_version }}

- uses: pnpm/action-setup@v3
name: Install pnpm
with:
version: ${{ env.pnpm_version }}
- run: pnpm install

- name: Run E2E tests
run: pnpm exec nx run @infra/azure-functions:test
env:
AZURE_FUNCTIONS_URL: https://nmt-e2e-${{ matrix.target }}-${{ secrets[matrix.resource_identifier_key] }}.azurewebsites.net
AZURE_FUNCTIONS_API_KEY: ${{ secrets.AZURE_FUNCTIONS_HOST_KEY }}
50 changes: 50 additions & 0 deletions docs/build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Build for Azure Functions

Azure Functions Custom Handler has limitation that need to be use only the single executable file. or using node command

`host.json` when I try to use the `esbuild` for bundling the code, the output file is `main.js`

```json
{
"customHandler": {
"description": {
"defaultExecutablePath": "node",
"arguments": [
"main.js"
]
},
"enableForwardingHttpRequest": true
}
}
```

However, the aim of Nammatham v3 to use Hono framework rather express.js, to use Hono with Node.js, they requires `@hono/node-server` pacakge.
That package requires at least node.js version 18.x and above. otherwise it will throw an error like this

```shell
var Request2 = class extends GlobalRequest {
^
n/a
TypeError: Class extends value undefined is not a constructor or null
```
## Final Solution
Make it single file executable by using `pkg` package or bun using [bun compile to Single-file executable](https://bun.sh/docs/bundler/executables)
For example, after build with the esbuild, then use the `bun` or `pkg` to compile the output file to single file executable,
Example:
```shell
npx pkg -t node18-macos-arm64 main.js
npx pkg -t node18-linux-x64 main.js
```
```shell

## Notes
- When publish into Azure Functions, the package.json will not be included in the final package.
So, it needs to specify the output file manually wheather it is ESM or CommonJS. for example `main.mjs` or `main.cjs`
- Azure Functions node runtime on Custom Handler is using Node.js 16.x (When I write this documentation)

The hono-node-server support the target node.js version 18.x and above
32 changes: 0 additions & 32 deletions docs/infer-handler-type.md

This file was deleted.

1 change: 0 additions & 1 deletion examples/nammatham-v3/.env.sample

This file was deleted.

18 changes: 0 additions & 18 deletions examples/nammatham-v3/.nmt/SimpleHttpTrigger/function.json

This file was deleted.

26 changes: 0 additions & 26 deletions examples/nammatham-v3/nammatham.config.mjs

This file was deleted.

29 changes: 0 additions & 29 deletions examples/nammatham-v3/package.json

This file was deleted.

53 changes: 0 additions & 53 deletions examples/nammatham-v3/src/main.ts

This file was deleted.

File renamed without changes.
6 changes: 6 additions & 0 deletions examples/with-bun/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# @examples/azure-functions


for bun build

bun build ./src/main.ts --compile --target bun-linux-x64 --outfile ./.nmt/dist/main
22 changes: 22 additions & 0 deletions examples/with-bun/nammatham.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @ts-check

/** @type {import('nammatham').NammathamConfigs} */
const nammathamConfig = {
runtime: 'bun',
hostConfig: {
version: '2.0',
logging: {
applicationInsights: {
samplingSettings: {
isEnabled: true,
excludedTypes: 'Request',
},
},
// logLevel: {
// default: 'Trace',
// },
},
},
};

export default nammathamConfig;
Loading

0 comments on commit 923971a

Please sign in to comment.