Skip to content

Support extending artifacts section for jobs

I was recently trying to deploy zipped lambdas to AWS using the components. Terraform provides an archive_file block that you can use to zip a file for later usage, for example:

data "archive_file" "lambda_zip_payload" {
  type        = "zip"
  source_file = "../../src/main.py"
  output_path = "../../output/main.zip"
}

The problem is that the zip file is only generated upon planning, meaning you would have to export it as an artifact to the apply job. By default, component jobs currently only export the plan cache artifact, and as far as I know it's not possible to extend their artifact configuration using YAML techniques in the .gitlab-ci.yml. A hacky workaround would be to overwrite the job artifact config, adding the required artifact path and hardcode the default one so it's not overwrriten. Not only hacky, it would also change the access level for the plan aswell (I learned that artifact access level doesn't prevent forwarding to downstream jobs):

# templates/plan.yml

# [...]
# '$[[ inputs.as ]]':
# [...]
  artifacts:
      access: '$[[ inputs.artifacts_access ]]'
      paths:
        - $GITLAB_TOFU_ROOT_DIR/$[[ inputs.plan_name ]].cache
      reports:
        terraform: $GITLAB_TOFU_ROOT_DIR/$[[ inputs.plan_name]].json
# .gitlab-ci.yml

variables:
  GITLAB_TOFU_ROOT_DIR: infra
  GITLAB_OPENTOFU_PLAN_NAME: plan

plan:
  artifacts:
    access: developer
    paths:
      - output/*
      - $GITLAB_TOFU_ROOT_DIR/$GITLAB_OPENTOFU_PLAN_NAME.json
    expire_in: 1 hour

Another workaround for the whole situation would be to delegate the zipping task to a separate job, and have its artifacts served into the apply job. In the mean time, I'll be using a Docker image approach to avoid zipping.

Proposal

It would be nice to be able to extend the artifacts section of a job:

include:
  - component: $CI_SERVER_FQDN/components/opentofu/validate-plan-apply@0.45.0
    inputs:
      plan_artifacts:
        - foo/
        - bar/
Edited by Gabriel Amado