无法处理 github actions powershell 任务中的特殊字符符号

问题描述 投票:0回答:1

我使用以下查询将

↓ DEV_BL_MYHOST
或此
↑ DEV_BL_MYHOST
保存在数据库中。

$updateQuery = "UPDATE $Database.[dbo].[tbl_myinventory] SET custompackage = NCHAR(8593) + ' '

我使用

Select
语句从数据库中读回这些数据,然后对数据执行多项任务,例如将其存储为 JSON 并将其用作
"↓ DEV_BL_MYHOST"
↑ DEV_BL_MYHOST
作为 Github 操作矩阵。

在 github actions 工作流程 powershell 步骤中读回后,它会打印特殊字符,如下所示:

  Write-Host 'Tracker12 - "${{ matrix.DISPLAYLIST }}"'
  $displayList = '${{ matrix.DISPLAYLIST }}'
  $splitList = $displayList.Split(' ')
  $trackerValue = $splitList[1]
  Write-Host "Tracker15 - $trackerValue"
  echo "approveenvName=$trackerValue" >> $env:GITHUB_OUTPUT
  $splitstring = '${{ matrix.DISPLAYLIST }}'
  $envName = (($splitstring -split '_', 2)[0] -split ' ', 2)[1]

Run Write-Host 'Tracker12 - "↓ DEV_BL_MYHOST"'
Tracker12 - "↓ DEV_BL_MYHOST"

Run $splitstring = '↑ DEV_BL_MYHOST'
At D:\Git-Runners\selfhostdrunner\_work\_temp\c7b0bbb2-5de5-4278-ba2d-75f7c02f1351.ps1:3 char:21
+ $splitstring = '↑ DEV_BL_MYHOST'

如您所见,我无法在从数据库读取此数据的变量周围使用单引号或双引号,即

${{ matrix.DISPLAYLIST }}
,因为其中之一注定会出错。

我的要求是删除空白之前的任何内容,然后在 powershell 中从

DEV_BL_MYHOST
收集
↑ DEV_BL_MYHOST

我尝试使用替换功能来解决此问题,如下所示:

      $splitstring = '${{ matrix.DISPLAYLIST }}'

      # Remove specific special characters using regular expressions
      $modifiedString = $splitstring -replace '[↑↓]', 'X'

但它在第一行本身就失败了

$splitstring = '${{ matrix.DISPLAYLIST }}'

注意:我不想阅读或恢复

UP or DOWN arrow symbols
,因此任何让我只获得文本部分
DEV_BL_MYHOST
的解决方法都会有所帮助。

powershell encoding github-actions runtime-error symbols
1个回答
0
投票

编码问题

根据评论,您的核心问题是编码问题。您从一个值为

↓ DEV_BL_MYHOST
的内存中字符串开始,并且在该行的某个地方错误地处理了该字符串的编码字节表示,这意味着您最终得到了内存中字符串
↓ DEV_BL_MYHOST

您可以使用以下代码查看其实际效果:

$original = "↓ DEV_BL_MYHOST"

# encode the string into a byte representation using the UTF* encoding
$encodedBytes = [System.Text.Encoding]::UTF8.GetBytes($original)

# encoded bytes are stored somewhere and then retrieved later...
$retrievedBytes = $encodedBytes

# incorrectly decode the encoded bytes using the wrong encoding -
# this is the root of the problem. it *should* use the same encoding
# (i.e. UTF8) as was used to encode the bnytes in the first place
$decoded = [System.Text.Encoding]::GetEncoding("windows-1252").GetString($retrievedBytes)

$decoded
# ↓ DEV_BL_MYHOST

即使您只对字符串的

DEV_BL_MYHOST
部分感兴趣,也值得尝试解决该问题,因为将来某个时候您可能会在该部分中得到损坏的文本。

替代问题

不幸的是,您的两个损坏的字符串都包含替代的 PowerShell 字符串分隔符 - 即

,因此当它们被替换到 GitHub Actions 的源代码中时,无论您是否使用它们,其中一个或另一个都会生成无效的 PowerShell 代码在代码中使用双引号或单引号:

# ↓ DEV_BL_MYHOST
Write-Host "Tracker12 - '↓ DEV_BL_MYHOST'"
#                          ^ string value contains an alternative double-quote

# ↑ DEV_BL_MYHOST
Write-Host 'Tracker12 - "↑ DEV_BL_MYHOST"'
#                          ^ string value contains an alternative single-quote

传递参数的更安全方法是将它们设置为 GitHub Actions 步骤中的环境变量,然后从 PowerShell 脚本中的环境中读取它们:

  - name: do stuff
    env:
      DISPLAY_LIST: ${{ matrix.DISPLAYLIST }}
    run: |
      $trackerValue = ($env:DISPLAY_LIST).Split(" ")[1]
      Write-Host "Tracker12 - '$trackerValue'"

如果您不想这样做,另一种选择可能是让操作使用

toJson
表达式对字符串进行 json 编码,然后将其替换到代码中,然后自己反转编码:

  - name: do stuff
    run: |
      $displayList = "${{ toJson( matrix.DISPLAYLIST ) }}" | convertfrom-json
      $trackerValue = $displayList.Split(" ")[1]
      Write-Host "Tracker12 - '$trackerValue'"

应该转义特殊字符并允许它们直接替换到PowerShell代码中而不破坏语法。

© www.soinside.com 2019 - 2024. All rights reserved.