Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Build process to support bun and E2E #140

Merged
merged 15 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ name: 'E2E Tests'

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


env:
pnpm_version: 8
Expand All @@ -18,7 +17,8 @@ env:

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:
Expand Down
16 changes: 0 additions & 16 deletions examples/with-bun/nammatham.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@ const nammathamConfig = {
runtime: 'bun',
hostConfig: {
version: '2.0',
extensionBundle: {
id: 'Microsoft.Azure.Functions.ExtensionBundle',
version: '[3.15.0, 4.0.0)',
},
extensions: {
http: {
routePrefix: 'api',
},
},
customHandler: {
description: {
defaultExecutablePath: '../node_modules/.bin/tsx',
arguments: ['watch', '../src/main.ts'],
},
enableForwardingHttpRequest: true,
},
logging: {
applicationInsights: {
samplingSettings: {
Expand Down
2 changes: 1 addition & 1 deletion examples/with-bun/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
"tsx": "^4.9.1",
"typescript": "^5.4.5"
}
}
}
23 changes: 0 additions & 23 deletions examples/with-node/nammatham.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,8 @@
/** @type {import('nammatham').NammathamConfigs} */
const nammathamConfig = {
runtime: 'node',
// buildOption: {
// target: {
// arch: 'x64',
// platform: 'linux',
// runtime: 'node18',
// },
// },
hostConfig: {
version: '2.0',
extensionBundle: {
id: 'Microsoft.Azure.Functions.ExtensionBundle',
version: '[3.15.0, 4.0.0)',
},
extensions: {
http: {
routePrefix: 'api',
},
},
// customHandler: {
// description: {
// defaultExecutablePath: '../node_modules/.bin/tsx',
// arguments: ['watch', '../src/main.ts'],
// },
// enableForwardingHttpRequest: true,
// },
logging: {
applicationInsights: {
samplingSettings: {
Expand Down
2 changes: 1 addition & 1 deletion examples/with-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"tsx": "^4.9.1",
"typescript": "^5.4.5"
}
}
}
99 changes: 98 additions & 1 deletion infra/azure-functions/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,102 @@
## Setup Infra

```
az ad sp create-for-rbac --display-name "nammatham-github-actions-e2e-tests" --role "Contributor" --scopes "/subscriptions/29523625-6fa5-4d9a-86bc-da000544be7d/resourceGroups/rg-nammatham-nmt-e2e-node18-linux-x64-f04f60dfb53e02" --json-auth
az ad sp create-for-rbac --display-name "nammatham-github-actions-e2e-tests" --role "Contributor" --scopes "/subscriptions/29523625-6fa5-4d9a-86bc-da000544be7d/resourceGroups/rg-nammatham-nmt-e2e-node18-linux-x64-xxxxxxxxxxxxxx" --json-auth
```

## How to add new Azure Functions to the project

### 1. Add more config in `src/config.ts`

For example:

```ts
export const infraConfigs = createInfraConfig(
{
// ....
// ^ the above is the existing infra config
'bun-linux-x64': process.env.RESOURCE_IDENTIFIER_BUN_LINUX_X64,
},
[
{
platform: 'linux',
arch: 'x64',
runtime: 'bun',
mode: 'create',
isDeployable: true,
},
]
);
```

### 2. Add more `RESOURCE_IDENTIFIER_BUN_LINUX_X64` in the `.env` file
using this: https://codebeautify.org/generate-random-hexadecimal-numbers

```env
RESOURCE_IDENTIFIER_BUN_LINUX_X64=6a2026b8a8736f
```

### 3. Run Plan

```sh
pnpm plan
```

the result will be

```
Running in plan mode: true
┌───┬──────────┬───────┬─────────┬─────────┬──────────────┬────────────────────┬────────┐
│ │ platform │ arch │ runtime │ version │ isDeployable │ resourceIdentifier │ mode │
├───┼──────────┼───────┼─────────┼─────────┼──────────────┼────────────────────┼────────┤
│ 0 │ linux │ x64 │ node │ 18 │ true │ c95a3623a9a936 │ │
│ 1 │ win │ x64 │ node │ 18 │ true │ 61abf3b30d9327 │ │
│ 2 │ macos │ arm64 │ node │ 18 │ false │ undefined │ │
│ 3 │ linux │ x64 │ bun │ │ true │ 21a4f3b0855015 │ create │
└───┴──────────┴───────┴─────────┴─────────┴──────────────┴────────────────────┴────────┘
Skipping infra config with target node18-linux-x64
Skipping infra config with target node18-win-x64
Skipping infra config with target node18-macos-arm64
```

### 3. Run Apply

```sh
pnpm apply
```

### 4. Setup Github Actions & Secrets

add more env in `.github/workflows/e2e.yml`

```yml
env:
# ...
# ^ the above is the existing env
RESOURCE_IDENTIFIER_BUN_LINUX_X64: ${{ secrets.RESOURCE_IDENTIFIER_BUN_LINUX_X64 }}
```

then use the resource identifier in the github secret `RESOURCE_IDENTIFIER_BUN_LINUX_X64`, from step 2 (`6a2026b8a8736f`).

### 5. Change infra mode config in `src/config.ts`, by remove the `mode: 'create'`

For example:

```ts
export const infraConfigs = createInfraConfig(
{
// ....
// ^ the above is the existing infra config
'bun-linux-x64': process.env.RESOURCE_IDENTIFIER_BUN_LINUX_X64,
},
[
{
platform: 'linux',
arch: 'x64',
runtime: 'bun',
// mode: 'create',
isDeployable: true,
},
]
);
```
4 changes: 2 additions & 2 deletions infra/azure-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"test-os": "tsx src/test-os.ts",
"apply": "bun run src/main.ts",
"plan": "PLAN_MODE=true bun run src/main.ts",
"test": "bun test",
"test": "bun test --timeout 120000",
"github-actions": "bun run src/github-actions.ts",
"lint": "tsc --noEmit && eslint ./src && prettier -c src",
"lint:fix": "eslint --fix ./src && prettier -w src"
Expand All @@ -24,4 +24,4 @@
"engines": {
"bun": "^1.1.0"
}
}
}
19 changes: 18 additions & 1 deletion infra/azure-functions/src/azure-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,29 @@ export async function createFunctionApp(infraConfig: InfraEnvConfig, options: In
await $`az functionapp create --name ${resourceName.prefix} --storage-account ${resourceName.shortPrefix} --consumption-plan-location ${location} --resource-group ${resourceGroup} --os-type ${osType} --runtime custom --functions-version ${functionsVersion}`;

console.log(`Set Function Key for ${resourceName.prefix} `);
await $`az functionapp keys set --name ${resourceName.prefix} --resource-group ${resourceGroup} --key-name github_actions --key-value ${process.env.AZURE_FUNCTIONS_API_KEY}`;
await $`az functionapp keys set --name ${resourceName.prefix} --resource-group ${resourceGroup} --key-name github_actions --key-type functionKeys --key-value ${process.env.AZURE_FUNCTIONS_API_KEY}`;

console.log('Function app created');
console.log('-'.repeat(80));
}

export async function assignRoleAssignment(infraConfig: InfraEnvConfig, options: InfraOptions) {
const resourceName = getResourceName(infraConfig);
console.log('-'.repeat(80));

if (options.isPlanMode) {
console.log('Plan mode enabled, skipping resource creation');
return;
}
console.log('Assigning role assignment');

const resourceGroup = `rg-nammatham-${resourceName.prefix}`;
const role = 'Contributor';
const assignee = process.env.AZURE_APPLICATION_ID;
const scope = `/subscriptions/${process.env.AZURE_SUBSCRIPTION_ID}/resourceGroups/${resourceGroup}`;
await $`az role assignment create --role ${role} --assignee ${assignee} --scope ${scope}`;
}

export async function destroyFunctionApp(infraConfig: InfraEnvConfig, options: InfraOptions) {
console.log('-'.repeat(80));
const resourceName = getResourceName(infraConfig);
Expand Down
54 changes: 26 additions & 28 deletions infra/azure-functions/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,49 @@ export const infraConfigs = createInfraConfig(
{
'node18-linux-x64': process.env.RESOURCE_IDENTIFIER_NODE18_LINUX_X64,
'node18-win-x64': process.env.RESOURCE_IDENTIFIER_NODE18_WIN_X64,
'bun-linux-x64': process.env.RESOURCE_IDENTIFIER_BUN_LINUX_X64,
'bun-win-x64': process.env.RESOURCE_IDENTIFIER_BUN_WIN_X64,
},
[
// --- Node 18 ---
{
platform: 'linux',
arch: 'x64',
runtime: 'node18',
runtime: 'node',
version: 18,
isDeployable: true,
},
{
platform: 'win',
arch: 'x64',
runtime: 'node18',
runtime: 'node',
version: 18,
isDeployable: true,
},
{
platform: 'macos',
arch: 'arm64',
runtime: 'node18',
runtime: 'node',
version: 18,
},
// --- Bun ----
// {
// platform: 'linux',
// arch: 'x64',
// runtime: 'bun',
// },
// {
// platform: 'linux',
// arch: 'arm64',
// runtime: 'bun',
// },
// {
// platform: 'win',
// arch: 'x64',
// runtime: 'bun',
// },
// {
// platform: 'win',
// arch: 'x64',
// runtime: 'node18',
// },
// {
// platform: 'macos',
// arch: 'x64',
// runtime: 'bun',
// },
{
platform: 'linux',
arch: 'x64',
runtime: 'bun',
isDeployable: true,
},
{
platform: 'win',
arch: 'x64',
runtime: 'bun',
isDeployable: true,
},
{
platform: 'macos',
arch: 'x64',
runtime: 'bun',
isDeployable: false,
},
]
);
3 changes: 2 additions & 1 deletion infra/azure-functions/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toTarget } from './utils';
import { infraConfigs } from './config';
import { createFunctionApp, destroyFunctionApp } from './azure-command';
import { assignRoleAssignment, createFunctionApp, destroyFunctionApp } from './azure-command';

const isPlanMode = process.env.PLAN_MODE === 'true';

Expand All @@ -15,6 +15,7 @@ for (const infraConfig of infraConfigs) {
}
if (infraConfig.mode === 'create') {
await createFunctionApp(infraConfig, { isPlanMode });
await assignRoleAssignment(infraConfig, { isPlanMode });
} else if (infraConfig.mode === 'destroy') {
await destroyFunctionApp(infraConfig, { isPlanMode });
} else {
Expand Down
4 changes: 4 additions & 0 deletions infra/azure-functions/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { TargetOptions } from 'nammatham';

export interface InfraEnvConfig extends TargetOptions {
/**
* Runtime
*/
runtime?: 'bun' | 'node';
/**
* Each infra config will be tested for build of specific target options
* Some infra configs may not be deployable, so we can skip them
Expand Down
2 changes: 1 addition & 1 deletion infra/azure-functions/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { InfraEnvConfig } from './types';

export function toTarget(infraConfig: InfraEnvConfig): string {
return `${infraConfig.runtime}-${infraConfig.platform}-${infraConfig.arch}`;
return `${infraConfig.runtime}${infraConfig.version ?? ''}-${infraConfig.platform}-${infraConfig.arch}`;
}

export function fallbackResourceIdentifier(infraConfig: InfraEnvConfig): string {
Expand Down
Loading