VSCode DevContainers GIT-Accesstoken / Azure DevOps PAT from environment variable

Hey, a couple of weeks I struggled to mount my git access token / azure devops PAT into devcontainers, I usually use. Ususally git tokens can’t be set via environment variables directly, but with GIT_ASKPASS (docs) you can do it on your own. The environment variable askpass should point to a local script, which provides credentials for your git server (in this case Azure DevOps). When git would prompt you to enter your credentials, git executes that script instead and uses the output as your credential. So we will setup a little script which just echos our GIT Token from the environment variables and we are done. There is a little trap, VSCodes DevContainer bring their own askpass setting and we need to deactivate this in the container.

DevContainer.json

With the containerEnv.GIT_ASKPASS we are pointing to our own small script which is handling the askpass execution later.

containerEnv.GIT_TOKEN gets the value from my PCs GIT_TOKEN environment variable. This environmentvariable contains my Azure DevOps PAT and is set in my operating system (Windows).

settings.git.useIntegratedAskPass needs to be set to false. This deactivates the build in askpass scripts from VSCodes DevContainer.

File: pat.sh

The pat.sh contains the little script to echo the GIT_TOKEN, when git needs credentials to authenticate. It’s just 2 lines:

#!/bin/bash
echo $GIT_TOKEN

Dockerfile

The Dockerfile just needs a little copy job for your own askpass script (line 2)

Thats all, and this is how your repository should look like.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store