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

Allow marking variables as required, and fail task if they're absent #889

Closed
deepbrook opened this issue Oct 10, 2022 · 4 comments
Closed
Labels
area: variables Changes related to variables. state: duplicate Duplicates another issue. type: feature A new feature or functionality.

Comments

@deepbrook
Copy link

I ran into behaviour which causes a lot of annoying trash on my gitlab instance.

Given the following task:

  init:purpose:module:
    dir: deploy/{{.module}}
    vars:
      purpose: "{{.purpose}}"
      module: "{{.module}}"
    cmds:
      - >
        terraform init -reconfigure
        -backend-config="address=https://gitlab.netrtl.com/api/v4/projects/{{.GL_PROJECT_ID}}/terraform/state/{{.purpose}}-{{.module}}-{{.env}}"
        -backend-config="lock_address=https://gitlab.netrtl.com/api/v4/projects/{{.GL_PROJECT_ID}}/terraform/state/{{.purpose}}-{{.module}}-{{.env}}/lock"
        -backend-config="unlock_address=https://gitlab.netrtl.com/api/v4/projects/{{.GL_PROJECT_ID}}/terraform/state/{{.purpose}}-{{.module}}-{{.env}}/lock"
        -backend-config="lock_method=POST"
        -backend-config="unlock_method=DELETE"
        -backend-config="retry_wait_min=5"

I get built-in failure if GL_PROJECT_ID is missing, because the gitlab API is going to throw a 404.
However, If misspell purpose on the cli when running this task like this:

task   init:purpose:module prupose=pass-the-butter module=butter

The task runs and the state file is simply created on GitLab when it doesn't exist:

task: [init:purpose:module] terraform init -reconfigure -backend-config="address=https://gitlab.netrtl.com/api/v4/projects/5729/terraform/state/-butter-prod" -backend-config="lock_address=https://gitlab.netrtl.com/api/v4/projects/5729/terraform/state/-butter-prod/lock" -backend-config="unlock_address=https://gitlab.netrtl.com/api/v4/projects/5729/terraform/state/-butter-prod/lock" -backend-config="lock_method=POST" -backend-config="unlock_method=DELETE" -backend-config="retry_wait_min=5"
[...]

I'd like to be able to declare certain variables as required/existing via the yaml schema, like this for example:

  init:purpose:module:
    dir: deploy/{{.module}}
    vars:
      purpose:
        value: "{{.purpose}}"
        required: true
      module: "{{.module}}"
@treybrisbane
Copy link

@deepbrook You can effectively get required variables by leveraging a combination of Go's template syntax and Slim-Sprig's fail function:

tasks:
  example-task:
    vars:
      example_variable: '{{or .example_variable (fail "Variable `example_variable` is required")}}'

Hope that helps! 🙂

@sdemura
Copy link

sdemura commented Oct 14, 2022

I use preconditions for this, but I will say using sprig is more clever

 example:
    vars:
      AWS_ACCESS_KEY_ID: "{{.AWS_ACCESS_KEY_ID | default nil}}"
      AWS_SECRET_ACCESS_KEY: "{{.AWS_SECRET_ACCESS_KEY | default nil}}"
    cmds:
      - cmd: aws ...
         silent: true
    preconditions:
      - sh: "[ {{.AWS_ACCESS_KEY_ID}} ]"
        msg: AWS_ACCESS_KEY_ID envvar not set.
      - sh: "[ {{.AWS_SECRET_ACCESS_KEY}} ]"
        msg: AWS_SECRET_ACCESS_KEY envvar not set.

@max-sixty
Copy link

FYI this is similar to #495

@andreynering andreynering added state: duplicate Duplicates another issue. type: feature A new feature or functionality. area: variables Changes related to variables. labels Jun 17, 2023
@andreynering
Copy link
Member

Closing as duplicate of #1203 and #1204.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: variables Changes related to variables. state: duplicate Duplicates another issue. type: feature A new feature or functionality.
Projects
None yet
Development

No branches or pull requests

5 participants