Skip to content

Discussion - hooks, and a higher-level implementation #314

@jantman

Description

@jantman

Some of my team's terraform makes extensive use of hook scripts/commands, specifically:

  • Before running any terraform commands at all, such as to download dependencies into the workspace. Granted, while it's not so intuitive, BuildStage could probably be used for this.
  • After successfully running terraform init
  • After successfully running terraform apply
  • After successfully running terraform plan
  • After all terraform commands regardless of success or failure (cleanup)

Right now we've got #255 "Refactor hooks in TerraformEnvironmentStage", #250 "Provide a hook for arbitrary scripts after 'apply'", and #292 "Provide a hook for arbitrary scripts after 'terraform validate'".

It seems to me that there's need for a more generic hook script mechanism here. #250 gives an example implementation that includes usage like PostApplyPlugin.run('./bin/run_migrations.sh'), but it seems to me that this will end up violating DRY.

Without much knowledge of the internals or how difficult this would be to implement, I was thinking about something much more like:

@Library(['[email protected]']) _

Jenkinsfile.init(this)
TerraformEnvironmentStage.run_before(TerraformEnvironmentStage.ALL, './bin/download_dependencies.sh')
TerraformEnvironmentStage.run_after(TerraformEnvironmentStage.INIT, './bin/after_init.sh')
TerraformEnvironmentStage.run_before(TerraformEnvironmentStage.PLAN, './bin/before_plan.sh')
TerraformEnvironmentStage.run_before(TerraformEnvironmentStage.APPLY, './bin/before_apply.sh')
TerraformEnvironmentStage.run_after(TerraformEnvironmentStage.ALL, './bin/cleanup.sh', TerraformEnvironmentStage.RUN_ALWAYS)

def validate = new TerraformValidateStage()
def deploy = new TerraformEnvironmentStage('dev')

validate.then(deploy).build()

In essence, my thought was about - at least for TerraformEnvironmentStage - a more generic way to set hooks, based on methods for setting before/after hooks given a constant for the command to run them before/after. The one piece here that I'm a bit more confused by, in terms of implementation, is how to get a run_after hook to execute even on failure; I guess we'd just need to wrap the commands in a try/finally, that may or may not have a hook in the finally...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions