├── .github └── workflows │ ├── pr-title.yml │ ├── pre-commit.yml │ └── release.yml ├── .gitignore ├── .pre-commit-config.yaml ├── .releaserc.json ├── CHANGELOG.md ├── LICENCE ├── README.md ├── examples ├── main.tf ├── outputs.tf ├── variables.tf └── versions.tf └── modules ├── gitlab-group-module ├── README.md ├── main.tf ├── outputs.tf ├── variables.tf └── versions.tf ├── gitlab-project-module ├── README.md ├── main.tf ├── outputs.tf ├── variables.tf └── versions.tf └── gitlab-user-module ├── README.md ├── main.tf ├── outputs.tf ├── variables.tf └── versions.tf /.github/workflows/pr-title.yml: -------------------------------------------------------------------------------- 1 | name: 'Validate PR title' 2 | 3 | on: 4 | pull_request_target: 5 | types: 6 | - opened 7 | - edited 8 | - synchronize 9 | 10 | jobs: 11 | main: 12 | name: Validate PR title 13 | runs-on: ubuntu-latest 14 | steps: 15 | # Please look up the latest version from 16 | # https://github.com/amannn/action-semantic-pull-request/releases 17 | - uses: amannn/action-semantic-pull-request@v3.4.6 18 | env: 19 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 20 | with: 21 | # Configure which types are allowed. 22 | # Default: https://github.com/commitizen/conventional-commit-types 23 | types: | 24 | fix 25 | feat 26 | docs 27 | ci 28 | chore 29 | # Configure that a scope must always be provided. 30 | requireScope: false 31 | # Configure additional validation for the subject based on a regex. 32 | # This example ensures the subject starts with an uppercase character. 33 | subjectPattern: ^[A-Z].+$ 34 | # If `subjectPattern` is configured, you can use this property to override 35 | # the default error message that is shown when the pattern doesn't match. 36 | # The variables `subject` and `title` can be used within the message. 37 | subjectPatternError: | 38 | The subject "{subject}" found in the pull request title "{title}" 39 | didn't match the configured pattern. Please ensure that the subject 40 | starts with an uppercase character. 41 | # For work-in-progress PRs you can typically use draft pull requests 42 | # from Github. However, private repositories on the free plan don't have 43 | # this option and therefore this action allows you to opt-in to using the 44 | # special "[WIP]" prefix to indicate this state. This will avoid the 45 | # validation of the PR title and the pull request checks remain pending. 46 | # Note that a second check will be reported if this is enabled. 47 | wip: true 48 | # When using "Squash and merge" on a PR with only one commit, GitHub 49 | # will suggest using that commit message instead of the PR title for the 50 | # merge commit, and it's easy to commit this by mistake. Enable this option 51 | # to also validate the commit message for one commit PRs. 52 | validateSingleCommit: false 53 | -------------------------------------------------------------------------------- /.github/workflows/pre-commit.yml: -------------------------------------------------------------------------------- 1 | name: Pre-Commit 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | - master 8 | 9 | env: 10 | TERRAFORM_DOCS_VERSION: v0.16.0 11 | 12 | jobs: 13 | collectInputs: 14 | name: Collect workflow inputs 15 | runs-on: ubuntu-latest 16 | outputs: 17 | directories: ${{ steps.dirs.outputs.directories }} 18 | steps: 19 | - name: Checkout 20 | uses: actions/checkout@v2 21 | 22 | - name: Get root directories 23 | id: dirs 24 | uses: clowdhaus/terraform-composite-actions/directories@v1.3.0 25 | 26 | preCommitMinVersions: 27 | name: Min TF pre-commit 28 | needs: collectInputs 29 | runs-on: ubuntu-latest 30 | strategy: 31 | matrix: 32 | directory: ${{ fromJson(needs.collectInputs.outputs.directories) }} 33 | steps: 34 | - name: Checkout 35 | uses: actions/checkout@v2 36 | 37 | - name: Terraform min/max versions 38 | id: minMax 39 | uses: clowdhaus/terraform-min-max@v1.0.3 40 | with: 41 | directory: ${{ matrix.directory }} 42 | 43 | - name: Pre-commit Terraform ${{ steps.minMax.outputs.minVersion }} 44 | # Run only validate pre-commit check on min version supported 45 | if: ${{ matrix.directory != '.' }} 46 | uses: clowdhaus/terraform-composite-actions/pre-commit@v1.3.0 47 | with: 48 | terraform-version: ${{ steps.minMax.outputs.minVersion }} 49 | args: 'terraform_validate --color=always --show-diff-on-failure --files ${{ matrix.directory }}/*' 50 | 51 | - name: Pre-commit Terraform ${{ steps.minMax.outputs.minVersion }} 52 | # Run only validate pre-commit check on min version supported 53 | if: ${{ matrix.directory == '.' }} 54 | uses: clowdhaus/terraform-composite-actions/pre-commit@v1.3.0 55 | with: 56 | terraform-version: ${{ steps.minMax.outputs.minVersion }} 57 | args: 'terraform_validate --color=always --show-diff-on-failure --files $(ls *.tf)' 58 | 59 | preCommitMaxVersion: 60 | name: Max TF pre-commit 61 | runs-on: ubuntu-latest 62 | needs: collectInputs 63 | steps: 64 | - name: Checkout 65 | uses: actions/checkout@v2 66 | with: 67 | ref: ${{ github.event.pull_request.head.ref }} 68 | repository: ${{github.event.pull_request.head.repo.full_name}} 69 | 70 | - name: Terraform min/max versions 71 | id: minMax 72 | uses: clowdhaus/terraform-min-max@v1.0.3 73 | 74 | - name: Pre-commit Terraform ${{ steps.minMax.outputs.maxVersion }} 75 | uses: clowdhaus/terraform-composite-actions/pre-commit@v1.3.0 76 | with: 77 | terraform-version: ${{ steps.minMax.outputs.maxVersion }} 78 | terraform-docs-version: ${{ env.TERRAFORM_DOCS_VERSION }} 79 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | branches: 7 | - main 8 | - master 9 | paths: 10 | - '**/*.tpl' 11 | - '**/*.py' 12 | - '**/*.tf' 13 | - '.github/workflows/release.yml' 14 | 15 | jobs: 16 | release: 17 | name: Release 18 | runs-on: ubuntu-latest 19 | # Skip running release workflow on forks 20 | if: github.repository_owner == 'EvoltDev' 21 | steps: 22 | - name: Checkout 23 | uses: actions/checkout@v2 24 | with: 25 | persist-credentials: false 26 | fetch-depth: 0 27 | 28 | - name: Release 29 | uses: cycjimmy/semantic-release-action@v2 30 | with: 31 | semantic_version: 18.0.0 32 | extra_plugins: | 33 | @semantic-release/changelog@6.0.0 34 | @semantic-release/git@10.0.0 35 | conventional-changelog-conventionalcommits@4.6.3 36 | env: 37 | GITHUB_TOKEN: ${{ secrets.SEMANTIC_RELEASE_TOKEN }} 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Local .terraform directories 2 | **/.terraform/* 3 | 4 | # .tfstate files 5 | *.tfstate 6 | *.tfstate.* 7 | 8 | # Crash log files 9 | crash.log 10 | crash.*.log 11 | 12 | # Exclude all .tfvars files, which are likely to contain sensitive data, such as 13 | # password, private keys, and other secrets. These should not be part of version 14 | # control as they are data points which are potentially sensitive and subject 15 | # to change depending on the environment. 16 | *.tfvars 17 | *.tfvars.json 18 | 19 | # Ignore override files as they are usually used to override resources locally and so 20 | # are not checked in 21 | override.tf 22 | override.tf.json 23 | *_override.tf 24 | *_override.tf.json 25 | 26 | # Include override files you do wish to add to version control using negated pattern 27 | # !example_override.tf 28 | 29 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan 30 | # example: *tfplan* 31 | 32 | # Ignore CLI configuration files 33 | .terraformrc 34 | terraform.rc 35 | .DS_Store 36 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/antonbabenko/pre-commit-terraform 3 | rev: v1.62.3 4 | hooks: 5 | - id: terraform_fmt 6 | - id: terraform_validate 7 | - id: terraform_docs 8 | args: 9 | - '--args=--lockfile=false' 10 | - id: terraform_tflint 11 | args: 12 | - '--args=--only=terraform_deprecated_interpolation' 13 | - '--args=--only=terraform_deprecated_index' 14 | - '--args=--only=terraform_unused_declarations' 15 | - '--args=--only=terraform_comment_syntax' 16 | - '--args=--only=terraform_documented_outputs' 17 | - '--args=--only=terraform_documented_variables' 18 | - '--args=--only=terraform_typed_variables' 19 | - '--args=--only=terraform_module_pinned_source' 20 | - '--args=--only=terraform_naming_convention' 21 | - '--args=--only=terraform_required_version' 22 | - '--args=--only=terraform_required_providers' 23 | - '--args=--only=terraform_standard_module_structure' 24 | - '--args=--only=terraform_workspace_remote' 25 | - repo: https://github.com/pre-commit/pre-commit-hooks 26 | rev: v4.1.0 27 | hooks: 28 | - id: check-merge-conflict 29 | - id: end-of-file-fixer 30 | -------------------------------------------------------------------------------- /.releaserc.json: -------------------------------------------------------------------------------- 1 | { 2 | "branches": [ 3 | "main", 4 | "master" 5 | ], 6 | "ci": false, 7 | "plugins": [ 8 | [ 9 | "@semantic-release/commit-analyzer", 10 | { 11 | "preset": "conventionalcommits" 12 | } 13 | ], 14 | [ 15 | "@semantic-release/release-notes-generator", 16 | { 17 | "preset": "conventionalcommits" 18 | } 19 | ], 20 | [ 21 | "@semantic-release/github", 22 | { 23 | "successComment": "This ${issue.pull_request ? 'PR is included' : 'issue has been resolved'} in version ${nextRelease.version} :tada:", 24 | "labels": false, 25 | "releasedLabels": false 26 | } 27 | ], 28 | [ 29 | "@semantic-release/changelog", 30 | { 31 | "changelogFile": "CHANGELOG.md", 32 | "changelogTitle": "# Changelog\n\nAll notable changes to this project will be documented in this file." 33 | } 34 | ], 35 | [ 36 | "@semantic-release/git", 37 | { 38 | "assets": [ 39 | "CHANGELOG.md" 40 | ], 41 | "message": "chore(release): version ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" 42 | } 43 | ] 44 | ] 45 | } 46 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | Be up to date with module 4 | 5 | ## v1.0.1 - 2023-03-27 6 | 7 | Added additional attributes for user 8 | 9 | ## v1.0.0 - 2022-03-25 10 | 11 | First release of module, you can create groups, projects and users with group/project membership. 12 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gitlab Terraform module 2 | 3 | Terraform module which can help you to create Gitlab resources and assign membership as you want. This module has three submodules for groups, projects and users. Below you can see how to use them. 4 | 5 | ## Gitlab groups 6 | 7 | Due to gitlab issue [Gitlab Issue 244345](https://gitlab.com/gitlab-org/gitlab/-/issues/244345) it is not possible to create root group. As a workaround first you need to create root group thought browser and use import command to add that group in state. 8 | 9 | Import command: 10 | ``` 11 | terraform import 'module.gitlab-group-module-dev.gitlab_group.group["dev"]' 12 | ``` 13 | 14 | ### Module usage: 15 | 16 | ```hcl 17 | module "gitlab-group-module-components" { 18 | source = "../modules/gitlab-group-module" 19 | groups = { 20 | frontend = { 21 | name = "Frontend" 22 | path = "frontend" 23 | parent_id = module.gitlab-group-module-dev.created_groups["dev"].id 24 | } 25 | backend = { 26 | name = "Backend" 27 | path = "backend" 28 | parent_id = module.gitlab-group-module-dev.created_groups["dev"].id 29 | } 30 | } 31 | } 32 | ``` 33 | 34 | ## Gitlab projects 35 | Use this submodule to create projects on you Gitlab instance or Gitlab.com. 36 | 37 | ### Module usage: 38 | ```hcl 39 | module "gitlab-project-module" { 40 | source = "../modules/gitlab-project-module" 41 | projects = { 42 | react_example_project = { 43 | name = "React example project" 44 | namespace_id = local.created_groups["frontend"].id 45 | push_rules = { 46 | commit_committer_check = true 47 | } 48 | } 49 | django_example_project = { 50 | name = "Django example project" 51 | namespace_id = local.created_groups["backend"].id 52 | } 53 | } 54 | } 55 | ``` 56 | 57 | ## Gitlab users 58 | Use this submodule to create users and asign membership to groups or projects. 59 | 60 | ### Module usage: 61 | ```hcl 62 | module "gitlab-user-module" { 63 | source = "../modules/gitlab-user-module" 64 | groups = local.created_groups 65 | projects = module.gitlab-project-module.created_projects 66 | users = { 67 | johnharper = { 68 | create = false 69 | username = "john.harper" 70 | email = "john.harper@example.com" 71 | name = "John Harper" 72 | groups = { 73 | frontend = { 74 | access_level = "maintainer" 75 | expires_at = null 76 | } 77 | dev = { 78 | access_level = "guest" 79 | } 80 | } 81 | } 82 | chrisharper = { 83 | username = "chris.harper" 84 | email = "chris.harper@example.com" 85 | name = "Chris Harper" 86 | groups = { 87 | backend = { 88 | access_level = "developer" 89 | expires_at = "2030-12-31" 90 | } 91 | dev = { 92 | access_level = "guest" 93 | expires_at = "2030-12-31" 94 | } 95 | } 96 | projects = { 97 | react_example_project = { 98 | access_level = "maintainer" 99 | } 100 | } 101 | } 102 | } 103 | } 104 | ``` 105 | 106 | ## License 107 | 108 | Apache 2 Licensed. See [LICENSE](https://github.com/EvoltDev/terraform-gitlab-module/blob/main/LICENCE) for full details. 109 | -------------------------------------------------------------------------------- /examples/main.tf: -------------------------------------------------------------------------------- 1 | locals { 2 | created_groups = merge(module.gitlab_group_module_dev.created_groups, module.gitlab_group_module_components.created_groups) 3 | } 4 | 5 | module "gitlab_group_module_dev" { 6 | source = "../modules/gitlab-group-module" 7 | groups = { 8 | dev = { 9 | auto_devops_enabled = false 10 | default_branch_protection = 2 11 | emails_disabled = false 12 | lfs_enabled = true 13 | mentions_disabled = false 14 | name = "dev1" 15 | parent_id = 0 16 | path = "dev2249" 17 | project_creation_level = "developer" 18 | request_access_enabled = true 19 | require_two_factor_authentication = false 20 | share_with_group_lock = false 21 | subgroup_creation_level = "maintainer" 22 | two_factor_grace_period = 48 23 | visibility_level = "private" 24 | } 25 | } 26 | } 27 | 28 | module "gitlab_group_module_components" { 29 | source = "../modules/gitlab-group-module" 30 | groups = { 31 | frontend = { 32 | name = "Frontend" 33 | path = "frontend" 34 | parent_id = module.gitlab_group_module_dev.created_groups["dev"].id 35 | } 36 | backend = { 37 | name = "Backend" 38 | path = "backend" 39 | parent_id = module.gitlab_group_module_dev.created_groups["dev"].id 40 | } 41 | } 42 | } 43 | 44 | module "gitlab_project_module" { 45 | source = "../modules/gitlab-project-module" 46 | projects = { 47 | react_example_project = { 48 | name = "React example project" 49 | namespace_id = local.created_groups["frontend"].id 50 | push_rules = { 51 | commit_committer_check = true 52 | } 53 | } 54 | django_example_project = { 55 | name = "Django example project" 56 | namespace_id = local.created_groups["backend"].id 57 | } 58 | express_example_project = { 59 | name = "Express example project" 60 | namespace_id = local.created_groups["backend"].id 61 | } 62 | } 63 | } 64 | 65 | module "gitlab_user_module" { 66 | source = "../modules/gitlab-user-module" 67 | groups = local.created_groups 68 | projects = module.gitlab_project_module.created_projects 69 | users = { 70 | johnharper = { 71 | create = false 72 | username = "john.harper" 73 | email = "john.harper@example.com" 74 | name = "John Harper" 75 | groups = { 76 | frontend = { 77 | access_level = "maintainer" 78 | expires_at = null 79 | } 80 | dev = { 81 | access_level = "guest" 82 | } 83 | } 84 | } 85 | chrisharper = { 86 | username = "chris.harper" 87 | email = "chris.harper@example.com" 88 | name = "Chris Harper" 89 | groups = { 90 | backend = { 91 | access_level = "developer" 92 | expires_at = "2030-12-31" 93 | } 94 | dev = { 95 | access_level = "guest" 96 | expires_at = "2030-12-31" 97 | } 98 | } 99 | projects = { 100 | react_example_project = { 101 | access_level = "maintainer" 102 | } 103 | } 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /examples/outputs.tf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvoltDev/terraform-gitlab-gitlab/f11ef97c06c6125f1f6eeb36d471f26104e7cb09/examples/outputs.tf -------------------------------------------------------------------------------- /examples/variables.tf: -------------------------------------------------------------------------------- 1 | variable "gitlab_token" { 2 | description = "Gitlab token" 3 | type = string 4 | } 5 | -------------------------------------------------------------------------------- /examples/versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">=1.3" 3 | 4 | backend "remote" { 5 | hostname = "app.terraform.io" 6 | organization = "evolt" 7 | workspaces { 8 | name = "gitlab-module" 9 | } 10 | } 11 | 12 | required_providers { 13 | gitlab = { 14 | source = "gitlabhq/gitlab" 15 | version = "~> 16.0" 16 | } 17 | } 18 | } 19 | 20 | provider "gitlab" { 21 | token = var.gitlab_token 22 | } 23 | -------------------------------------------------------------------------------- /modules/gitlab-group-module/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Requirements 3 | 4 | | Name | Version | 5 | |------|---------| 6 | | [terraform](#requirement\_terraform) | >=1.3 | 7 | | [gitlab](#requirement\_gitlab) | ~> 16.0 | 8 | 9 | ## Providers 10 | 11 | | Name | Version | 12 | |------|---------| 13 | | [gitlab](#provider\_gitlab) | ~> 16.0 | 14 | 15 | ## Modules 16 | 17 | No modules. 18 | 19 | ## Resources 20 | 21 | | Name | Type | 22 | |------|------| 23 | | [gitlab_group.group](https://registry.terraform.io/providers/gitlabhq/gitlab/16.5.0/docs/resources/group) | resource | 24 | 25 | ## Inputs 26 | 27 | | Name | Description | Type | Default | Required | 28 | |------|-------------|------|---------|:--------:| 29 | | [groups](#input\_groups) | Map for list of groups |
map(object({
name = string
path = string
auto_devops_enabled = optional(bool)
default_branch_protection = optional(number)
description = optional(string)
emails_disabled = optional(bool)
lfs_enabled = optional(bool)
mentions_disabled = optional(bool)
parent_id = number
project_creation_level = optional(string)
request_access_enabled = optional(bool)
require_two_factor_authentication = optional(bool)
share_with_group_lock = optional(bool)
subgroup_creation_level = optional(string)
two_factor_grace_period = optional(number)
visibility_level = optional(string)
}))
| n/a | yes | 30 | | [parent\_id](#input\_parent\_id) | Parent group id | `number` | `0` | no | 31 | 32 | ## Outputs 33 | 34 | | Name | Description | 35 | |------|-------------| 36 | | [created\_groups](#output\_created\_groups) | List of created groups with module | 37 | 38 | -------------------------------------------------------------------------------- /modules/gitlab-group-module/main.tf: -------------------------------------------------------------------------------- 1 | resource "gitlab_group" "group" { 2 | for_each = var.groups 3 | 4 | # required 5 | name = each.value.name 6 | path = each.value.path 7 | 8 | # optional 9 | auto_devops_enabled = lookup(each.value, "auto_devops_enabled", false) 10 | default_branch_protection = lookup(each.value, "default_branch_protection", 2) 11 | description = lookup(each.value, "description", "default_description") 12 | emails_disabled = lookup(each.value, "emails_disabled", false) 13 | lfs_enabled = lookup(each.value, "lfs_enabled", true) 14 | mentions_disabled = lookup(each.value, "mentions_disabled", false) 15 | parent_id = lookup(each.value, "parent_id", var.parent_id) 16 | project_creation_level = lookup(each.value, "project_creation_level", "Maintainer") 17 | request_access_enabled = lookup(each.value, "request_access_enabled", false) 18 | require_two_factor_authentication = lookup(each.value, "require_two_factor_authentication", false) 19 | share_with_group_lock = lookup(each.value, "share_with_group_lock", false) 20 | subgroup_creation_level = lookup(each.value, "subgroup_creation_level", "Owner") 21 | two_factor_grace_period = lookup(each.value, "two_factor_grace_period", 48) 22 | visibility_level = lookup(each.value, "visibility_level", "private") 23 | } 24 | -------------------------------------------------------------------------------- /modules/gitlab-group-module/outputs.tf: -------------------------------------------------------------------------------- 1 | output "created_groups" { 2 | description = "List of created groups with module" 3 | value = gitlab_group.group 4 | } 5 | -------------------------------------------------------------------------------- /modules/gitlab-group-module/variables.tf: -------------------------------------------------------------------------------- 1 | variable "groups" { 2 | description = "Map for list of groups" 3 | type = map(object({ 4 | name = string 5 | path = string 6 | auto_devops_enabled = optional(bool) 7 | default_branch_protection = optional(number) 8 | description = optional(string) 9 | emails_disabled = optional(bool) 10 | lfs_enabled = optional(bool) 11 | mentions_disabled = optional(bool) 12 | parent_id = number 13 | project_creation_level = optional(string) 14 | request_access_enabled = optional(bool) 15 | require_two_factor_authentication = optional(bool) 16 | share_with_group_lock = optional(bool) 17 | subgroup_creation_level = optional(string) 18 | two_factor_grace_period = optional(number) 19 | visibility_level = optional(string) 20 | })) 21 | } 22 | 23 | variable "parent_id" { 24 | description = "Parent group id" 25 | type = number 26 | default = 0 27 | } 28 | -------------------------------------------------------------------------------- /modules/gitlab-group-module/versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">=1.3" 3 | 4 | required_providers { 5 | gitlab = { 6 | source = "gitlabhq/gitlab" 7 | version = "~> 16.0" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /modules/gitlab-project-module/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Requirements 3 | 4 | | Name | Version | 5 | |------|---------| 6 | | [terraform](#requirement\_terraform) | >=1.3 | 7 | | [gitlab](#requirement\_gitlab) | ~> 16.0 | 8 | 9 | ## Providers 10 | 11 | | Name | Version | 12 | |------|---------| 13 | | [gitlab](#provider\_gitlab) | ~> 16.0 | 14 | 15 | ## Modules 16 | 17 | No modules. 18 | 19 | ## Resources 20 | 21 | | Name | Type | 22 | |------|------| 23 | | [gitlab_project.project](https://registry.terraform.io/providers/gitlabhq/gitlab/16.5.0/docs/resources/project) | resource | 24 | 25 | ## Inputs 26 | 27 | | Name | Description | Type | Default | Required | 28 | |------|-------------|------|---------|:--------:| 29 | | [projects](#input\_projects) | Map for list of projects |
map(object({
name = string
namespace_id = number
allow_merge_on_skipped_pipeline = optional(bool)
approvals_before_merge = optional(number)
archive_on_destroy = optional(bool)
archived = optional(bool)
build_coverage_regex = optional(string)
ci_config_path = optional(string)
ci_forward_deployment_enabled = optional(bool)
container_registry_enabled = optional(bool)
default_branch = optional(string)
description = optional(string)
group_with_project_templates_id = optional(number)
import_url = optional(string)
initialize_with_readme = optional(bool)
issues_enabled = optional(bool)
issues_template = optional(string)
lfs_enabled = optional(bool)
merge_method = optional(string)
merge_pipelines_enabled = optional(bool)
merge_requests_enabled = optional(bool)
merge_requests_template = optional(string)
merge_trains_enabled = optional(bool)
mirror = optional(bool)
mirror_overwrites_diverged_branches = optional(bool)
mirror_trigger_builds = optional(bool)
only_allow_merge_if_all_discussions_are_resolved = optional(bool)
only_allow_merge_if_pipeline_succeeds = optional(bool)
only_mirror_protected_branches = optional(bool)
packages_enabled = optional(bool)
pages_access_level = optional(string)
path = optional(string)
pipelines_enabled = optional(bool)
printing_merge_request_link_enabled = optional(bool)
push_rules = optional(object({
commit_committer_check = optional(bool)
deny_delete_tag = optional(bool)
max_file_size = optional(number)
member_check = optional(bool)
prevent_secrets = optional(bool)
reject_unsigned_commits = optional(bool)
author_email_regex = optional(string)
branch_name_regex = optional(string)
commit_message_negative_regex = optional(string)
commit_message_regex = optional(string)
file_name_regex = optional(string)
})
)
remove_source_branch_after_merge = optional(bool)
request_access_enabled = optional(bool)
shared_runners_enabled = optional(bool)
snippets_enabled = optional(bool)
squash_option = optional(string)
tags = optional(list(string))
template_name = optional(string)
template_project_id = optional(number)
use_custom_template = optional(bool)
visibility_level = optional(string)
wiki_enabled = optional(bool)
}))
| n/a | yes | 30 | 31 | ## Outputs 32 | 33 | | Name | Description | 34 | |------|-------------| 35 | | [created\_projects](#output\_created\_projects) | List of created projects with module | 36 | 37 | -------------------------------------------------------------------------------- /modules/gitlab-project-module/main.tf: -------------------------------------------------------------------------------- 1 | resource "gitlab_project" "project" { 2 | for_each = var.projects 3 | 4 | # required 5 | name = each.value.name 6 | namespace_id = each.value.namespace_id 7 | 8 | # optional 9 | allow_merge_on_skipped_pipeline = lookup(each.value, "allow_merge_on_skipped_pipeline", false) 10 | approvals_before_merge = lookup(each.value, "approvals_before_merge", 0) 11 | archive_on_destroy = lookup(each.value, "archive_on_destroy", null) 12 | archived = lookup(each.value, "archived", false) 13 | build_coverage_regex = lookup(each.value, "build_coverage_regex", "") 14 | ci_config_path = lookup(each.value, "ci_config_path", "") 15 | ci_forward_deployment_enabled = lookup(each.value, "ci_forward_deployment_enabled", true) 16 | container_registry_enabled = lookup(each.value, "container_registry_enabled", true) 17 | default_branch = lookup(each.value, "default_branch", "main") 18 | description = lookup(each.value, "description", "") 19 | group_with_project_templates_id = lookup(each.value, "group_with_project_templates_id", null) 20 | import_url = lookup(each.value, "import_url", "") 21 | initialize_with_readme = lookup(each.value, "initialize_with_readme", null) 22 | issues_enabled = lookup(each.value, "issues_enabled", true) 23 | issues_template = lookup(each.value, "issues_template", "") 24 | lfs_enabled = lookup(each.value, "lfs_enabled", true) 25 | merge_method = lookup(each.value, "merge_method", "merge") 26 | merge_pipelines_enabled = lookup(each.value, "merge_pipelines_enabled", false) 27 | merge_requests_enabled = lookup(each.value, "merge_requests_enabled", true) 28 | merge_requests_template = lookup(each.value, "merge_requests_template", "") 29 | merge_trains_enabled = lookup(each.value, "merge_trains_enabled", false) 30 | mirror = lookup(each.value, "mirror", false) 31 | mirror_overwrites_diverged_branches = lookup(each.value, "mirror_overwrites_diverged_branches", false) 32 | mirror_trigger_builds = lookup(each.value, "mirror_trigger_builds", false) 33 | only_allow_merge_if_all_discussions_are_resolved = lookup(each.value, "only_allow_merge_if_all_discussions_are_resolved", false) 34 | only_allow_merge_if_pipeline_succeeds = lookup(each.value, "only_allow_merge_if_pipeline_succeeds", false) 35 | only_mirror_protected_branches = lookup(each.value, "only_mirror_protected_branches", false) 36 | packages_enabled = lookup(each.value, "packages_enabled", true) 37 | pages_access_level = lookup(each.value, "pages_access_level", "private") 38 | path = lookup(each.value, "path", each.value.name) # set name as path 39 | pipelines_enabled = lookup(each.value, "pipelines_enabled", true) 40 | printing_merge_request_link_enabled = lookup(each.value, "printing_merge_request_link_enabled", true) 41 | remove_source_branch_after_merge = lookup(each.value, "remove_source_branch_after_merge", true) 42 | request_access_enabled = lookup(each.value, "request_access_enabled", true) 43 | shared_runners_enabled = lookup(each.value, "shared_runners_enabled", true) 44 | snippets_enabled = lookup(each.value, "snippets_enabled", true) 45 | squash_option = lookup(each.value, "squash_option", "default_off") 46 | tags = lookup(each.value, "tags", []) 47 | template_name = lookup(each.value, "template_name", null) 48 | template_project_id = lookup(each.value, "template_project_id", null) 49 | use_custom_template = lookup(each.value, "use_custom_template", null) 50 | visibility_level = lookup(each.value, "visibility_level", "private") 51 | wiki_enabled = lookup(each.value, "wiki_enabled", true) 52 | 53 | dynamic "push_rules" { 54 | for_each = lookup(each.value, "push_rules", null) != null ? [1] : [] 55 | content { 56 | commit_committer_check = lookup(each.value.push_rules, "commit_committer_check", false) 57 | deny_delete_tag = lookup(each.value.push_rules, "deny_delete_tag", false) 58 | max_file_size = lookup(each.value.push_rules, "max_file_size", 0) 59 | member_check = lookup(each.value.push_rules, "member_check", false) 60 | prevent_secrets = lookup(each.value.push_rules, "prevent_secrets", false) 61 | reject_unsigned_commits = lookup(each.value.push_rules, "reject_unsigned_commits", false) 62 | author_email_regex = lookup(each.value.push_rules, "author_email_regex", "") 63 | branch_name_regex = lookup(each.value.push_rules, "branch_name_regex", "") 64 | commit_message_negative_regex = lookup(each.value.push_rules, "commit_message_negative_regex", "") 65 | commit_message_regex = lookup(each.value.push_rules, "commit_message_regex", "") 66 | file_name_regex = lookup(each.value.push_rules, "file_name_regex", "") 67 | } 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /modules/gitlab-project-module/outputs.tf: -------------------------------------------------------------------------------- 1 | output "created_projects" { 2 | description = "List of created projects with module" 3 | value = gitlab_project.project 4 | } 5 | -------------------------------------------------------------------------------- /modules/gitlab-project-module/variables.tf: -------------------------------------------------------------------------------- 1 | variable "projects" { 2 | description = "Map for list of projects" 3 | type = map(object({ 4 | name = string 5 | namespace_id = number 6 | allow_merge_on_skipped_pipeline = optional(bool) 7 | approvals_before_merge = optional(number) 8 | archive_on_destroy = optional(bool) 9 | archived = optional(bool) 10 | build_coverage_regex = optional(string) 11 | ci_config_path = optional(string) 12 | ci_forward_deployment_enabled = optional(bool) 13 | container_registry_enabled = optional(bool) 14 | default_branch = optional(string) 15 | description = optional(string) 16 | group_with_project_templates_id = optional(number) 17 | import_url = optional(string) 18 | initialize_with_readme = optional(bool) 19 | issues_enabled = optional(bool) 20 | issues_template = optional(string) 21 | lfs_enabled = optional(bool) 22 | merge_method = optional(string) 23 | merge_pipelines_enabled = optional(bool) 24 | merge_requests_enabled = optional(bool) 25 | merge_requests_template = optional(string) 26 | merge_trains_enabled = optional(bool) 27 | mirror = optional(bool) 28 | mirror_overwrites_diverged_branches = optional(bool) 29 | mirror_trigger_builds = optional(bool) 30 | only_allow_merge_if_all_discussions_are_resolved = optional(bool) 31 | only_allow_merge_if_pipeline_succeeds = optional(bool) 32 | only_mirror_protected_branches = optional(bool) 33 | packages_enabled = optional(bool) 34 | pages_access_level = optional(string) 35 | path = optional(string) 36 | pipelines_enabled = optional(bool) 37 | printing_merge_request_link_enabled = optional(bool) 38 | push_rules = optional(object({ 39 | commit_committer_check = optional(bool) 40 | deny_delete_tag = optional(bool) 41 | max_file_size = optional(number) 42 | member_check = optional(bool) 43 | prevent_secrets = optional(bool) 44 | reject_unsigned_commits = optional(bool) 45 | author_email_regex = optional(string) 46 | branch_name_regex = optional(string) 47 | commit_message_negative_regex = optional(string) 48 | commit_message_regex = optional(string) 49 | file_name_regex = optional(string) 50 | }) 51 | ) 52 | remove_source_branch_after_merge = optional(bool) 53 | request_access_enabled = optional(bool) 54 | shared_runners_enabled = optional(bool) 55 | snippets_enabled = optional(bool) 56 | squash_option = optional(string) 57 | tags = optional(list(string)) 58 | template_name = optional(string) 59 | template_project_id = optional(number) 60 | use_custom_template = optional(bool) 61 | visibility_level = optional(string) 62 | wiki_enabled = optional(bool) 63 | })) 64 | } 65 | -------------------------------------------------------------------------------- /modules/gitlab-project-module/versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">=1.3" 3 | 4 | required_providers { 5 | gitlab = { 6 | source = "gitlabhq/gitlab" 7 | version = "~> 16.0" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /modules/gitlab-user-module/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Requirements 3 | 4 | | Name | Version | 5 | |------|---------| 6 | | [terraform](#requirement\_terraform) | >=1.3 | 7 | | [gitlab](#requirement\_gitlab) | ~> 16.0 | 8 | 9 | ## Providers 10 | 11 | | Name | Version | 12 | |------|---------| 13 | | [gitlab](#provider\_gitlab) | ~> 16.0 | 14 | 15 | ## Modules 16 | 17 | No modules. 18 | 19 | ## Resources 20 | 21 | | Name | Type | 22 | |------|------| 23 | | [gitlab_group_membership.group_membership](https://registry.terraform.io/providers/gitlabhq/gitlab/16.5.0/docs/resources/group_membership) | resource | 24 | | [gitlab_project_membership.project_membership](https://registry.terraform.io/providers/gitlabhq/gitlab/16.5.0/docs/resources/project_membership) | resource | 25 | | [gitlab_user.user](https://registry.terraform.io/providers/gitlabhq/gitlab/16.5.0/docs/resources/user) | resource | 26 | | [gitlab_user.user](https://registry.terraform.io/providers/gitlabhq/gitlab/16.5.0/docs/data-sources/user) | data source | 27 | 28 | ## Inputs 29 | 30 | | Name | Description | Type | Default | Required | 31 | |------|-------------|------|---------|:--------:| 32 | | [groups](#input\_groups) | Map for list of groups |
map(object({
id = number
name = string
path = string
auto_devops_enabled = optional(bool)
default_branch_protection = optional(number)
description = optional(string)
emails_disabled = optional(bool)
lfs_enabled = optional(bool)
mentions_disabled = optional(bool)
parent_id = number
project_creation_level = optional(string)
request_access_enabled = optional(bool)
require_two_factor_authentication = optional(bool)
share_with_group_lock = optional(bool)
subgroup_creation_level = optional(string)
two_factor_grace_period = optional(number)
visibility_level = optional(string)
}))
| n/a | yes | 33 | | [projects](#input\_projects) | Map for list of projects |
map(object({
id = number
name = string
namespace_id = number
allow_merge_on_skipped_pipeline = optional(bool)
approvals_before_merge = optional(number)
archive_on_destroy = optional(bool)
archived = optional(bool)
build_coverage_regex = optional(string)
ci_config_path = optional(string)
ci_forward_deployment_enabled = optional(bool)
container_registry_enabled = optional(bool)
default_branch = optional(string)
description = optional(string)
group_with_project_templates_id = optional(number)
import_url = optional(string)
initialize_with_readme = optional(bool)
issues_enabled = optional(bool)
issues_template = optional(string)
lfs_enabled = optional(bool)
merge_method = optional(string)
merge_pipelines_enabled = optional(bool)
merge_requests_enabled = optional(bool)
merge_requests_template = optional(string)
merge_trains_enabled = optional(bool)
mirror = optional(bool)
mirror_overwrites_diverged_branches = optional(bool)
mirror_trigger_builds = optional(bool)
only_allow_merge_if_all_discussions_are_resolved = optional(bool)
only_allow_merge_if_pipeline_succeeds = optional(bool)
only_mirror_protected_branches = optional(bool)
packages_enabled = optional(bool)
pages_access_level = optional(string)
path = optional(string)
pipelines_enabled = optional(bool)
printing_merge_request_link_enabled = optional(bool)
push_rules = optional(any)
remove_source_branch_after_merge = optional(bool)
request_access_enabled = optional(bool)
shared_runners_enabled = optional(bool)
snippets_enabled = optional(bool)
squash_option = optional(string)
tags = optional(list(string))
template_name = optional(string)
template_project_id = optional(number)
use_custom_template = optional(bool)
visibility_level = optional(string)
wiki_enabled = optional(bool)
}))
| n/a | yes | 34 | | [users](#input\_users) | Map for list of users with groups and projects membership |
map(object({
create = optional(bool)
username = string
email = string
name = string
password = optional(string)
can_create_group = optional(bool)
is_admin = optional(bool)
is_external = optional(bool)
note = optional(string)
projects_limit = optional(number)
reset_password = optional(bool)
skip_confirmation = optional(bool)
state = optional(string)
groups = map(object({
access_level = string
expires_at = optional(string) #format: YYYY-MM-DD
}))
projects = optional(map(object({
access_level = string
})))
}))
| n/a | yes | 35 | 36 | ## Outputs 37 | 38 | | Name | Description | 39 | |------|-------------| 40 | | [created\_users](#output\_created\_users) | List of created users with module | 41 | 42 | -------------------------------------------------------------------------------- /modules/gitlab-user-module/main.tf: -------------------------------------------------------------------------------- 1 | locals { 2 | 3 | user_groups = flatten([ 4 | for user_key, user in var.users : [ 5 | for group_key, group in user.groups : { 6 | user_key = user_key 7 | group_key = group_key 8 | username = user.username 9 | access_level = group.access_level 10 | expires_at = group.expires_at 11 | } 12 | ] 13 | ]) 14 | 15 | user_projects = flatten([ 16 | for user_key, user in var.users : [ 17 | for project_key, project in user.projects : { 18 | user_key = user_key 19 | project_key = project_key 20 | username = user.username 21 | access_level = project.access_level 22 | } 23 | ] if user.projects != null 24 | ]) 25 | } 26 | 27 | resource "gitlab_user" "user" { 28 | for_each = { 29 | for username, user in var.users : username => user if coalesce(user.create, false) 30 | } 31 | 32 | # required 33 | email = each.value.email 34 | name = each.value.name 35 | username = each.value.username 36 | 37 | # optional 38 | password = lookup(each.value, "password", null) 39 | can_create_group = lookup(each.value, "can_create_group", false) 40 | is_admin = lookup(each.value, "is_admin", false) 41 | is_external = lookup(each.value, "is_external", false) 42 | note = lookup(each.value, "note", "") 43 | projects_limit = lookup(each.value, "projects_limit", 0) 44 | reset_password = lookup(each.value, "reset_password", false) 45 | skip_confirmation = lookup(each.value, "skip_confirmation", true) 46 | state = lookup(each.value, "state", "active") 47 | } 48 | 49 | data "gitlab_user" "user" { 50 | for_each = var.users 51 | username = each.value.username 52 | 53 | depends_on = [ 54 | gitlab_user.user, 55 | ] 56 | } 57 | 58 | resource "gitlab_group_membership" "group_membership" { 59 | for_each = { 60 | for user_group in local.user_groups : "${user_group.user_key}.${user_group.group_key}" => user_group 61 | } 62 | group_id = var.groups[each.value.group_key].id 63 | user_id = data.gitlab_user.user[each.value.user_key].user_id 64 | access_level = each.value.access_level 65 | expires_at = lookup(each.value, "expires_at", null) 66 | } 67 | 68 | resource "gitlab_project_membership" "project_membership" { 69 | for_each = { 70 | for user_project in local.user_projects : "${user_project.user_key}.${user_project.project_key}" => user_project 71 | } 72 | project = var.projects[each.value.project_key].id 73 | user_id = data.gitlab_user.user[each.value.user_key].user_id 74 | access_level = each.value.access_level 75 | } 76 | -------------------------------------------------------------------------------- /modules/gitlab-user-module/outputs.tf: -------------------------------------------------------------------------------- 1 | output "created_users" { 2 | description = "List of created users with module" 3 | value = gitlab_user.user 4 | } 5 | -------------------------------------------------------------------------------- /modules/gitlab-user-module/variables.tf: -------------------------------------------------------------------------------- 1 | variable "users" { 2 | description = "Map for list of users with groups and projects membership" 3 | type = map(object({ 4 | create = optional(bool) 5 | username = string 6 | email = string 7 | name = string 8 | password = optional(string) 9 | can_create_group = optional(bool) 10 | is_admin = optional(bool) 11 | is_external = optional(bool) 12 | note = optional(string) 13 | projects_limit = optional(number) 14 | reset_password = optional(bool) 15 | skip_confirmation = optional(bool) 16 | state = optional(string) 17 | groups = map(object({ 18 | access_level = string 19 | expires_at = optional(string) #format: YYYY-MM-DD 20 | })) 21 | projects = optional(map(object({ 22 | access_level = string 23 | }))) 24 | })) 25 | } 26 | 27 | variable "groups" { 28 | description = "Map for list of groups" 29 | type = map(object({ 30 | id = number 31 | name = string 32 | path = string 33 | auto_devops_enabled = optional(bool) 34 | default_branch_protection = optional(number) 35 | description = optional(string) 36 | emails_disabled = optional(bool) 37 | lfs_enabled = optional(bool) 38 | mentions_disabled = optional(bool) 39 | parent_id = number 40 | project_creation_level = optional(string) 41 | request_access_enabled = optional(bool) 42 | require_two_factor_authentication = optional(bool) 43 | share_with_group_lock = optional(bool) 44 | subgroup_creation_level = optional(string) 45 | two_factor_grace_period = optional(number) 46 | visibility_level = optional(string) 47 | })) 48 | } 49 | 50 | variable "projects" { 51 | description = "Map for list of projects" 52 | type = map(object({ 53 | id = number 54 | name = string 55 | namespace_id = number 56 | allow_merge_on_skipped_pipeline = optional(bool) 57 | approvals_before_merge = optional(number) 58 | archive_on_destroy = optional(bool) 59 | archived = optional(bool) 60 | build_coverage_regex = optional(string) 61 | ci_config_path = optional(string) 62 | ci_forward_deployment_enabled = optional(bool) 63 | container_registry_enabled = optional(bool) 64 | default_branch = optional(string) 65 | description = optional(string) 66 | group_with_project_templates_id = optional(number) 67 | import_url = optional(string) 68 | initialize_with_readme = optional(bool) 69 | issues_enabled = optional(bool) 70 | issues_template = optional(string) 71 | lfs_enabled = optional(bool) 72 | merge_method = optional(string) 73 | merge_pipelines_enabled = optional(bool) 74 | merge_requests_enabled = optional(bool) 75 | merge_requests_template = optional(string) 76 | merge_trains_enabled = optional(bool) 77 | mirror = optional(bool) 78 | mirror_overwrites_diverged_branches = optional(bool) 79 | mirror_trigger_builds = optional(bool) 80 | only_allow_merge_if_all_discussions_are_resolved = optional(bool) 81 | only_allow_merge_if_pipeline_succeeds = optional(bool) 82 | only_mirror_protected_branches = optional(bool) 83 | packages_enabled = optional(bool) 84 | pages_access_level = optional(string) 85 | path = optional(string) 86 | pipelines_enabled = optional(bool) 87 | printing_merge_request_link_enabled = optional(bool) 88 | push_rules = optional(any) 89 | remove_source_branch_after_merge = optional(bool) 90 | request_access_enabled = optional(bool) 91 | shared_runners_enabled = optional(bool) 92 | snippets_enabled = optional(bool) 93 | squash_option = optional(string) 94 | tags = optional(list(string)) 95 | template_name = optional(string) 96 | template_project_id = optional(number) 97 | use_custom_template = optional(bool) 98 | visibility_level = optional(string) 99 | wiki_enabled = optional(bool) 100 | })) 101 | } 102 | -------------------------------------------------------------------------------- /modules/gitlab-user-module/versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">=1.3" 3 | 4 | required_providers { 5 | gitlab = { 6 | source = "gitlabhq/gitlab" 7 | version = "~> 16.0" 8 | } 9 | } 10 | } 11 | --------------------------------------------------------------------------------