Skip to content

feat: set TF_APPEND_USER_AGENT environment variable #5588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

lyoung-confluent
Copy link
Contributor

@lyoung-confluent lyoung-confluent commented May 18, 2025

what

Many Terraform providers will respect the TF_APPEND_USER_AGENT environment variable and append it to the existing provider/terraform user-agent when making HTTP requests.

This change sets the value to indicate that Terraform is running within Atlantis and includes the PR URL, head commit SHA and username who triggered the command (NOTE: this is not necessarily the PR author). It is set before custom environment variables so users who have set this manually (ex: #2651) will still have their own value set.

why

It's useful to have this information in logs (ex: CloudTrail) to identify exactly what PR modified a resource.

tests

I added a test for this new variable.

references

@dosubot dosubot bot added feature New functionality/enhancement go Pull requests that update Go code labels May 18, 2025
Signed-off-by: Luke Young <lyoung@confluent.io>
Signed-off-by: Luke Young <lyoung@confluent.io>
@github-actions github-actions bot added the docs Documentation label May 20, 2025
lyoung-confluent and others added 2 commits May 19, 2025 17:33
Signed-off-by: Luke Young <lyoung@confluent.io>
@lyoung-confluent
Copy link
Contributor Author

@nitrocode Any other feedback/concerns on this PR? I'd like to get it merged/address any feedback before it gets eaten by the stale PR robot again like #2688 was 😄

jamengual
jamengual previously approved these changes May 28, 2025
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label May 28, 2025
Signed-off-by: Luke Young <lyoung@confluent.io>
nitrocode
nitrocode previously approved these changes May 30, 2025
Co-authored-by: RB <7775707+nitrocode@users.noreply.github.com>
Signed-off-by: Luke Young <91491244+lyoung-confluent@users.noreply.github.com>
@nitrocode
Copy link
Member

Thanks @lyoung-confluent for the changes. I added a couple comments

@lyoung-confluent
Copy link
Contributor Author

@nitrocode I finally got a chance to test this, it does NOT work. The environment variable gets set for custom workflows without issue, but a default plan/apply steps do not work, it seems only these environment variables are set for the executed terraform process:

// Will de-emphasize specific commands to run in output.
"TF_IN_AUTOMATION=true",
// Cache plugins so terraform init runs faster.
fmt.Sprintf("WORKSPACE=%s", workspace),
fmt.Sprintf("ATLANTIS_TERRAFORM_VERSION=%s", v.String()),
fmt.Sprintf("DIR=%s", path),

I'm curious what you would suggest as a fix, it seems that I would have to inject the values somewhere around here for it to be present consistently?

case "env":
out, err = p.EnvStepRunner.Run(ctx, step.RunShell, step.RunCommand, step.EnvVarValue, absPath, envs)
envs[step.EnvVarName] = out
// We reset out to the empty string because we don't want it to
// be printed to the PR, it's solely to set the environment variable.
out = ""
case "multienv":
out, err = p.MultiEnvStepRunner.Run(ctx, step.RunShell, step.RunCommand, absPath, envs, step.Output)

Signed-off-by: Luke Young <lyoung@confluent.io>
Signed-off-by: Luke Young <lyoung@confluent.io>
@lyoung-confluent
Copy link
Contributor Author

Okay, so this diff works (verified using local instance), only problem now is it's not being actually exercised in a test...

Signed-off-by: Luke Young <lyoung@confluent.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation feature New functionality/enhancement go Pull requests that update Go code lgtm This PR has been approved by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Docs on appending the user agent with relevant atlantis information
3 participants