Use powershell script to customize azure devOps pipeline?

Abdallah Shukor 1 Reputation point
2022-01-04T11:18:51.597+00:00

I have an azure function app that is running on Azure. I am now trying to configure the application based on a powershell script. This powershell script is being used to create azure resources (Ex: KeyVault, ApplicationInsights...) that will be then used by the function app. I created the powershell script and my question here is how can I add the powershell script in the yaml file which is responsible to deploy the application. The powershell script is located in the repository under the name functionapp.ps1 . The idea here is to run the powershell script once the build is finished so in the deploy stage. What I did so far is the following:

stages:
- stage: Build
  displayName: Build stage

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

    steps:
    - task: DotNetCoreCLI@2
      displayName: Build
      inputs:
        command: 'build'
        projects: |
          $(workingDirectory)/*.csproj
        arguments: --output $(System.DefaultWorkingDirectory)/publish_output --configuration Release

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

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

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()

  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: 'development'
    pool:
      vmImage: $(vmImageName)

    strategy:
      runOnce:
        deploy:

          steps:
          - task: CmdLine@2
            inputs:
              script: |
                echo  '$(System.DefaultWorkingDirectory)'
                dir

          - task: PowerShell@2
            inputs:
              targetType: 'filePath'
              filePath: $(System.DefaultWorkingDirectory)/functionapp.ps1

          - task: AzureFunctionApp@1
            displayName: 'Azure functions app deploy'
            inputs:
              azureSubscription: '$(azureSubscription)'
              appType: functionApp
              appName: $(functionAppName)
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'

When the stages are being run and it arrives to this task I get the following error:

##[error]Invalid file path 'D:\a\1\s\functionapp.ps1'. A path to a .ps1 file is required.

I tried using the command line task to check the default working directory and I got the following result: D:\a\1\s

I still don't know what I am missing here and why I am getting this error. Can someone please give me a concrete solution to this problem ?

I ended up having the same error. Can someone please tell me what is the issue here ?

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2022-01-10T08:16:33.337+00:00

    Hi there,

    The default directory where your source code files are downloaded can be referenced from the build-in variable $(Build.SourcesDirectory). This might help you in resolving the issues.

    The syntax for including PowerShell Core is slightly different from the syntax for Windows PowerShell. You can run Windows PowerShell on a Windows build agent. PowerShell Core runs on any platform.

    A list with the build-in variables can be found from the below URL:

    Use predefined variables
    https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

    Here are some links to help you out

    Use a PowerShell script to customize your pipeline
    https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/powershell?view=azure-devops&tabs=yaml

    https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-powershell?view=azure-devops

    -----------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.