Using Environment Variables in Configuration Pipelines

Hi everyone

I’m currently working with environment variables inside configuration pipelines, so I can use the same code for different environments or different customers even. For example, I have substituted values like API tokens with an environment variable:

basic_auth {
username = “alloy”
password = sys.env(“DEVOPS_API_KEY”)
}

For this to work I needed to add the variable to ~/.bashrc and also to the /lib/systemd/system/alloy.service file.

Now I have a use case, where I want to fetch a targets.yaml file from a remote repository using remote.http but instead of declaring the actual URL, I want to use an environment variable. This is because targets differ across multiple locations but I want to use the same configuration pipeline.

I used the same procedure as in the example with the API token but somehow alloy seems to be getting an empty string from that environment variable.

Pipeline:
remote.http “targets” {

url =  sys.env("TARGETS_URL")
client {
	basic_auth {
		username = "alloy"
		password = sys.env("DEVOPS_API_KEY")
	}
}

}

bashrc:
export TARGET_URL=“https ://dev.azure.com/mycompany/…”

alloy.service:
Environment=TARGET_URL=“https ://dev.azure.com/mycompany/…”

I get the following error:
ts=2025-02-06T10:06:09.33457648Z level=error msg=“failed to fetch remote config” service=remotecfg err=“161:2: Failed to build component: building component: performing request: Get "": unsupported protocol scheme "" (and 1 more diagnostics)”

Any clues on why working with environment variables in the case of API tokens works but in the case of an URL it fails? Maybe wrong syntax in the variable definition?

url = sys.env("TARGETS_URL") has an extra S (TARGETS vs TARGET). Might be the issue. If that’s not it then I am not sure.

Oops, that’s a little typo on my side, sorry. In my environment I have it matching everywhere.
Something I might have to add: The full URL I’m trying to store in this variable contains a lot of special characters like “&” or “%”. Might this have something to do with the issue? Here is an example URL:
“https ://dev.azure.com/company/company/_apis/sourceProviders/TfsGit/filecontents?repository=example&commitOrBranch=main&api-version=7.0&path=example%2Fexample%2Ftargets.yaml”

Quick Update:
I was able to fix the issue by referencing a custom environment file containing all the variables inside the alloy.service file, instead of writing the variables directly into the alloy.service file.

[Service]

Restart=always
User=alloy
Environment=HOSTNAME=%H
Environment=ALLOY_DEPLOY_MODE=deb
EnvironmentFile=/etc/default/custom_alloy
EnvironmentFile=/etc/default/alloy