GitLab Pipeline部署Azure Function后函数未显示求助
我配置了GitLab Pipeline用于发布基于Node.js(TypeScript)的Azure Function,Pipeline执行无报错,但部署完成后,Azure Function App的函数列表中并未显示目标函数。不过开发者通过VS Code部署该函数时可以正常显示。相关配置代码及执行日志如下:
GitLab Pipeline配置
stages: - publish image: mcr.microsoft.com/azure-functions/node:4-node18-core-tools .publish: stage: publish script: - npm install @azure/functions - az login --service-principal -u $AZURE_CLIENT_ID --federated-token `cat $AZURE_FEDERATED_TOKEN_FILE` --tenant $AZURE_TENANT_ID - cd src/functions - | echo '{ "IsEncrypted": false, "Values": { "FUNCTIONS_WORKER_RUNTIME": "node", "AzureWebJobsFeatureFlags": "EnableWorkerIndexing" } }' > local.settings.json - npm run build - func azure functionapp publish example-functions-acc
Pipeline执行日志
$ npm install @azure/functions added 48 packages, and audited 49 packages in 2s 14 packages are looking for funding run `npm fund` for details found 0 vulnerabilities $ az login --service-principal -u $AZURE_CLIENT_ID --federated-token `cat $AZURE_FEDERATED_TOKEN_FILE` --tenant $AZURE_TENANT_ID [ { "cloudName": "AzureCloud", "homeTenantId": "6246c9fe-79e6-4621-bcc2-xxxx", "id": "c606ee86-7f71-4aa3-be45-xxxx", "isDefault": true, "managedByTenants": [], "name": "Example - Acceptance", "state": "Enabled", "tenantId": "6246c9fe-79e6-4621-bcc2-xxx", "user": { "name": "d63551ed-959a-4b9d-90ff-xxx", "type": "servicePrincipal" } } ] $ cd src/functions $ echo '{ # collapsed multi-line command $ npm run build > exampletaskrunner@1.0.0 build > tsc $ func azure functionapp publish example-functions-acc Getting site publishing info... Uploading package... Uploading 534 B [] Upload completed successfully. Deployment completed successfully. Syncing triggers... Functions in example-functions-acc: Cleaning up project directory and file based variables 00:00 Job succeeded
相关配置文件
package.json
{ "name": "exampletaskrunner", "version": "1.0.0", "description": "", "scripts": { "build": "tsc", "watch": "tsc -w", "clean": "rimraf dist", "prestart": "npm run clean && npm run build", "start": "func start", "test": "echo \"No tests yet...\"" }, "dependencies": { "@azure/functions": "^4.0.0" }, "devDependencies": { "@types/node": "^18.x", "typescript": "^4.0.0", "rimraf": "^5.0.0" }, "main": "dist/src/functions/*.js" }
tsconfig.json
{ "compilerOptions": { "module": "commonjs", "target": "es6", "outDir": "dist", "rootDir": ".", "sourceMap": true, "strict": false } }
BlobFunction.ts
import { app, InvocationContext } from '@azure/functions'; export async function airlinesCSVBlobTrigger(myBlob: Buffer, context: InvocationContext): Promise<void> { const url = 'https://my-acc.example.com/api/airlines'; const options = { method: 'PUT', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ token: 'nN4P0aImxxxxxx' }) } await fetch(url, options); context.log("EXAMPLE-FE blob trigger function processed blob", myBlob.length); // context.log("EXAMPLE-FE blob trigger function processed blob \n Blob:", context.bindingData.blobTrigger, "\n Blob Size:", myBlob.length, "Bytes"); } app.storageBlob('airlinesCSVBlobTrigger', { path: 'i5docs/AirlinesMapper.csv', connection: 'example_STORAGE', handler: airlinesCSVBlobTrigger });
排查思路
1. 核实部署包的完整性
从日志看,上传的包仅534B,远小于正常Azure Function包的大小,说明大量必要文件(如编译后的dist目录、node_modules)未被包含。问题根源可能是执行发布命令的目录不对:
- VS Code部署会自动识别项目根目录并打包所有必要文件,而Pipeline中进入
src/functions后执行func publish,仅打包了当前目录的少量文件。建议回到项目根目录执行发布,或在发布命令中明确指定包含的文件:func azure functionapp publish example-functions-acc --publish-local-settings -i
2. 检查package.json的入口配置
当前package.json的main字段为dist/src/functions/*.js,但Azure Functions v4需要明确的入口文件路径,或确保编译后的代码结构与预期一致。可以将main改为具体的入口文件(如dist/BlobFunction.js),或确认编译后的文件路径是否正确。
3. 验证编译结果是否正确
在Pipeline中添加步骤,检查编译后的文件是否生成且结构正确:
npm run build ls -la dist/src/functions/
同时检查项目中是否存在.funcignore文件,若有需确认未误排除dist目录或依赖文件。
4. 同步云端应用设置
local.settings.json中的AzureWebJobsFeatureFlags": "EnableWorkerIndexing"不会自动同步到云端,需确保Azure Function App的应用设置中已添加该配置。可在Pipeline中添加命令:
az functionapp config appsettings set --name example-functions-acc --resource-group <你的资源组名> --settings AzureWebJobsFeatureFlags=EnableWorkerIndexing
5. 使用完整Zip包发布
默认的增量发布可能导致文件缺失,尝试添加--publish-type zip参数强制发布完整包:
func azure functionapp publish example-functions-acc --publish-type zip
6. 匹配Node.js版本
确保Azure Function App的运行时Node.js版本与Pipeline使用的node18一致,可在Azure Portal的Function App配置中查看FUNCTIONS_WORKER_RUNTIME_VERSION,设置为18.x。
内容的提问来源于stack exchange,提问作者Jarek Skoczeń

