我正在开发一个 Azure DevOps 管道,需要从存储库中获取 SQL 文件 (
up5.sql
) 并在虚拟机 (VM) 上执行它。管道和 SQL 文件位于同一存储库中。
这是我当前用来执行虚拟机上已存在的 SQL 文件的 YAML:
trigger:
- main
pool:
vmImage: 'windows-latest'
stages:
- stage: Deploy
displayName: 'Deploy to Production'
jobs:
- deployment: RunPowerShellOnVM
displayName: 'Run PowerShell Script on VM'
environment:
name: 'Production'
resourceType: VirtualMachine
strategy:
runOnce:
deploy:
steps:
- task: PowerShellOnTargetMachines@3
displayName: 'Run SQL Deployment Script on VM'
inputs:
Machines: $(Machine)
UserName: $(UserName)
UserPassword: $(UserPassword)
InlineScript: |
$dbplain = $(UserPassword)
$dblist = C:\mybin\bin\mysql.exe -u myuser -p"$dbplain" -e 'show databases;'
foreach ($dbname in $dblist) {
if ($dbname.ToLower().Contains('prod')) {
Write-Host "Skipping Database: $dbname"
} else {
Write-Host "Updating $dbname..."
C:\Sites\UpdatePackage\sqlRunDeployment.cmd $dbplain $dbname
}
}
我不想使用虚拟机上已有的硬编码 SQL 文件,而是:
up5.sql
)。mysql
命令直接在虚拟机上执行 SQL 文件。获取SQL文件:
临时执行:
这是我想要实现的目标:
trigger:
- main
pool:
vmImage: 'windows-latest'
stages:
- stage: Deploy
displayName: 'Deploy to Production'
jobs:
- deployment: RunPowerShellOnVM
displayName: 'Run SQL Script from Repo on VM'
environment:
name: 'Production'
resourceType: VirtualMachine
strategy:
runOnce:
deploy:
steps:
- checkout: self #getting issue in this
displayName: 'Checkout Repository'
- task: PowerShellOnTargetMachines@3
displayName: 'Run SQL File on VM'
inputs:
Machines: $(Machine)
UserName: $(UserName)
UserPassword: $(UserPassword)
InlineScript: |
$sqlFile = "$(Pipeline.Workspace)\Batch\up5.sql"
$dbplain = $(UserPassword)
C:\mybin\bin\mysql.exe -u myuser -p"$dbplain" --database=mydb < $sqlFile
使用
PowerShellOnTargetMachines
任务,您可以 从存储库复制特定文件并在虚拟机上执行它。
感谢@RuiJarimba 的深思熟虑的评论和提出的富有洞察力的问题。
使用 PowerShell 将文件从管道工作区复制到目标虚拟机。
YAML 管道:
trigger:
- main
pool:
vmImage: 'windows-latest'
stages:
- stage: Deploy
displayName: 'Deploy to Production'
jobs:
- deployment: RunPowerShellOnVM
displayName: 'Run PowerShell Script on VM'
environment:
name: 'Production'
resourceType: VirtualMachine
strategy:
runOnce:
deploy:
steps:
- checkout: self # Checkout the repository (this will pull all files, including SQL)
- task: PowerShellOnTargetMachines@3
displayName: 'Copy and Run SQL File on VM'
inputs:
Machines: $(Machine)
UserName: $(UserName)
UserPassword: $(UserPassword)
InlineScript: |
# Define the path of the SQL file from the repository
$sqlFile = "$(Build.SourcesDirectory)\Batch\up5.sql" # Adjust based on your repo structure
# Path on the target VM where the SQL file will be copied
$targetFilePath = "C:\temp\up5.sql"
# Copy the SQL file from pipeline workspace to the VM
Copy-Item -Path $sqlFile -Destination $targetFilePath -Force
# Check if the file is successfully copied
if (Test-Path $targetFilePath) {
Write-Host "SQL file copied successfully to: $targetFilePath"
# Run the SQL file on the target VM using MySQL
$dbPlain = $(UserPassword)
$mysqlCommand = "C:\mybin\bin\mysql.exe -u myuser -p'$dbPlain' --database=mydb < $targetFilePath"
Write-Host "Executing SQL file..."
# Execute the SQL file
Invoke-Expression $mysqlCommand
} else {
Write-Host "Failed to copy the SQL file."
exit 1 # Exit with error code if the file isn't copied
}
在上述管道 YAML 中,正在使用的存储库是 Azure Repos Git,如
checkout: self
任务所示
$targetFilePath = "C:\temp\up5.sql"
这是目标虚拟机上将复制 SQL 文件的路径。
管道中的powershell脚本会将SQL文件从管道的工作区复制到目标VM并执行。
Invoke-Expression
cmdlet 运行 MySQL 命令。
使用
mysql
命令执行的管道(up5.sql):