i am starting out with azure devops i have an on prem agent and wanting to update powershell scripts. repo is created and files uploaded. using vscode, created local folder and cloned repo when i modify a file, i want to commit to repo, and also copy the file to a windows server.
task: CopyFiles@2 inputs:
SourceFolder: '\\machines\folder'
Contents: '*.ps1' # Adjust the pattern as needed
TargetFolder: '\\server\DevOps'
CleanTargetFolder: true # Optional: Clean existing files in the target folder
OverWrite: true # Optional: Replace existing files
i can't seem to get the command to copy the files from my sourcefolder - that is step 1
however, moving on after that i would like to be able to have others use this pipeline to copy their files they have modified also, how can i set the sourcefolder to be the users own cloned folder in vscode?
i tried copy files as above, and am getting errors to do with access Error: EPERM: operation not permitted, stat '\machines\folder'
expecting it to pickup file and copy to the server targetfolder
You can commit and push the PowerShell script files to a git repository in your Azure DevOps private project. And set up a CI pipeline for this repository like as below.
# azure-pipelines.yml
trigger:
batch: true
branches:
include:
- main
stages:
- stage: A
jobs:
- job: A1
pool:
name: {pool_name} # The name of agent pool where the self-hosted (on prem) agent is in.
demands: Agent.Name -equals {agent_name} # Can use the demands to specify the particular self-hosted agent by the agent name.
steps:
- task: CopyFiles@2
displayName: 'Copy .ps1 files'
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
Contents: '**/*.ps1'
TargetFolder: '\\server\DevOps'
OverWrite: true
Description:
Ensure the azure-pipelines.yml
file is in the main
branch of the git repository in Azure DevOps.
On the agent machine where the self-hosted agent is installed, ensure you have the permission to directly access the remote file share "\\server\DevOps
". It is recommended mapping the file share as a Network Drive (for example, Drive Z:) on the agent machine so that you can easily access it like as accessing the local drives on the agent machine.
When you install/configure the self-hosted agent on the agent machine, ensure you have provided the username and password of your account that is used to login the machine. Do not use the default 'NT AUTHORITY\NETWORK SERVICE
' (Windows). By this way, the agent will use your provided account to login the machine and run the tasks on the machine, and also have the access to the file share (or the mapped Network Drive).
with above configurations, when new changes are committed and pushed to the main
branch of the git repository in Azure DevOps, the pipeline will be triggered. Then the CopyFiles@2 task will run in the job to copy the .ps1
files into the file share "\\server\DevOps
".
EDIT:
steps:
- task: PowerShell@2
displayName: 'Show Infor'
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
inputs:
pwsh: true
targetType: inline
script: |
$prefix_url = "$(System.CollectionUri)"
$project = "$(System.TeamProject)"
$buildId = $(Build.BuildId)
$url = "${prefix_url}${project}/_apis/build/builds/${buildId}/changes?includeSourceChange=true&api-version=7.0"
$headers = @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
$response=Invoke-RestMethod -Uri $url -Headers $headers
$changesCount = $response.count
Write-Host "The count of commits associated with this build is $changesCount."
$srcDir = "{Source directory that contains all the modified files}"
$destDir = "{Destination directory that the files are copied to}"
$modifiedFiles= git diff HEAD HEAD~$changesCount --name-only
foreach ($file in $modifiedFiles)
{
if ($file -like '**/*.ps1')
{
Write-Host "------------------------------"
$srcFile = "$srcDir/$file"
$destFile = "$destDir/$file"
Write-Host "Copying file: $srcFile ---> $destFile"
$parentDir = Split-Path -Parent $destFile
if (-not (Test-Path $parentDir))
{
Write-Host "Create directory: $parentDir"
New-Item -Path $parentDir -ItemType Directory
}
Copy-Item "$srcFile" -Destination "$destFile"
}
}