Azure DevOps构建管道-无法通过脚本访问$(Build.BuildNumber)

时间:2018-10-19 20:35:11

标签: powershell azure-devops azure-pipelines

在我的构建管道中,我正在执行以下操作:

  1. 从Git恢复
  2. Powershell脚本-检索构建 数字,然后将其写入json文件。...
  3. 构建解决方案
  4. 存档文件
  5. 发布工件。

在步骤2中,Powershell脚本非常简单:

定义的环境变量:

Name: buildNumber  Value: $(Build.BuildNumber)
Name: rootPath Value:$(Build.ArtifactStagingDirectory)

代码:

$theFile = Get-ChildItem -Path $rootPath -Recurse -Filter "host.json" | Select-Object -First 1
$propertyName = "BuildNumber"

if($theFile)
{
    $json = Get-Content "$theFile" | Out-String | ConvertFrom-Json
    if($json.$propertyName)
    { 
        $json.$propertyName = $buildNumber
    }else{    
        Add-Member -InputObject $json -MemberType NoteProperty -Name $propertyName -Value $buildNumber
    }
    $json | ConvertTo-Json -depth 100 | Out-File "$theFile"

}
else
{
    Write-Warning "Found no files."
}

由于某种原因,我的$ buildNumber返回空值。 $ rootPath正在工作。 我无法在构建步骤之外访问$(Build.BuildNumber)吗?内部版本号格式在“管道的选项”中定义,在标记内部版本时可以很好地工作,但是我无法在Powershell脚本中访问它。

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

使用 import java.io.*; public class FileArray{ public static void writeArray(){ String fileName = "file.bin"; int[] array = {10, 20, 30}; try{ FileOutputStream fileOs = new FileOutputStream(fileName); DataOutputStream os = new DataOutputStream(fileOs); for (int i = 0; i < 3; i++) os.writeInt(array[i]); os.close(); fileOs.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e){ // TODO Auto-generated catch block e.printStackTrace(); } } public void readArray(){ String fileName = "file.bin"; int[] array1; try{ FileInputStream fileIs = new FileInputStream(fileName); DataInputStream is = new DataInputStream(fileIs); System.out.println(is.readInt()); is.close(); fileIs.close(); } catch (FileNotFoundException e){ // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e){ // TODO Auto-generated catch block e.printStackTrace(); } } } 代替 public class FileArrayDemo { public static void main(String[] args){ FileArray write = new FileArray(); write.writeArray(); System.out.println("Done writing. Now reading."); FileArray read = new FileArray(); read.readArray(); } } 表示法。

请参见different notations for different script types in the docs