简体   繁体   中英

Bitbucket pipeline to deploy to Azure VM using Azure-cli command cannot access to script file

I have my source code in Bitbucket and I'm using bitbucket pipeline to build and deploy my Web application to Azure VM.

I'm not using Azure Web Application because of constraint into the use of third parties tools.

I'm stuck on how to use a script file into my Azure cli run command.

Actual error is:

"/opt/atlassian/pipelines/agent/build/SetupSimpleSite.ps1 : The term \n'/opt/atlassian/pipelines/agent/build/SetupSimpleSite.ps1' is not recognized as the name of a cmdlet, function, script \nfile, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct \nand try again.\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\1.1.8\\Downloads\\script1.ps1:1 char:1\n+ /opt/atlassian/pipelines/agent/build/SetupSimpleSite.ps1

my pipeline code:

    test-azure-cli-pipeline:
    - step:
        name: "Display Azure account"
        deployment: staging
        script:
        - pipe: microsoft/azure-cli-run:1.1.0
          variables:
            AZURE_APP_ID: $AZURE_APP_ID
            AZURE_PASSWORD: $AZURE_PASSWORD
            AZURE_TENANT_ID: $AZURE_TENANT_ID
            #CLI_COMMAND: 'az account show'
            CLI_COMMAND: 'az vm run-command invoke --command-id RunPowerShellScript --name $AZURE_VM_NAME --resource-group $AZURE_RESOURCE_GROUP_NAME --scripts $BITBUCKET_CLONE_DIR/SetupSimpleSite.ps1'
      

The script SetupSimpleSite.ps1 is located at the root of my Git repo, same directory than my bitbucket-pipelines.yml

Note that the Azure cli is working fine as the az account show is displaying account details as expected.

I cannot found any relevant information from the repository on how to use source code script from the azure cli, link: https://bitbucket.org/microsoft/azure-cli-run/src/master/

I would like my script Powershell to be kept into my source code.

I finally get it working, you should prefix the file with '@'.

I found the solution from there: Using the 'az vm run-command' with a.ps1 file

So here is the final script working:

    test-azure-cli-pipeline:
    - step:
        name: "Display Azure account"
        deployment: staging
        script:
        - pipe: microsoft/azure-cli-run:1.1.0
          variables:
            AZURE_APP_ID: $AZURE_APP_ID
            AZURE_PASSWORD: $AZURE_PASSWORD
            AZURE_TENANT_ID: $AZURE_TENANT_ID
            CLI_COMMAND: 'az vm run-command invoke --command-id RunPowerShellScript --name $AZURE_VM_NAME --resource-group $AZURE_RESOURCE_GROUP_NAME --scripts @SetupSimpleSite.ps1'
            DEBUG: 'true'

The debug true is useful thanks Chase

First add DEBUG: 'true' Also added pwd; ls; pwd; ls; to your CLI_COMAND verify that your path is correct, this is the error you receive so its the most likely the issue. Fix path and test again.

test-azure-cli-pipeline:
- step:
    name: "Display Azure account"
    deployment: staging
    script:
    - pipe: microsoft/azure-cli-run:1.1.0
      variables:
        AZURE_APP_ID: $AZURE_APP_ID
        AZURE_PASSWORD: $AZURE_PASSWORD
        AZURE_TENANT_ID: $AZURE_TENANT_ID
        CLI_COMMAND: 'pwd ; ls ; az vm run-command invoke --command-id RunPowerShellScript --name $AZURE_VM_NAME --resource-group $AZURE_RESOURCE_GROUP_NAME --scripts $BITBUCKET_CLONE_DIR/SetupSimpleSite.ps1'
        DEBUG: 'true'

If this doesnt work, stop using the azure-cli-pipe, and instead use a step with the az-cli docker image: mcr.microsoft.com/azure-cli and run as a regular script... (I'm not familiar enough with az cli to know how to configure credentials, but you should know.)

- step:
    name: Run AZ script
    image: mcr.microsoft.com/azure-cli
    script:
      - az login -u $AZURE_APP_ID -p $AZURE_PASSWORD #setup az credentials
      - az vm run-command invoke --command-id RunPowerShellScript --name $AZURE_VM_NAME --resource-group $AZURE_RESOURCE_GROUP_NAME --scripts $BITBUCKET_CLONE_DIR/SetupSimpleSite.ps1'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM