英文:
Remove specific lines from web.config using YAML pipelines
问题
期望:删除带有注释的行
<service behaviorConfiguration="mybehavior" name="">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract=""/>
</service>
<service behaviorConfiguration="mybehavior" name="">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract=""/>
</service>
关于此,我在当前的YAML管道中有以下任务(CopyFile@2):
- task: CopyFiles@2
displayName: 将web.config复制到包中
condition: eq(variables.generateConfigs, true)
inputs:
SourceFolder: '$(ReleasesPath.SourceFolderName)'
Contents: |
Web.config
TargetFolder: '$(CopyPath.TragetFolderName)'
CleanTargetFolder: false
preserveTimestamp: true
英文:
I want to delete some specific lines from my web.config while copying the file to the artifact using azure yaml pipelines.
Actual:
`<service behaviorConfiguration="mybehavior" name="">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract=""/>
<!--<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>-->
</service>
<service behaviorConfiguration="mybehavior" name="">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract=""/>
<!--<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>-->
</service>`
Expected: delete lines with comments
`<service behaviorConfiguration="mybehavior" name="">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract=""/>
</service>
<service behaviorConfiguration="mybehavior" name="">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract=""/>
</service>`
For information, I have the following task (CopyFile@2) in my current yml pipelines:
`- task: CopyFiles@2
displayName: Copy web config into the Package
condition: eq(variables.generateConfigs, true)
inputs:
SourceFolder: '$(ReleasesPath.SourceFolderName)'
Contents: |
Web.config
TargetFolder: '$(CopyPath.TragetFolderName)'
CleanTargetFolder: false
preserveTimestamp: true`
答案1
得分: 0
我添加了一个PowerShell任务,用于从Web.Config文件中删除注释行,然后添加了一个复制任务来将Web.Config文件复制到目标位置。
Web.Config文件:-
以下是带有PowerShell和复制任务的我的YAML代码:-
# 启动管道
# 从一个最小的管道开始,您可以根据需要进行自定义以构建和部署您的代码。
# 添加构建、运行测试、部署等步骤:
# https://aka.ms/yaml
触发器:
- 主要
池:
vmImage: windows-latest
步骤:
- 脚本: echo 你好,世界!
displayName: '运行一行脚本'
- 脚本: |
echo 添加其他任务来构建、测试和部署您的项目。
echo 请参阅 https://aka.ms/yaml
displayName: '运行多行脚本'
- 任务: PowerShell@2
inputs:
targetType: 'inline'
script: |
Get-Location
Get-ChildItem .
- 任务: PowerShell@2
inputs:
targetType: 'inline'
script: |
$configPath ='$(Build.SourcesDirectory)\Web.Config'
$content = Get-Content $configPath
$content = $content | Where-Object { $_ -notmatch '<!--' }
$content | Set-Content $configPath
- 任务: CopyFiles@2
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
Contents: 'Web.Config'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- 任务: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
输出:-
打开了Web.Config文件,PowerShell任务已删除了注释行,如下所示:-
英文:
I added a Powershell task to remove the commented lines from the Web.Config file and then added the Copy task to Copy the Web.Config file to the destination.
Web.Config file :-
Below is my yaml code with Powershell and Copy task:-
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- main
pool:
vmImage: windows-latest
steps:
- script: echo Hello, world!
displayName: 'Run a one-line script'
- script: |
echo Add other tasks to build, test, and deploy your project.
echo See https://aka.ms/yaml
displayName: 'Run a multi-line script'
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
Get-Location
Get-ChildItem .
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$configPath ='$(Build.SourcesDirectory)\Web.Config'
$content = Get-Content $configPath
$content = $content | Where-Object { $_ -notmatch '<!--' }
$content | Set-Content $configPath
- task: CopyFiles@2
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
Contents: 'Web.Config'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
Output:-
Opened the Web.Config file and the commented lines were removed by the PowerShell task like below:-
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论