Tarta
Tarta

Reputation: 2061

Running azure powershell script through YAML release pipeline

I have my normal and working release pipeline that, by given a certain deployment group, performs some tasks:

enter image description here

  1. Copies a script
  2. Executes that powershell script (on the target machines defined in the Deployment Group)
  3. Deletes the script

I know that YAML doesn't support deployment groups, but (lucky me!) so far my deployment group has only one machine, let's call it MyTestVM .

So what I am trying to achieve mainly is simply executing a powershell script on that vm . Normally, what happenes with the release pipeline, is that you have a tentacle/release agent installed on the VM, your deployment target (which is inside the Deployment Group) is hooked up to that, and your release pipeline (thanks to the Deployment Group specification) is able to use that release agent on the machine and do whatever it wants on the VM itself.

I need the same... but through YAML ! I know there is PowerShellOnTargetMachines command available in YAML but I don't want to use that. It uses PSSession, it requires SSL certificates and many other things. I just want to use the already existing agent on the VM !

What I have in place so far:

pool: 'Private Pool'

steps:
- task: DownloadPipelineArtifact@2
  inputs:
    buildType: 'specific'
    project: 'blahblah'
    definition: 'blah'
    buildVersionToDownload: 'latest'
    targetPath: '$(Pipeline.Workspace)'
    
- task: CopyFiles@2
  displayName: 'Copy Files to: C:\TestScript'
  inputs:
    SourceFolder: '$(Pipeline.Workspace)/Scripts/'
    Contents: '**/*.ps1'
    TargetFolder: 'C:\TestScript'
    CleanTargetFolder: true
    OverWrite: true

The first part just downloads the Artifact containing my script. And then to be honest I am not even sure I need to copy the script in the second part.. first because I don't think it is copying the script to the VM target workspace, but it is copying it to the VM where the azure pipeline agent is installed. And second: I think I can just reference it from my artifact.. but this is not the important part. How can I make my YAML pipeline make use of the release agent installed on the VM in the same way that a normal release pipeline does?

Upvotes: 3

Views: 14107

Answers (2)

Tarta
Tarta

Reputation: 2061

Reached somehow a solution. First of all worth mentioning that since deployment groups don't work with YAML pipelines the way to proceed is to create an Environment and add as resource your target VM.

So I didn't need to create my own hosted agent or anything special since the problem was the target itself and not the agent running the pipeline. By creating an Environment and adding a resource (in my case a VM) to that environment, we create also a new release agent on the target itself. So my target VM will now have 2 release agents: the old one that can be used by normal release pipelines, and the new one, attached to the Environment resource on Azure Devops that can be used by YAML pipelines.

Now I am finally able to hit my VM:

- stage: PerformScriptInVM
  jobs:
  - deployment: VMDeploy
    pool:
      vmImage: 'windows-latest'
    # watch out: this creates an environment if it doesn’t exist
    environment:
      name: My Environment Name
      resourceType: VirtualMachine
    strategy:
      runOnce:
        deploy:
        steps:
        - task: DownloadPipelineArtifact@2
          inputs:
            buildType: 'specific'
            project: 'blahblahblah'
            definition: 'blah'
            buildVersionToDownload: 'latest'
            targetPath: '$(Pipeline.Workspace)'
        - task: PowerShell@2
          displayName: 'PowerShell Script'
          inputs:
            targetType: filePath
            filePath: '$(Pipeline.Workspace)/Scripts/TestScript.ps1'
            arguments: 'whatever your script needs..'

Upvotes: 3

WaitingForGuacamole
WaitingForGuacamole

Reputation: 4301

To get the job to run on the specific release agent you want, you can do two things:

Create a pool and only put your release agent into it.

pool: 'My Pool with only one release agent'

Use an existing pool, and publish/demand a capability for your agent.

On the agent machine itself, add a system environment variable (for example, MyCustomCapability. Give it a value like 1

then your pipeline becomes:

pool:
  name: 'My pool with potentially more than one agent'
  demands: 'MyCustomCapability'

If only this agent has this environment variable set, then only this agent can execute the job

Upvotes: 0

Related Questions