从YAML管道中删除web.config中的特定行

huangapple go评论61阅读模式
英文:

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文件:-

从YAML管道中删除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'

输出:-

从YAML管道中删除web.config中的特定行

从YAML管道中删除web.config中的特定行

从YAML管道中删除web.config中的特定行

打开了Web.Config文件,PowerShell任务已删除了注释行,如下所示:-

从YAML管道中删除web.config中的特定行

从YAML管道中删除web.config中的特定行

英文:

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 :-

从YAML管道中删除web.config中的特定行

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: &#39;Run a one-line script&#39;

  

- script: |

echo Add other tasks to build, test, and deploy your project.

echo See https://aka.ms/yaml

displayName: &#39;Run a multi-line script&#39;

  

- task: PowerShell@2

inputs:

targetType: &#39;inline&#39;

script: |

Get-Location

Get-ChildItem .

  

- task: PowerShell@2

inputs:

targetType: &#39;inline&#39;

script: |

$configPath =&#39;$(Build.SourcesDirectory)\Web.Config&#39;

$content = Get-Content $configPath

$content = $content | Where-Object { $_ -notmatch &#39;&lt;!--&#39; }

$content | Set-Content $configPath

  

- task: CopyFiles@2

inputs:

SourceFolder: &#39;$(Build.SourcesDirectory)&#39;

Contents: &#39;Web.Config&#39;

TargetFolder: &#39;$(Build.ArtifactStagingDirectory)&#39;

  

- task: PublishBuildArtifacts@1

inputs:

PathtoPublish: &#39;$(Build.ArtifactStagingDirectory)&#39;

ArtifactName: &#39;drop&#39;

publishLocation: &#39;Container&#39;

Output:-

从YAML管道中删除web.config中的特定行

从YAML管道中删除web.config中的特定行

从YAML管道中删除web.config中的特定行

Opened the Web.Config file and the commented lines were removed by the PowerShell task like below:-

从YAML管道中删除web.config中的特定行

从YAML管道中删除web.config中的特定行

huangapple
  • 本文由 发表于 2023年2月7日 04:54:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定