0

I am trying to deploy a hello world Azure Functions app through Azure Pipeline but it just wont deploy.

I am using azure-functions-core-tools v4.x which allows me to create a function app using func init and func new ...

Now the folder structure I got is this:

root
host.json
node_modules
package.json
src/function/helloWorld.js <-- JUST A SIMPLE HELLO WORLD FUNCTION
const { app } = require('@azure/functions');

app.http('helloWorld', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    handler: async (request, context) => {
        context.log(`Http function processed request for url "${request.url}"`);

        const name = request.query.get('name') || await request.text() || 'world';

        return { body: `Hello, ${name}!` };
    }
});

have host.json at root.

My pipeline looks like this:

steps:
  - task: UseNode@1
    inputs:
      version: $(nodeVersion)

  - task: Npm@1
    displayName: "Install dependencies"
    inputs:
      command: "install"

  - task: ArchiveFiles@2
    displayName: "Archive function app"
    inputs:
      rootFolderOrFile: "$(System.DefaultWorkingDirectory)" # <-- zip entire project
      includeRootFolder: false
      archiveType: "zip"
      archiveFile: "$(Build.ArtifactStagingDirectory)/functionapp.zip"
      replaceExistingArchive: true

  - task: PublishBuildArtifacts@1
    displayName: "Publish function app artifact"
    inputs:
      PathtoPublish: "$(Build.ArtifactStagingDirectory)"
      ArtifactName: "functionapp"

  - task: AzureFunctionApp@1
    displayName: "Deploy to Azure Function App"
    inputs:
      azureSubscription: $(azureServiceConnection)
      appType: functionAppLinux
      appName: $(functionAppName)
      package: "$(Build.ArtifactStagingDirectory)/functionapp.zip"

I am getting error:

App Service Application settings are already present. Package deployment using ZIP Deploy initiated. ##[error]Failed to deploy web package to App Service. ##[warning]Can't find loc string for key: KuduStackTraceURL ##[error]KuduStackTraceURL [REDACTED - DOESNT OPEN IN BROWSER ANYWAY] ##[error]Error: Error: Failed to deploy web package to App Service. Bad Gateway (CODE: 502) ##[warning]Error: Failed to update deployment history. Error: Method Not Allowed (CODE: 405) App Service Application URL: [REDACTED] Finishing: Deploy to Azure Function App

I have confirmed that Artifact is being built. All other steps in pipeline are running fine but I DONT KNOW WHY this step is not working.

As per GPT, its a folder structure issue. I got this folder structure from azure-functions-core-tools package.

1
  • Did you try deploying the function code using any other IDE or cli? Commented May 9 at 12:47

1 Answer 1

1

Set the deploy task to AzureFunctionApp@2 in the pipeline:

deploy:
          steps:
          - task: AzureFunctionApp@2

I have used below yml code to Build and deploy the Node Js function to Azure using devops pipeline.

Pipeline:

trigger:
- master

variables:

  azureSubscription: 'f94XXXded364'
 
  # Function app name
  functionAppName: 'appname'

  # Environment name
  environmentName: 'environmentname'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
    - task: NodeTool@0
      inputs:
        versionSpec: '20.x'
      displayName: 'Install Node.js'

    - script: |
        if [ -f extensions.csproj ]
        then
            dotnet build extensions.csproj --runtime ubuntu.16.04-x64 --output ./bin
        fi
      displayName: 'Build extensions'

    - script: |
        npm install
        npm run build --if-present
        npm run test --if-present
      displayName: 'Prepare binaries'

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      artifact: drop

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()
  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: $(environmentName)
    pool:
      vmImage: $(vmImageName)
    strategy:
      runOnce:
        deploy:
          steps:
          - task: AzureFunctionApp@2
            inputs:
              connectedServiceNameARM: '$(azureSubscription)'
              appType: 'functionAppLinux'
              appName: $(functionAppName)
              deployToSlotOrASE: true
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
              runtimeStack: 'NODE|20'
              deploymentMethod: 'auto'

Code snippet:

const { app } = require('@azure/functions');

app.http('httpTrigger', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    handler: async (request, context) => {
        context.log(`Http function processed request for url "${request.url}"`);

        const name = request.query.get('name') || await request.text() || 'world';

        return { body: `Hello, ${name}!` };
    }
});

Able to build and deploy the function successfully:

enter image description here

enter image description here

Portal:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I had already solved it. I was using @1 version of task. Instead I needed to use @2 version. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.