diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 64f0d5330f09d2c4981bd15eaf615560be8a6245..68134fbeeae7f42bfb24575330b4de2af79c7322 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,7 +5,7 @@ workflow: - if: $CI_COMMIT_TAG - if: $CI_COMMIT_REF_PROTECTED == "true" -include: +include: - local: tests/unit.gitlab-ci.yml rules: - changes: @@ -29,7 +29,7 @@ include: - if: $CI_COMMIT_TAG # FIXME: we cannot make this work for all use cases because of the following: # - cannot pass parallel.matrix to the component, thus we need to extend it - # - that leads to a problem that when this component is ruled out, the extended job fails, + # - that leads to a problem that when this component is ruled out, the extended job fails, # because it doesn't have a script or trigger. # Additionally, when this project is mirrored into another instance the component reference fails. # This may be solved with https://gitlab.com/gitlab-org/gitlab/-/issues/434260#note_1776822074 @@ -186,7 +186,7 @@ gitlab-opentofu-image:deploy:with-opentofu-version: entrypoint: [""] variables: RELEASE_IMAGE_NAME: "$CI_REGISTRY_IMAGE/gitlab-opentofu" - # OCI image tags are not compatible with semver, specifically the build metadata part + # OCI image tags are not compatible with semver, specifically the build metadata part # indicated with a `+` sign, see https://github.com/distribution/distribution/issues/1201 # We use a dash `-` here, instead of the `+`. # This may be problematic, because it indicates a semver prerelease. @@ -259,13 +259,13 @@ create-release: - if: $CI_COMMIT_TAG before_script: - apk add --update yq envsubst - script: + script: - echo "Creating release $CI_COMMIT_TAG" - AVAILABLE_OPENTOFU_VERSIONS=$(yq -r '.spec.inputs.opentofu_version.options | filter((. | test("\$.*")) == false) | .[] | "- [`" + . + "`](https://github.com/opentofu/opentofu/releases/tag/v" + . + ")"' templates/full-pipeline.yml) - export AVAILABLE_OPENTOFU_VERSIONS - 'AVAILABLE_IMAGES=$(cat image*.md | sort -r | tee images.md | sed -E "s/(\(digest: .*\))/\n - \1/")' - export AVAILABLE_IMAGES - - cat .gitlab/release-notes.md.template | envsubst > release-notes.md + - envsubst < .gitlab/release-notes.md.template > release-notes.md artifacts: paths: - images.md diff --git a/.gitlab/README.md.template b/.gitlab/README.md.template index 2a9d0ad46904055469504984e87fbf08b3485c61..284eaf77b0e1cf297e7bd6e175e5321d83b03bcc 100644 --- a/.gitlab/README.md.template +++ b/.gitlab/README.md.template @@ -1,14 +1,14 @@ # OpenTofu CI/CD Component > 🚧 **NOTE** 🚧 -> +> > The `src/gitlab-tofu.sh` script is still merely a copy from [`gitlab-terraform`](https://gitlab.com/gitlab-org/terraform-images). > Therefore, lots of things in this script and in the templates are still Terraform-related and haven't > been changed to their OpenTofu equivalents. -> Have a look at the [Migrating from the Terraform CI/CD templates](#migrating-from-the-terraform-cicd-templates) +> Have a look at the [Migrating from the Terraform CI/CD templates](#migrating-from-the-terraform-cicd-templates) section when migrating from Terraform CI/CD templates. -This project is home to the **OpenTofu CI/CD component** and it's related assets, +This project is home to the **OpenTofu CI/CD component** and it's related assets, like the `gitlab-tofu` wrapper script and OCI images containing that script together with an OpenTofu version. @@ -107,7 +107,7 @@ fmt: ### Opinionated Templates -This component repository also provides some templates that may often be used, +This component repository also provides some templates that may often be used, for example one that only runs validation (`fmt` and `validate`), plan and an apply, but no destructive actions. @@ -207,7 +207,7 @@ plan: ## Releases & Versioning -This project currently releases tagged commits. +This project currently releases tagged commits. An overview of releases can be found on the [Releases page](https://gitlab.com/components/opentofu/-/releases) and a Changelog can be found [here](CHANGELOG.md). @@ -221,7 +221,7 @@ The component release versions follow [Semantic Versioning 2.0.0](https://semver This project releases multiple OCI image variants that can be used with the component. The intention is that the images used in a component have the same version and or not mixed. -Due to the limitations described in https://gitlab.com/gitlab-org/gitlab/-/issues/438275 +Due to the limitations described in https://gitlab.com/gitlab-org/gitlab/-/issues/438275 it's currently required to provide the component version in the `component` include field and as the `version` input. Check out the [Usage](#Usage) section for examples. @@ -233,7 +233,7 @@ Each component release deploys the following images: - `registry.gitlab.com/components/opentofu/gitlab-opentofu:<VERSION>` - Includes the latest stable OpenTofu version at the time of releasing the component -In the above examples `<VERSION>` references the component version and `<OPENTOFU_VERSION>` +In the above examples `<VERSION>` references the component version and `<OPENTOFU_VERSION>` an OpenTofu release, from [here](https://github.com/opentofu/opentofu/releases). *Note: unfortunately, these image versions are not SemVer compatible, @@ -249,7 +249,7 @@ GitLab CI/CD components are not yet distributed and available on self-managed Gi It's also not possible to just include CI/CD components across instance, thus an include like `- component: gitlab.com/components/opentofu/full-pipeline@~latest` won't work from a self-managed instance. -However, you could mirror this project from GitLab.com onto any self-managed instance using +However, you could mirror this project from GitLab.com onto any self-managed instance using a [repository pull mirror](https://docs.gitlab.com/ee/user/project/repository/mirror/pull.html). and then use the component as you would from GitLab.com, but change the domain, like so: diff --git a/.gitlab/release-notes.md.template b/.gitlab/release-notes.md.template index f7ce60fd80a17d80d77abd27fd575bfcf2ca86dc..a24d368e2e86b8c8678758bb56a6ce13e31fe645 100644 --- a/.gitlab/release-notes.md.template +++ b/.gitlab/release-notes.md.template @@ -11,7 +11,7 @@ include: - component: gitlab.com/components/opentofu/full-pipeline@$CI_COMMIT_TAG inputs: # The version must currently be specified explicitly as an input, - # to find the correctly associated images. # This can be removed + # to find the correctly associated images. # This can be removed # once https://gitlab.com/gitlab-org/gitlab/-/issues/438275 is solved. version: $CI_COMMIT_TAG opentofu_version: $LATEST_OPENTOFU_VERSION diff --git a/.gitlab/scripts/check-semantic-version.sh b/.gitlab/scripts/check-semantic-version.sh index 7a7a5b7c2b1a3304eab5a1f8203721c5d4a9aba7..376af8f1364e301e801398ed9e249f855102bab8 100755 --- a/.gitlab/scripts/check-semantic-version.sh +++ b/.gitlab/scripts/check-semantic-version.sh @@ -3,7 +3,7 @@ # Regex from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string SEMANTIC_VERSION_REGEX='^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$' -match=$(cat - | perl -ne "print if /$SEMANTIC_VERSION_REGEX/") +match=$(perl -ne "print if /$SEMANTIC_VERSION_REGEX/") if [ -z "$match" ]; then echo "The given version doesn't match the semantic versioning format." diff --git a/.gitlab/scripts/fetch-states.sh b/.gitlab/scripts/fetch-states.sh index 4a2982891bf2d32d92c7fc13ac4b30af1a115c48..8c0f17fb40b872d0d1ceb4997a41ae1de4105d49 100755 --- a/.gitlab/scripts/fetch-states.sh +++ b/.gitlab/scripts/fetch-states.sh @@ -4,7 +4,7 @@ # Script to fetch all Terraform States of a project, # optionally filter states which are older than a given # date time. -# +# # The output is in CSV and contains a header line # containing the names of the columns. # You can easily store it as CSV using: @@ -12,14 +12,14 @@ # or stream it to another script, e.g. without the header line: # `fetch-states.sh | sed -n '1d;p' | ...` # -# The script is optimized to run in a GitLab pipeline +# The script is optimized to run in a GitLab pipeline # and therefore uses environment variables which are # defined there by default. -# +# # It requires an additional `GITLAB_TOKEN` variable, which # contains a valid GitLab token with permissions to read # Terraform states. -# +# if [ -z "$FETCH_OLDER_THAN" ]; then FETCH_OLDER_THAN=$(date "+%Y-%m-%dT%H:%M:%SZ") diff --git a/.gitlab/scripts/remove-states.sh b/.gitlab/scripts/remove-states.sh index 49ec5dc162581de6a8ace59fc2c250f8ace0ff5b..1ef238e926a9dcc127dd81cf8a0b61b00f86bf94 100755 --- a/.gitlab/scripts/remove-states.sh +++ b/.gitlab/scripts/remove-states.sh @@ -4,15 +4,15 @@ # Script to remove the given Terraform States. # The states are read from stdin in the format: # `<state-name>,<last-updated-at>` -# -# The script is optimized to run in a GitLab pipeline +# +# The script is optimized to run in a GitLab pipeline # and therefore uses environment variables which are # defined there by default. -# +# # It requires an additional `GITLAB_TOKEN` variable, which # contains a valid GitLab token with permissions to delete # Terraform states. -# +# GITLAB_BASE_TF_ADDRESS="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fd3b1ced7f470e41faf062c01fb10d509f02ed1f..6b190871f815d5716abb48a05b910989ff9ba066 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -22,12 +22,12 @@ from the templates and the components pipeline defintion - at least in the featu ## Backports -The OpenTofu CI/CD component needs to be backported as OpenTofu CI/CD template, +The OpenTofu CI/CD component needs to be backported as OpenTofu CI/CD template, because components are not yet properly supported in self-managed instances. That is, they are not bundled and it's not possible to use a component across instances. The OpenTofu CI/CD job and pipeline templates can be generated using `make backports`. -The output is generated into the `backports` folder. +The output is generated into the `backports` folder. Please contribute those files only upon full manual inspection to the canonical GitLab repository. ## Releasing diff --git a/Makefile b/Makefile index cd64e273fa4c6733cbdc20caf05979bdfb9c6e69..b13cdcebfad3eaf8cd07467064a2d1598564cc57 100644 --- a/Makefile +++ b/Makefile @@ -22,8 +22,8 @@ BACKPORTS_BASE_FILE := $(BACKPORTS_BASE_DIR)/Base.latest.gitlab-ci.yml .PHONY: backports -# NOTE: this make target requires GNU sed and not the mac OS sed. -# Install it with `brew install gnu-sed` and follow the instructions in `brew info gnu-sed` to +# NOTE: this make target requires GNU sed and not the mac OS sed. +# Install it with `brew install gnu-sed` and follow the instructions in `brew info gnu-sed` to # make it the standard `sed` binary (if you wish) or temporarily alias sed=gsed backports: @echo "Generating $(BACKPORTS_BASE_FILE) ..." @@ -35,7 +35,7 @@ backports: @sed '1,/^---$$/d' templates/apply.yml | sed -e 's/$$\[\[ inputs.as \]\]/.opentofu:apply/' | sed -e 's/$$\[\[ inputs.stage \]\]/deploy/' | sed -e 's/$$\[\[ inputs.auto_apply \]\]/$$_TF_AUTO_APPLY/' >> $(BACKPORTS_BASE_FILE) @sed '1,/^---$$/d' templates/destroy.yml | sed -e 's/$$\[\[ inputs.as \]\]/.opentofu:destroy/' | sed -e 's/$$\[\[ inputs.stage \]\]/cleanup/' | sed -e 's/$$\[\[ inputs.auto_destroy \]\]/$$_TF_AUTO_DESTROY/' | sed -e 's/$$\[\[ inputs.create_destroy_job \]\]/$$TF_CREATE_DESTROY_JOB/' >> $(BACKPORTS_BASE_FILE) @sed '1,/^---$$/d' templates/delete-state.yml | sed -e 's/$$\[\[ inputs.as \]\]/.opentofu:delete-state/' | sed -e 's/$$\[\[ inputs.stage \]\]/cleanup/' | sed -e 's/$$\[\[ inputs.create_delete_state_job \]\]/$$TF_CREATE_DELETE_STATE_JOB/' >> $(BACKPORTS_BASE_FILE) - + @# Common inputs @sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.image_registry_base \]\]/$$GITLAB_OPENTOFU_IMAGE_REGISTRY_BASE/' @sed -i $(BACKPORTS_BASE_FILE) -e 's/$$\[\[ inputs.image_name \]\]/gitlab-opentofu/' diff --git a/README.md b/README.md index 7784f24682a610f34584308e7d3d324b835e2865..b14016a6d3d3d2193705e491feb911b15c47282b 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,14 @@ # OpenTofu CI/CD Component > 🚧 **NOTE** 🚧 -> +> > The `src/gitlab-tofu.sh` script is still merely a copy from [`gitlab-terraform`](https://gitlab.com/gitlab-org/terraform-images). > Therefore, lots of things in this script and in the templates are still Terraform-related and haven't > been changed to their OpenTofu equivalents. -> Have a look at the [Migrating from the Terraform CI/CD templates](#migrating-from-the-terraform-cicd-templates) +> Have a look at the [Migrating from the Terraform CI/CD templates](#migrating-from-the-terraform-cicd-templates) section when migrating from Terraform CI/CD templates. -This project is home to the **OpenTofu CI/CD component** and it's related assets, +This project is home to the **OpenTofu CI/CD component** and it's related assets, like the `gitlab-tofu` wrapper script and OCI images containing that script together with an OpenTofu version. @@ -109,7 +109,7 @@ fmt: ### Opinionated Templates -This component repository also provides some templates that may often be used, +This component repository also provides some templates that may often be used, for example one that only runs validation (`fmt` and `validate`), plan and an apply, but no destructive actions. @@ -230,7 +230,7 @@ plan: ## Releases & Versioning -This project currently releases tagged commits. +This project currently releases tagged commits. An overview of releases can be found on the [Releases page](https://gitlab.com/components/opentofu/-/releases) and a Changelog can be found [here](CHANGELOG.md). @@ -244,7 +244,7 @@ The component release versions follow [Semantic Versioning 2.0.0](https://semver This project releases multiple OCI image variants that can be used with the component. The intention is that the images used in a component have the same version and or not mixed. -Due to the limitations described in https://gitlab.com/gitlab-org/gitlab/-/issues/438275 +Due to the limitations described in https://gitlab.com/gitlab-org/gitlab/-/issues/438275 it's currently required to provide the component version in the `component` include field and as the `version` input. Check out the [Usage](#Usage) section for examples. @@ -256,7 +256,7 @@ Each component release deploys the following images: - `registry.gitlab.com/components/opentofu/gitlab-opentofu:<VERSION>` - Includes the latest stable OpenTofu version at the time of releasing the component -In the above examples `<VERSION>` references the component version and `<OPENTOFU_VERSION>` +In the above examples `<VERSION>` references the component version and `<OPENTOFU_VERSION>` an OpenTofu release, from [here](https://github.com/opentofu/opentofu/releases). *Note: unfortunately, these image versions are not SemVer compatible, @@ -272,7 +272,7 @@ GitLab CI/CD components are not yet distributed and available on self-managed Gi It's also not possible to just include CI/CD components across instance, thus an include like `- component: gitlab.com/components/opentofu/full-pipeline@~latest` won't work from a self-managed instance. -However, you could mirror this project from GitLab.com onto any self-managed instance using +However, you could mirror this project from GitLab.com onto any self-managed instance using a [repository pull mirror](https://docs.gitlab.com/ee/user/project/repository/mirror/pull.html). and then use the component as you would from GitLab.com, but change the domain, like so: diff --git a/templates/full-pipeline.yml b/templates/full-pipeline.yml index cb2afebdcc8dcdfbb066b0189bcaf1ebab7425ac..ef3451de82eb08e159cc047096ee9df117eb0fbc 100644 --- a/templates/full-pipeline.yml +++ b/templates/full-pipeline.yml @@ -3,7 +3,7 @@ spec: # Stages stage_validate: default: 'validate' - description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.' + description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.' stage_test: default: 'test' description: 'Defines the test stage. This stage includes the `test` job.' @@ -99,7 +99,7 @@ include: root_dir: $[[ inputs.root_dir ]] state_name: $[[ inputs.state_name ]] rules: - - exists: + - exists: - $[[ inputs.root_dir ]]/**/*.tftest.hcl - local: '/templates/plan.yml' inputs: diff --git a/templates/job-templates.yml b/templates/job-templates.yml index 37e3db7e9561b1ff040116e15bea73fa03dd7a76..aa435d32188d9c2dd5eb6b3151c4d4649731268a 100644 --- a/templates/job-templates.yml +++ b/templates/job-templates.yml @@ -3,7 +3,7 @@ spec: # Stages stage_validate: default: 'validate' - description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.' + description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.' stage_test: default: 'test' description: 'Defines the test stage. This stage includes the `test` job.' diff --git a/templates/validate-plan-apply.yml b/templates/validate-plan-apply.yml index ffcb172254ddb10375fc433275a4264b63d999e5..aee7d8903ea01ce119265816e3c5f85494828a02 100644 --- a/templates/validate-plan-apply.yml +++ b/templates/validate-plan-apply.yml @@ -3,7 +3,7 @@ spec: # Stages stage_validate: default: 'validate' - description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.' + description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.' stage_build: default: 'build' description: 'Defines the build stage. This stage includes the `plan` job.' diff --git a/templates/validate-plan.yml b/templates/validate-plan.yml index 1cf2a9d8751d9f9ae6e1c7f06c43a666519b0a9c..0caca76e9227a74c552dc514cc0c9ceabcd4b7a6 100644 --- a/templates/validate-plan.yml +++ b/templates/validate-plan.yml @@ -3,7 +3,7 @@ spec: # Stages stage_validate: default: 'validate' - description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.' + description: 'Defines the validate stage. This stage includes the `fmt` and `validate` jobs.' stage_build: default: 'build' description: 'Defines the build stage. This stage includes the `plan` job.' diff --git a/tests/integration-tests/BackportTemplates.gitlab-ci.yml b/tests/integration-tests/BackportTemplates.gitlab-ci.yml index 96ae6de1bad12b4429ef9ade191f83e0960ae53a..a0ec6f9b2dec3a313605478dddadd0bc42d6ae73 100644 --- a/tests/integration-tests/BackportTemplates.gitlab-ci.yml +++ b/tests/integration-tests/BackportTemplates.gitlab-ci.yml @@ -17,7 +17,7 @@ plan: extends: .opentofu:plan rules: [{when: always}] -apply: +apply: extends: .opentofu:apply rules: [{when: always}] diff --git a/tests/integration-tests/Defaults.gitlab-ci.yml b/tests/integration-tests/Defaults.gitlab-ci.yml index 0b85e6eb539de02fc8875b3cda43f257c20db60b..ad65d6aa2619e54e24e9cdaa5deddae3bdd8706e 100644 --- a/tests/integration-tests/Defaults.gitlab-ci.yml +++ b/tests/integration-tests/Defaults.gitlab-ci.yml @@ -23,7 +23,7 @@ test: plan: rules: [{when: always}] -apply: +apply: rules: [{when: always}] destroy: diff --git a/tests/unit.gitlab-ci.yml b/tests/unit.gitlab-ci.yml index 64103f70381c4be0247dcc6b1c1ea7158ece83c2..e307e3d6437660628aff3db83bce1f04f67e993b 100644 --- a/tests/unit.gitlab-ci.yml +++ b/tests/unit.gitlab-ci.yml @@ -132,7 +132,7 @@ gitlab-tofu-init-without-reconfigure: - gitlab-tofu init -no-color >/tmp/output.txt 2>&1 || FAILED=true - cat /tmp/output.txt - test $FAILED = true - - 'cat /tmp/output.txt | grep "Error: Backend configuration changed"' + - 'grep "Error: Backend configuration changed" /tmp/output.txt' gitlab-tofu-init-with-reconfigure: extends: @@ -306,7 +306,7 @@ gitlab-tofu-without-implicit-init: - gitlab-tofu $CMD -no-color >/tmp/output.txt 2>&1 || FAILED=true - cat /tmp/output.txt - test $FAILED = true - - 'cat /tmp/output.txt | grep "$ERROR"' + - 'grep "$ERROR" /tmp/output.txt' parallel: matrix: - CMD: apply @@ -333,4 +333,4 @@ gitlab-tofu-no-wrapper: - gitlab-tofu -- apply -no-color >/tmp/output.txt 2>&1 || FAILED=true - cat /tmp/output.txt - test $FAILED = true - - 'cat /tmp/output.txt | grep "Error: Backend initialization required, please run \"tofu init\""' + - 'grep "Error: Backend initialization required, please run \"tofu init\"" /tmp/output.txt'