Skip to content
full-pipeline.yml 5.81 KiB
Newer Older
Timo Furrer's avatar
Timo Furrer committed
    # Stages
    stage_validate:
      default: 'validate'
Timo Furrer's avatar
Timo Furrer committed
      description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.'  
    stage_build:
      default: 'build'
Timo Furrer's avatar
Timo Furrer committed
      description: 'Defines the build stage. This stage includes the `plan` job.'
    stage_deploy:
      default: 'deploy'
Timo Furrer's avatar
Timo Furrer committed
      description: 'Defines the deploy stage. This stage includes the `apply` job.'
    stage_cleanup:
      default: 'cleanup'
Timo Furrer's avatar
Timo Furrer committed
      description: 'Defines the cleanup stage. This stage includes the `destroy` and `delete-state` jobs.'
Timo Furrer's avatar
Timo Furrer committed

    # Versions
Timo Furrer's avatar
Timo Furrer committed
    # This version is only required, because we cannot access the context of the component,
    # see https://gitlab.com/gitlab-org/gitlab/-/issues/438275
    version:
      default: 'latest'
Timo Furrer's avatar
Timo Furrer committed
      description: 'Version of this component. Has to be the same as the one in the component include entry.'
Timo Furrer's avatar
Timo Furrer committed
      
Timo Furrer's avatar
Timo Furrer committed
    opentofu_version:
      default: '1.6.0'
Timo Furrer's avatar
Timo Furrer committed
      # options:
      #   - '1.6.0'
      #   - '1.6.0-rc1'
Timo Furrer's avatar
Timo Furrer committed
      description: 'OpenTofu version that should be used.'
Timo Furrer's avatar
Timo Furrer committed

    # Images
Timo Furrer's avatar
Timo Furrer committed
    _image_registry_base:
      default: '$CI_REGISTRY/components/opentofu'
    # FIXME: not yet possible because of https://gitlab.com/gitlab-org/gitlab/-/issues/438722
    # gitlab_opentofu_image:
    #   # FIXME: This should reference the component tag that is used.
    #   #        Currently, blocked by https://gitlab.com/gitlab-org/gitlab/-/issues/438275
    #   # default: '$CI_REGISTRY/components/opentofu/gitlab-opentofu:$[[ inputs.opentofu_version ]]'
    #   default: '$CI_REGISTRY/components/opentofu/gitlab-opentofu:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]'
    #   description: 'Tag of the gitlab-opentofu image.'
Timo Furrer's avatar
Timo Furrer committed
    
    # Configuration
    root_dir: 
      default: ${CI_PROJECT_DIR}
Timo Furrer's avatar
Timo Furrer committed
      description: 'Root directory for the OpenTofu project.'
Timo Furrer's avatar
Timo Furrer committed
    state_name:
      default: default
Timo Furrer's avatar
Timo Furrer committed
      description: 'Remote OpenTofu state name.'
    auto_apply:
      default: 'false'
Timo Furrer's avatar
Timo Furrer committed
      description: 'Whether the apply job is manual or automatically run.'
    auto_destroy:
      default: 'false'
Timo Furrer's avatar
Timo Furrer committed
      description: 'Whether the destroy job is manual or automatically run.'
Timo Furrer's avatar
Timo Furrer committed
    create_destroy_job:
      default: 'false'
      description: 'Wheather the destroy job should be created or not.'
    create_delete_state_job:
      default: 'false'
      description: 'Wheather the delete-state job should be created or not.'
Timo Furrer's avatar
Timo Furrer committed

Timo Furrer's avatar
Timo Furrer committed
    name: '$[[ inputs._image_registry_base ]]/gitlab-opentofu:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]'
Timo Furrer's avatar
Timo Furrer committed
    key: "$[[ inputs.root_dir ]]"
Timo Furrer's avatar
Timo Furrer committed
      - $[[ inputs.root_dir ]]/.terraform/

fmt:
  extends: .default
  stage: $[[ inputs.stage_validate ]]
  needs: []
  script:
Timo Furrer's avatar
Timo Furrer committed
    - gitlab-tofu fmt
  allow_failure: true
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_OPEN_MERGE_REQUESTS  # Don't add it to a *branch* pipeline if it's already in a merge request pipeline.
      when: never
    - if: $CI_COMMIT_BRANCH        # If there's no open merge request, add it to a *branch* pipeline instead.

validate:
  extends: .default
  stage: $[[ inputs.stage_validate ]]
  script:
Timo Furrer's avatar
Timo Furrer committed
    - gitlab-tofu validate
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_OPEN_MERGE_REQUESTS  # Don't add it to a *branch* pipeline if it's already in a merge request pipeline.
      when: never
    - if: $CI_COMMIT_BRANCH        # If there's no open merge request, add it to a *branch* pipeline instead.

plan:
  extends: .default
  stage: $[[ inputs.stage_build ]]
  script:
Timo Furrer's avatar
Timo Furrer committed
    - gitlab-tofu plan
    - gitlab-tofu plan-json
Timo Furrer's avatar
Timo Furrer committed
    name: $[[ inputs.state_name ]]
Timo Furrer's avatar
Timo Furrer committed
  resource_group: $[[ inputs.state_name ]]
  artifacts:
    # Terraform's cache files can include secrets which can be accidentally exposed.
    # Please exercise caution when utilizing secrets in your Terraform infrastructure and
    # consider limiting access to artifacts or take other security measures to protect sensitive information.
    #
    # The next line, which disables public access to pipeline artifacts, is not available on GitLab.com.
    # See: https://docs.gitlab.com/ee/ci/yaml/#artifactspublic
    public: false
    paths:
Timo Furrer's avatar
Timo Furrer committed
      - $[[ inputs.root_dir ]]/plan.cache
Timo Furrer's avatar
Timo Furrer committed
      terraform: $[[ inputs.root_dir ]]/plan.json
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_OPEN_MERGE_REQUESTS  # Don't add it to a *branch* pipeline if it's already in a merge request pipeline.
      when: never
    - if: $CI_COMMIT_BRANCH        # If there's no open merge request, add it to a *branch* pipeline instead.

apply:
  extends: .default
  stage: $[[ inputs.stage_deploy ]]
  script:
Timo Furrer's avatar
Timo Furrer committed
    - gitlab-tofu apply
Timo Furrer's avatar
Timo Furrer committed
    name: $[[ inputs.state_name ]]
Timo Furrer's avatar
Timo Furrer committed
  resource_group: $[[ inputs.state_name ]]
Timo Furrer's avatar
Timo Furrer committed
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && "$[[ inputs.auto_apply ]]" == "true"'
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: manual

destroy:
  extends: .default
Timo Furrer's avatar
Timo Furrer committed
  stage: $[[ inputs.stage_cleanup ]]
Timo Furrer's avatar
Timo Furrer committed
    - gitlab-tofu destroy
Timo Furrer's avatar
Timo Furrer committed
  environment:
    name: $[[ inputs.state_name ]]
    action: stop
Timo Furrer's avatar
Timo Furrer committed
  resource_group: $[[ inputs.state_name ]]
  rules:
Timo Furrer's avatar
Timo Furrer committed
    - if: '"$[[ inputs.create_destroy_job ]]" != "true"'
      when: never
Timo Furrer's avatar
Timo Furrer committed
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && "$[[ inputs.auto_destroy ]]" == "true"'
    - when: manual
Timo Furrer's avatar
Timo Furrer committed

delete-state:
  extends: .default
  stage: $[[ inputs.stage_cleanup ]]
  needs: [destroy]
  resource_group: $[[ inputs.state_name ]]
  rules:
    - when: never
  script:
    - curl --request DELETE -u "gitlab-ci-token:$CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/terraform/state/$[[ inputs.state_name ]]"
Timo Furrer's avatar
Timo Furrer committed
  rules:
    - if: '"$[[ inputs.create_delete_state_job ]]" != "true"'
      when: never
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
    - when: manual