'An error occurred while loading the YAML build pipeline. The string must have at least one character. Parameter name: environmentName

My code is writing in JS using node.js express.

so i upload my code to AZURE, then in configure i chose

Node.js Express Web App to Linux on Azure

but then when running pipeline this problem accure:

An error occurred while loading the YAML build pipeline. The string must have at least one character. Parameter name: environmentName

this is what i have in the azure-pipelines.yml

# Node.js Express Web App to Linux on Azure
# Build a Node.js Express app and deploy it to Azure as a Linux web app.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- master

variables:

  # Azure Resource Manager connection created during pipeline creation
  azureSubscription: 'fbdfe789-60d1-4de0-a5a8-cc90b2084141'

  # Web app name
  webAppName: ''

  # Environment name
  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: '10.x'
      displayName: 'Install Node.js'

    - script: |
        npm install
        npm run build --if-present
        npm run test --if-present
      displayName: 'npm install, build and test'

    - 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: AzureWebApp@1
            displayName: 'Azure Web App Deploy: '
            inputs:
              azureSubscription: $(azureSubscription)
              appType: webAppLinux
              appName: $(webAppName)
              runtimeStack: 'NODE|10.10'
              package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
              startUpCommand: 'npm run start'

what im missing?



Solution 1:[1]

It helps to understand that pipeline run sequence. The first step is to process the pipeline, which expands templates and template parameters, evaluates dependencies, etc. It does this to ensure that the pipeline has permission to access key resources in the pipeline such as Service Connections, Envrionments and Variable Groups before the pipeline can even begin execution.

The environment parameter of the deployment job has to be defined at compile-time. By defining it as a variable using the macro-syntax ($(environmentName)) you've defined that the environment name will be determined at runtime, which is not valid syntax since at compile-time the value of environmentName will be empty.

Currently, you have:

variables:
   environmentName: ''

Switching this to a valid environment will solve the problem.

If your intention of declaring the environment name as a variable is so that you can dynamically determine which environment to use, the correct way would be to use templates.

As a rough example:

# deployment-template.yml
parameters:
- name: environmentName
  type: string

- name: vmImageName
  type: string
  default: 'ubuntu-latest'

jobs:
- deployment: Deploy
  displayName: Deploy
  environment: ${{ parameters.environmentName }}
    pool:
      vmImage: ${{ parameters.vmImageName }}
    strategy:
      runOnce:
        deploy:
          steps:
          - <steps go here >
# pipeline.yml

stages:
- stage: BUILD
  steps:
  - <ci-steps go here>

- stage: DEV
  dependsOn: BUILD
  jobs:
  - template: deployment-template.yml
    parameters:
       environmentName: DEV1

- stage: QA
  dependsOn: DEV
  jobs:
  - template: deployment-template.yml
    parameters:
       environmentName: QA1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1