Skip to content
full-pipeline.yml 4.25 KiB
Newer Older
Timo Furrer's avatar
Timo Furrer committed
    # Stages
    stage_validate:
      default: 'validate'
      description: 'Defines the validate stage'  
    stage_test:
      default: 'test'
      description: 'Defines the test stage'
    stage_build:
      default: 'build'
      description: 'Defines the build stage'
    stage_deploy:
      default: 'deploy'
      description: 'Defines the deploy stage'
    stage_cleanup:
      default: 'cleanup'
      description: 'Defines the cleanup stage'
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'
      description: 'Version of this component'
      
Timo Furrer's avatar
Timo Furrer committed
    opentofu_version:
      default: '1.6.0'
      options:
        - '1.6.0'
        - '1.6.0-rc1'
      description: 'Released version of upstream OpenTofu'

    # Images
    gitlab_opentofu_image:
Timo Furrer's avatar
Timo Furrer committed
      # 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 ]]'
Timo Furrer's avatar
Timo Furrer committed
      default: '$CI_REGISTRY/components/opentofu/gitlab-opentofu:$[[ inputs.version ]]-opentofu$[[ inputs.opentofu_version ]]'
Timo Furrer's avatar
Timo Furrer committed
      description: 'Image name of the gitlab-opentofu image'
    
    # Configuration
    root_dir: 
      default: ${CI_PROJECT_DIR}
      description: 'Root directory for the OpenTofu project'
    state_name:
      default: default
      description: 'State name'

Timo Furrer's avatar
Timo Furrer committed
    name: $[[ inputs.gitlab_opentofu_image ]]
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 ]]
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $TF_AUTO_DEPLOY == "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 ]]