Skip to content
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

Require a git pull before a terraform apply #31

Open
amyipdev opened this issue Mar 21, 2024 · 3 comments
Open

Require a git pull before a terraform apply #31

amyipdev opened this issue Mar 21, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@amyipdev
Copy link
Contributor

Is there a way to add a hook into Terraform that requires the current branch be up-to-date before running terraform apply? If so, should we add that? It doesn't stop emergency applys - just git commit -m "temp" and undo it after, which doesn't make the branch out-of-date locally - but it does prevent situations liking accidentally taking down March Madness for a few minutes...

@amyipdev amyipdev added the enhancement New feature or request label Mar 21, 2024
@diamondburned
Copy link
Member

Related issue: hashicorp/terraform#32930

Since we have the Nix shell at our disposal, it would be really easy for us to completely hide terraform from the user and enforce deploying via our shell scripts instead.

@amyipdev
Copy link
Contributor Author

Related issue

That would definitely be a great help...

Since we have the Nix shell at our disposal, it would be really easy for us to completely hide terraform from the user and enforce deploying via our shell scripts instead.

Which scripts?

@diamondburned
Copy link
Member

Which scripts?

We'll just make them:

writeShellScriptBin "deploy" ''
  # source the scripts/lib thing
  if [[ $(git branch --show-current) != main ]]; then
    fatal "You're not in the main branch. Deployments are allowed from that branch only."
  fi

  originHEAD=$(git rev-parse origin/main)
  behind=$(git rev-list --count HEAD..${originHEAD})
  if (( behind > 0 )); then
    log "Your local repository is behind upstream by $behind commits."
    log 'Ensure you ran `git pull` before proceeding.'
    exit 1
  fi

  # Run a Nix-imported Terraform here.
  # This way, we don't need Terraform in our $PATH.
  ${lib.getBin pkgs.terraform} apply "$@"
''

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants