Skip to content

Instantly share code, notes, and snippets.

@ujwaldhakal
Last active April 27, 2021 16:17
Show Gist options
  • Save ujwaldhakal/bc690b3c4340ebd2e666921d051e22b7 to your computer and use it in GitHub Desktop.
Save ujwaldhakal/bc690b3c4340ebd2e666921d051e22b7 to your computer and use it in GitHub Desktop.
Deployment file for blog
package main
import (
"os"
"os/exec"
"github.com/ujwaldhakal/go-gcp-docker-utils/docker"
"github.com/ujwaldhakal/go-gcp-docker-utils/git"
"github.com/ujwaldhakal/go-gcp-docker-utils/utils"
)
func main() {
env := os.Args[1]
credentialFilePath := getCredentialsFilePath(string(env))
configJson := utils.ConvertTfConfigToJson(getTfVarFileName(string(env)))
commitHash := git.GetCommitHash()
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", credentialFilePath)
docker.Login(credentialFilePath)
docker.Build(configJson.Project, configJson.Github_repo_name, commitHash)
initTerraform()
applyTerraform(git.GetCommitHash(), string(env))
}
func applyTerraform(commitHash string, env string) {
cmd := exec.Command("terraform", "apply", "-var", "image_tag="+commitHash, "-var-file="+getTfVarFileName(env), "-auto-approve", "-lock=false")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
panic("error applying terraform")
}
}
func initTerraform() {
cmd := exec.Command("terraform",
"init",
"-backend-config",
"bucket=tf-test-app")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
panic("error initializing terraform")
}
}
func getTfVarFileName(env string) string {
if env == "dev" {
return "dev.tfvars"
}
if env == "production" {
return "prof.tfvars"
}
panic("Please select correct environment only dev & production available at the moment")
}
func getCredentialsFilePath(env string) string {
if env == "dev" {
return "credentials/dev-cred.json"
}
if env == "production" {
return "credentials/prod-cred.json"
}
panic("error on loading credentials")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment