Codebuild path env var not updating when using windows image (windows-base:2019-2.0 using powershell)

0

PATH environment variable not updating in AWS CodeBuild when using windows-base:2019-2.0 with powershell.

When installing cargo with rustup (or any software as far as I can tell) it fails to update the PATH environment variable on the windows image with powershell. Things I've tried:

  • Waiting til next build step
  • Loading local path from System Environment ($env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") )
  • Setting path manually after rustup install ($env:PATH += ";${env:USERPROFILE}\cargo\.bin")

env:path before, after and during build will be identical to one another in this example and the expected commands (cargo in this case) will not work in the build step.

Found no workaround for this (other than maybe hardcoding the entire path along with the expected values in variables section but this is obviously not a good idea)

version: 0.2

env:
  shell: powershell.exe 
phases:
  install:
    commands:
      - Write-Host "env:Path before..."
      - Write-Host "$env:Path"
      - Write-Host "Installing required tools..."
      - Invoke-WebRequest -Uri https://win.rustup.rs/ -OutFile rustup-init.exe
      - ./rustup-init.exe -y
      - Write-Host "env:Path after"
      - Write-Host "$env:Path"
  pre_build:
    commands:
      - Write-Host "Running pre-build commands..."
  build:
    commands:
      - Write-Host "env:Path during build"
      - Write-Host "$env:Path"
      - cargo -V
kb
已提問 8 個月前檢視次數 122 次
1 個回答
0

I found that this is caused by a behavior of CodeBuild when running on Windows. The behavior is that Codebuild invokes a brand new process for every line that starts with '-' in the buildspec.

This appears to be an undocumented defect in codebuild that was confirmed by an AWS support engineer.

Your updated buildspec would be:

version: 0.2

env:
  shell: powershell.exe 
phases:
  build:
    commands:
      - |
        Write-Host "env:Path before..."
        Write-Host "$env:Path"
        Write-Host "Installing required tools..."
        Invoke-WebRequest -Uri https://win.rustup.rs/ -OutFile rustup-init.exe
        ./rustup-init.exe -y
        Write-Host "env:Path after"
        Write-Host "$env:Path"
        cargo -V
已回答 1 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南