BlueBSH
BlueBSH

Reputation: 401

PowerShell script location on release job task

In my local source repo, I have a bunch of power shell scripts that will need to execute as part of a release job.

In order to get them to be executed by the deployment agent, what is the correct way to get the scripts where they need to be and reference them? Do I need to copy the scripts into an artifact and retrieve that artifact on the deploy agent side, or is there another way to do this? All MS documentation says use $(System.DefaultWorkingDirectory) as my base path but on the deployment side the scripts aren't copied to there since it does not download the repo. I am assuming making a artifact and downloading it is the correct way to do this, then they would be in that location.

Upvotes: 0

Views: 536

Answers (2)

Bowman Zhu
Bowman Zhu

Reputation: 7251

In build pipeline:

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)/xxx.ps1' # Put all of the powershell script files here.
    includeRootFolder: true
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
    replaceExistingArchive: true

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

In release pipeline:

enter image description here

steps:
- task: ExtractFiles@1
  displayName: 'Extract files '
  inputs:
    destinationFolder: '$(System.ArtifactsDirectory)'
    cleanDestinationFolder: false

steps:
- powershell: |
   # Write your PowerShell commands here.
   
   Write-Host "Hello World"
   
   cd $(System.ArtifactsDirectory)
   
   dir
   
  displayName: 'PowerShell Script'

Get the ps1 file:

enter image description here

Upvotes: 0

GeralexGR
GeralexGR

Reputation: 3592

You can use a download Repository artifact on your release pipeline and then use the predefined release variables in order to cd into that directory.System.DefaultWorkingDirectory

enter image description here

change dir

cd $(System.DefaultWorkingDirectory)/$(Release.PrimaryArtifactSourceAlias)
ls

Then you can locate code. enter image description here

Release variables documentation:
https://learn.microsoft.com/en-us/azure/devops/pipelines/release/variables?view=azure-devops&tabs=batch

Upvotes: 1

Related Questions