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
preguntada hace 8 meses122 visualizaciones
1 Respuesta
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
respondido hace un mes

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas