Azure / deployment-stacks

Contains Deployment Stacks CLI scripts and releases
MIT License
87 stars 7 forks source link

Question: Creating a Deployment Stacks ADO pipeline on Ubuntu agent? #49

Closed andrew-kelleher closed 2 years ago

andrew-kelleher commented 2 years ago

Has anyone managed to create an Azure DevOps pipeline that installs and runs the Deployment Stack preview cmdlets on a Microsoft-hosted Ubuntu build agent?

I almost have it working but am hitting an issue with running the cmdlets within the pipeline. They simply return an error such as -

The 'Get-AzSubscriptionDeploymentStack' command was found in the module 'Az.Resources', but the module could not be loaded

I've customized the downloaded installation zip file such that it's non-interactive and performs a system-wide installation. This is then added to our repo, unzipped and installed as part of the pipeline.

Example pipeline yaml

The pipeline yaml I'm currently using is -

job: 'Build'
    displayName: 'Build Job'
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - checkout: self

    # Unzip Deployment Stacks installation
    - script: |
        unzip deploymentStacks/AzDeploymentStacksPrivatePreview_custom.zip -d /tmp
        ls /tmp
      displayName: 'Unzip Deployment Stacks'

    # Install AZ module (required on Ubuntu agent)
    - script: |
        sudo /usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command "Install-Module -Name Az -Scope AllUsers -Repository PSGallery -Force -AllowClobber"
      displayName: 'Install Az Powershell Module'

    # Install Deployment Stacks preview cmdlets
    - script: |
        sudo /usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -File "/tmp/AzDeploymentStacksPrivatePreview/AzDeploymentStacksPrivatePreview.ps1"
      displayName: 'Install Deployment Stacks preview cmdlets'

    - task: AzurePowerShell@5
      inputs:
        azureSubscription: 'CorpLZ-serviceconnection-spn'
        scriptType: inlineScript
        inline: |
          echo $PSVersionTable
          Write-host "Verify that Deployment Stacks cmdlets are available..."
          get-module az.accounts -listavailable
          get-module az.resources -listavailable
          Write-host "Verify target subscription is enabled for preview feature..."
          Get-AzProviderFeature -ProviderNamespace Microsoft.Resources
          Write-host "Run test Deployment Stacks cmdlet..."
          Get-AzSubscriptionDeploymentStack
        azurePowerShellVersion: 'latestVersion'
        pwsh: true
      displayName: 'Verify Deployment Stack installation'

Both of the install steps complete successfully. Within the last verify step get-module is showing that the az.resources v9.0.1 module containing the Deployment Stack cmdlets is installed.

However it then fails attempting to run the Get-AzSubscriptionDeploymentStack cmdlet with -

The 'Get-AzSubscriptionDeploymentStack' command was found in the module 'Az.Resources', but the module could not be loaded

If I add Import-module az.resources -version 9.0.1 prior to Get-AzSubscriptionDeploymentStack I receive the following error -

Import-Module: /home/vsts/work/_temp/5f818ce0-9565-48c5-92e6-391c3e150d7b.ps1:11
Line |
  11 |  Import-module az.resources -version 9.0.1
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Assembly with same name is already loaded

Does anyone have ideas or an alternative approach to getting the Deployment Stack cmdlets working in a pipeline?

I appreciate that as this moves out of preview the installation will be much cleaner and this should just work natively. But in the meantime we're keen to see how Deployment Stacks can be integrated into our existing pipelines.

Thanks!

bmoore-msft commented 2 years ago

@Xynoclafe - this is similar to the error we ran into in earlier release but I don't remember the fix/workaround... do we have to remove-module az.resources prior to install in this case?

Xynoclafe commented 2 years ago

I suspect the Az.Resources module is being loaded into the session before the new modules version is installed/loaded. Removing it should fix the problem, as long as no cmdlet from Az.Resources is being run before the installation of new modules is completed.

bmoore-msft commented 2 years ago

@andrew-kelleher - have you had a chance to try this by ensuring the [default] module is not already loaded on the box?

andrew-kelleher commented 2 years ago

@bmoore-msft no, I've not had an opportunity to test this yet.

Now that the Deployment Stacks AZ CLI cmds are available in the latest preview we're likely going to focus our testing there. The AZ CLI tends to align much better with what we're doing with our Linux build agents and pipelines.

Feel free to close this issue unless it'd help anyone else.

bmoore-msft commented 2 years ago

Sounds good - feel free to open/comment as needed...