├── CHANGELOG.md ├── Dockerfile ├── LICENSE ├── README.md ├── action.yml ├── examples ├── README.md ├── arguments.md ├── backends.md ├── credentials-file.md ├── output.md ├── tainting.md ├── terraform-versions.md ├── variables.md ├── working-directory.md └── workspaces.md └── src ├── main.sh ├── terragrunt_apply.sh ├── terragrunt_destroy.sh ├── terragrunt_fmt.sh ├── terragrunt_import.sh ├── terragrunt_init.sh ├── terragrunt_output.sh ├── terragrunt_plan.sh ├── terragrunt_taint.sh └── terragrunt_validate.sh /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## v0.8.0 4 | 5 | ### Added 6 | 7 | * Added an `import` subcommand. ([#117](https://github.com/hashicorp/terraform-github-actions/pull/117)) 8 | * Added a `taint` subcommand. ([#134](https://github.com/hashicorp/terraform-github-actions/pull/134)) 9 | 10 | ### Changed 11 | 12 | * Use unary operator to test for non-empty variable. ([#145](https://github.com/hashicorp/terraform-github-actions/pull/145)) 13 | 14 | ## v0.7.1 15 | 16 | ### Fixed 17 | 18 | * Fixed missing `tf_actions_plan_has_changes` output when `plan` exit code is `0`. ([#136](https://github.com/hashicorp/terraform-github-actions/pull/136)) 19 | 20 | ## v0.7.0 21 | 22 | ### Added 23 | 24 | * Added `tf_actions_plan_output` output. ([#119](https://github.com/hashicorp/terraform-github-actions/pull/119)) 25 | 26 | ### Changed 27 | 28 | * Removed unecessary step in `Dockerfile`. ([#132](https://github.com/hashicorp/terraform-github-actions/pull/132)) 29 | 30 | ### Fixed 31 | 32 | * Process multi-line outputs correctly. ([#116](https://github.com/hashicorp/terraform-github-actions/pull/116)) 33 | * Fixed typo in outputs documentation. ([#126](https://github.com/hashicorp/terraform-github-actions/pull/126)) 34 | 35 | ## v0.6.4 36 | 37 | ### Added 38 | 39 | * Added the ability to download latest stable Terraform version when `tf_actions_version` is set to `latest`. 40 | 41 | ## v0.6.3 42 | 43 | ### Added 44 | 45 | * Added the ability to configure a CLI credentials file to authenticate to Terraform Cloud/Enterprise. 46 | 47 | ## v0.6.2 48 | 49 | ### Added 50 | 51 | * Added an `output` subcommand and corresponding `tf_actions_output` output. 52 | 53 | ### Fixed 54 | 55 | * Fixed improper passing of arguments to the subcommand. ([#114](https://github.com/hashicorp/terraform-github-actions/issues/114)) 56 | 57 | ## v0.6.1 58 | 59 | ### Fixed 60 | 61 | * Fixed improper handling of `args` in each `terraform` command when `args` contained no value. ([#109](https://github.com/hashicorp/terraform-github-actions/issues/109)) ([#110](https://github.com/hashicorp/terraform-github-actions/issues/110)) 62 | 63 | ## v0.6.0 64 | 65 | ### Added 66 | 67 | * Allow passing arguments using GitHub Actions `args` attribute. ([#105](https://github.com/hashicorp/terraform-github-actions/issues/105)) 68 | 69 | ### Changed 70 | 71 | * Updated examples to reflect new additions. 72 | 73 | ## v0.5.4 74 | 75 | ### Changed 76 | 77 | * Always post a comment on a pull request regardless of exit code when using `apply`. ([#97](https://github.com/hashicorp/terraform-github-actions/issues/97)) 78 | * Pass comment content to `jq` using pipes instead of arguments. 79 | 80 | ## v0.5.3 81 | 82 | ### Fixed 83 | 84 | * Fixed improper comment formatting on `fmt`, `plan`, and `apply`. 85 | 86 | ## v0.5.2 87 | 88 | ### Fixed 89 | 90 | * Fixed an error with `terraform fmt` processing STDERR output when `TF_LOG` was set. 91 | 92 | ## v0.5.1 93 | 94 | ### Fixed 95 | 96 | * Do not use `-recursive` option with `terraform fmt` for Terraform 0.11.x. ([#90](https://github.com/hashicorp/terraform-github-actions/issues/90)) 97 | 98 | ## v0.5.0 99 | 100 | ### Added 101 | 102 | * Added new YAML syntax for GitHub Actions. 103 | 104 | ### Changed 105 | 106 | * Completely refactored the codebase into one GitHub Action. Please refer to the README for current usage. 107 | 108 | ### Removed 109 | 110 | * Removed all `TF_ACTION` environment variables. Please refer to the README for current usage. 111 | * Removed HashiCorp Configuration Language (HCL) syntax. 112 | 113 | ### Fixed 114 | 115 | * The actions now use the new YAML syntax. ([#67](https://github.com/hashicorp/terraform-github-actions/issues/67)) 116 | * Added support for Terraform 0.11.14. ([#42](https://github.com/hashicorp/terraform-github-actions/issues/67)) 117 | * Comments will not be posted to pull requests when `terraform plan` contains no changes. ([#29](https://github.com/hashicorp/terraform-github-actions/issues/67)) 118 | * Added ability to specify a Terraform version to use. ([#23](https://github.com/hashicorp/terraform-github-actions/issues/67)) 119 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3 2 | 3 | RUN ["/bin/sh", "-c", "apk add --update --no-cache bash ca-certificates curl git jq openssh"] 4 | 5 | COPY ["src", "/src/"] 6 | 7 | ENTRYPOINT ["/src/main.sh"] 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Terragrunt GitHub Actions 2 | 3 | Terragrunt GitHub Actions allow you to execute Terragrunt commands within GitHub Actions. 4 | 5 | The output of the actions can be viewed from the Actions tab in the main repository view. If the actions are executed on a pull request event, a comment may be posted on the pull request. 6 | 7 | Terragrunt GitHub Actions are a single GitHub Action that executes different Terragrunt subcommands depending on the content of the GitHub Actions YAML file. 8 | 9 | ## Success Criteria 10 | 11 | An exit code of `0` is considered a successful execution. 12 | 13 | ## Usage 14 | 15 | The most common workflow is to run `terragrunt fmt`, `terragrunt init`, `terragrunt validate`, `terragrunt plan`, and `terragrunt taint` on all of the Terragrunt files in the root of the repository when a pull request is opened or updated. A comment will be posted to the pull request depending on the output of the Terragrunt subcommand being executed. This workflow can be configured by adding the following content to the GitHub Actions workflow YAML file. Note that this action will use `terragrunt` binary to run all commands. In case of passing a `terraform` subcommand `terragrunt` will forward it to `terraform`. 16 | 17 | ```yaml 18 | name: 'Terragrunt GitHub Actions' 19 | on: 20 | - pull_request 21 | env: 22 | tf_version: 'latest' 23 | tg_version: 'latest' 24 | tf_working_dir: '.' 25 | jobs: 26 | terragrunt: 27 | name: 'Terragrunt' 28 | runs-on: ubuntu-latest 29 | steps: 30 | - name: 'Checkout' 31 | uses: actions/checkout@master 32 | - name: 'Terragrunt Format' 33 | uses: the-commons-project/terragrunt-github-actions@master 34 | with: 35 | tf_actions_version: ${{ env.tf_version }} 36 | tg_actions_version: ${{ env.tg_version }} 37 | tf_actions_binary: 'terraform' 38 | tf_actions_subcommand: 'fmt' 39 | tf_actions_working_dir: ${{ env.tf_working_dir }} 40 | tf_actions_comment: true 41 | env: 42 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 43 | - name: 'Terragrunt Init' 44 | uses: the-commons-project/terragrunt-github-actions@master 45 | with: 46 | tf_actions_version: ${{ env.tf_version }} 47 | tg_actions_version: ${{ env.tg_version }} 48 | tf_actions_subcommand: 'init' 49 | tf_actions_working_dir: ${{ env.tf_working_dir }} 50 | tf_actions_comment: true 51 | env: 52 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 53 | - name: 'Terragrunt Validate' 54 | uses: the-commons-project/terragrunt-github-actions@master 55 | with: 56 | tf_actions_version: ${{ env.tf_version }} 57 | tg_actions_version: ${{ env.tg_version }} 58 | tf_actions_binary: 'terraform' 59 | tf_actions_subcommand: 'validate' 60 | tf_actions_working_dir: ${{ env.tf_working_dir }} 61 | tf_actions_comment: true 62 | env: 63 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 64 | - name: 'Terragrunt Plan' 65 | uses: the-commons-project/terragrunt-github-actions@master 66 | with: 67 | tf_actions_version: ${{ env.tf_version }} 68 | tg_actions_version: ${{ env.tg_version }} 69 | tf_actions_subcommand: 'plan' 70 | tf_actions_working_dir: ${{ env.tf_working_dir }} 71 | tf_actions_comment: true 72 | env: 73 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 74 | ``` 75 | 76 | This was a simplified example showing the basic features of these Terragrunt GitHub Actions. Please refer to the examples within the `examples` directory for other common workflows. 77 | 78 | ## Inputs 79 | 80 | Inputs configure Terraform GitHub Actions to perform different actions. 81 | 82 | | Input Name | Description | Required | 83 | |:------------------------------------|:-----------------------------------------------------------|:--------:| 84 | | tf_actions_subcommand | The Terraform/Terragrunt subcommand to execute. | `Yes` | 85 | | tf_actions_binary | The binary to run the commands with | `No` | 86 | | tf_actions_version | The Terraform version to install and execute. If set to `latest`, the latest stable version will be used. | `Yes` | 87 | | tg_actions_version | The Terragrunt version to install and execute. If set to `latest`, the latest stable version will be used. | `Yes` | 88 | | tf_actions_cli_credentials_hostname | Hostname for the CLI credentials file. Defaults to `app.terraform.io`. | `No` | 89 | | tf_actions_cli_credentials_token | Token for the CLI credentials file. | `No` | 90 | | tf_actions_comment | Whether or not to comment on GitHub pull requests. Defaults to `true`. | `No` | 91 | | tf_actions_working_dir | The working directory to change into before executing Terragrunt subcommands. Defaults to the root of the GitHub repository. | `No` | 92 | | tf_actions_fmt_write | Whether or not to write `fmt` changes to source files. Defaults to `false`. | `No` | 93 | 94 | ## Outputs 95 | 96 | Outputs are used to pass information to subsequent GitHub Actions steps. 97 | 98 | | Output Name | Description | 99 | |:----------------------------|:---------------------------------------------------------------------------------| 100 | | tf_actions_output | The Terragrunt outputs in (stringified) JSON format. | 101 | | tf_actions_plan_has_changes | `'true'` if the Terragrunt plan contained changes, otherwise `'false'`. | 102 | | tf_actions_plan_output | The Terragrunt plan output. | 103 | | tf_actions_fmt_written | Whether or not the Terragrunt formatting from `fmt` was written to source files. | 104 | 105 | ## Secrets 106 | 107 | Secrets are similar to inputs except that they are encrypted and only used by GitHub Actions. It's a convenient way to keep sensitive data out of the GitHub Actions workflow YAML file. 108 | 109 | * `GITHUB_TOKEN` - (Optional) The GitHub API token used to post comments to pull requests. Not required if the `tf_actions_comment` input is set to `false`. 110 | 111 | Other secrets may be needed to authenticate with Terraform backends and providers. 112 | 113 | **WARNING:** These secrets could be exposed if the action is executed on a malicious Terraform file. To avoid this, it is recommended not to use these Terraform GitHub Actions on repositories where untrusted users can submit pull requests. 114 | 115 | ## Environment Variables 116 | 117 | Environment variables are exported in the environment where the Terraform GitHub Actions are executed. This allows a user to modify the behavior of certain GitHub Actions. 118 | 119 | The usual [Terraform environment variables](https://www.terraform.io/docs/commands/environment-variables.html) are supported. Here are a few of the more commonly used environment variables. 120 | 121 | * [`TF_LOG`](https://www.terraform.io/docs/commands/environment-variables.html#tf_log) 122 | * [`TF_VAR_name`](https://www.terraform.io/docs/commands/environment-variables.html#tf_var_name) 123 | * [`TF_CLI_ARGS`](https://www.terraform.io/docs/commands/environment-variables.html#tf_cli_args-and-tf_cli_args_name) 124 | * [`TF_CLI_ARGS_name`](https://www.terraform.io/docs/commands/environment-variables.html#tf_cli_args-and-tf_cli_args_name) 125 | * `TF_WORKSPACE` 126 | 127 | Other environment variables may be configured to pass data into Terraform. If the data is sensitive, consider using [secrets](#secrets) instead. 128 | 129 | **This is a fork of [Terraform Github Actions](https://github.com/hashicorp/terraform-github-actions).** -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'Terragrunt GitHub Actions' 2 | description: 'Runs Terragrunt commands via GitHub Actions.' 3 | author: 'HashiCorp, Inc. Terraform Team ' 4 | branding: 5 | icon: 'cloud' 6 | color: 'purple' 7 | inputs: 8 | tf_actions_subcommand: 9 | description: 'Terraform or Terragrunt subcommand to execute.' 10 | required: true 11 | tf_actions_binary: 12 | description: 'Binary to use. Terraform or Terragrunt' 13 | default: 'terragrunt' 14 | tf_actions_version: 15 | description: 'Terraform version to install.' 16 | required: true 17 | default: 'latest' 18 | tg_actions_version: 19 | description: 'Terragrunt version to install.' 20 | required: true 21 | default: 'latest' 22 | tf_actions_cli_credentials_hostname: 23 | description: 'Hostname for the CLI credentials file.' 24 | default: 'app.terraform.io' 25 | tf_actions_cli_credentials_token: 26 | description: 'Token for the CLI credentials file.' 27 | tf_actions_comment: 28 | description: 'Whether or not to comment on pull requests.' 29 | default: true 30 | tf_actions_working_dir: 31 | description: 'Terragrunt working directory.' 32 | default: '.' 33 | tf_actions_fmt_write: 34 | description: 'Write Terragrunt fmt changes to source files.' 35 | default: false 36 | outputs: 37 | tf_actions_output: 38 | description: 'The Terragrunt outputs in JSON format.' 39 | tf_actions_plan_has_changes: 40 | description: 'Whether or not the Terragrunt plan contained changes.' 41 | tf_actions_plan_output: 42 | description: 'The Terragrunt plan output.' 43 | tf_actions_fmt_written: 44 | description: 'Whether or not the Terragrunt formatting was written to source files.' 45 | runs: 46 | using: 'docker' 47 | image: './Dockerfile' 48 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | # Examples 2 | 3 | Here are some examples showing common workflows using Terragrunt GitHub Actions. 4 | -------------------------------------------------------------------------------- /examples/arguments.md: -------------------------------------------------------------------------------- 1 | # Arguments 2 | 3 | Arguments can be passed to each subcommand in two ways. 4 | 5 | ## Using Arguments 6 | 7 | GitHub Actions supports an `args` attribute that will pass arguments to the Terragrunt subcommand. Using this `args` attribute will place the arguments at the end of the entire `terragrunt` command, even after all of the arguments defined in the source code. In this example, the argument `-var="env=dev"` will be appended to the `terragrunt init` command. 8 | 9 | ```yaml 10 | name: 'Terragrunt GitHub Actions' 11 | on: 12 | - pull_request 13 | jobs: 14 | terragrunt: 15 | name: 'Terragrunt' 16 | runs-on: ubuntu-latest 17 | steps: 18 | - name: 'Checkout' 19 | uses: actions/checkout@master 20 | - name: 'Terragrunt Init' 21 | uses: the-commons-project/terragrunt-github-actions@master 22 | with: 23 | tf_actions_version: 0.12.13 24 | tg_actions_version: 'latest' 25 | tf_actions_subcommand: 'init' 26 | tf_actions_working_dir: '.' 27 | tf_actions_comment: true 28 | args: '-var="env=dev"' 29 | env: 30 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 31 | ``` 32 | 33 | 34 | ## Using Environment Variables 35 | 36 | Terragrunt supports environment variables named `TF_CLI_ARGS` and `TF_CLI_ARG_name` where `name` is the subcommand that is being executed. Using these environment variables will place the arguments after the subcommand but before any arguments defined in the source code. In this example, the argument `-var="env=dev"` will be appended to the `terragrunt init` command. 37 | 38 | ```yaml 39 | name: 'Terragrunt GitHub Actions' 40 | on: 41 | - pull_request 42 | jobs: 43 | terragrunt: 44 | name: 'Terragrunt' 45 | runs-on: ubuntu-latest 46 | steps: 47 | - name: 'Checkout' 48 | uses: actions/checkout@master 49 | - name: 'Terragrunt Init' 50 | uses: the-commons-project/terragrunt-github-actions@master 51 | with: 52 | tf_actions_version: 0.12.13 53 | tg_actions_version: 'latest' 54 | tf_actions_subcommand: 'init' 55 | tf_actions_working_dir: '.' 56 | tf_actions_comment: true 57 | env: 58 | TF_CLI_ARGS_init: '-var="env=dev"' 59 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 60 | ``` 61 | -------------------------------------------------------------------------------- /examples/backends.md: -------------------------------------------------------------------------------- 1 | # Terragrunt Backends 2 | 3 | Terragrunt GitHub Actions supports initializing a `backend` block using the `-backend-config` option. 4 | 5 | The example below shows how to pass the `token` and `organization` arguments to the `remote` backend block. The `token` argument is passed using GitHub Actions secrets while the organization is hardcoded. 6 | 7 | ```yaml 8 | name: 'Terragrunt GitHub Actions' 9 | on: 10 | - pull_request 11 | jobs: 12 | terragrunt: 13 | name: 'Terragrunt' 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: 'Checkout' 17 | uses: actions/checkout@master 18 | - name: 'Terragrunt Init' 19 | uses: the-commons-project/terragrunt-github-actions@master 20 | with: 21 | tf_actions_version: 0.12.13 22 | tg_actions_version: 'latest' 23 | tf_actions_subcommand: 'init' 24 | tf_actions_working_dir: '.' 25 | tf_actions_comment: true 26 | args: '-backend-config="token=${{ secrets.TF_API_TOKEN }}" -backend-config="organization=CHANGE_ME"' 27 | env: 28 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 29 | ``` 30 | -------------------------------------------------------------------------------- /examples/credentials-file.md: -------------------------------------------------------------------------------- 1 | # Terragrunt CLI Credentials File 2 | 3 | The Terraform CLI credentials file is used to authenticate to Terraform Cloud/Enterprise. This is useful if the Terraform configuration contains many `terraform_remote_state` data sources that read from the same Terraform Cloud/Enterprise instance or if the configuration uses modules located in the Private Module Registry. 4 | 5 | This example shows how to pass the hostname and token needed to create the CLI credentials file. 6 | 7 | ```yaml 8 | name: 'Terragrunt GitHub Actions' 9 | on: 10 | - pull_request 11 | jobs: 12 | terraform: 13 | name: 'Terragrunt' 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: 'Checkout' 17 | uses: actions/checkout@master 18 | - name: 'Terragrunt Init' 19 | uses: the-commons-project/terragrunt-github-actions@master 20 | with: 21 | tf_actions_version: 0.12.13 22 | tg_actions_version: 'latest' 23 | tf_actions_subcommand: 'init' 24 | tf_actions_working_dir: '.' 25 | tf_actions_comment: true 26 | tf_actions_cli_credentials_hostname: app.terraform.io 27 | tf_actions_cli_credentials_token: ${{ secrets.TF_API_TOKEN }} 28 | env: 29 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 30 | - name: 'Terragrunt Plan' 31 | uses: the-commons-project/terragrunt-github-actions@master 32 | with: 33 | tf_actions_version: 0.12.13 34 | tg_actions_version: 'latest' 35 | tf_actions_subcommand: 'plan' 36 | tf_actions_working_dir: '.' 37 | ``` 38 | -------------------------------------------------------------------------------- /examples/output.md: -------------------------------------------------------------------------------- 1 | # Terragrunt Output 2 | 3 | If you need the outputs of your Terraform configuration later in your GitHub Actions workflow, you can use the `output` subcommand. 4 | 5 | ```yaml 6 | name: 'Terragrunt GitHub Actions' 7 | on: 8 | - push 9 | jobs: 10 | terraform: 11 | name: 'Terragrunt' 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: 'Checkout' 15 | uses: actions/checkout@master 16 | - name: 'Terraform Output' 17 | id: terraform 18 | uses: the-commons-project/terraform-github-actions@master 19 | with: 20 | tf_actions_version: 0.12.13 21 | tg_actions_version: 'latest' 22 | tf_actions_subcommand: 'output' 23 | tf_actions_working_dir: '.' 24 | - name: 'Use Terraform Output' 25 | run: echo ${{ steps.terraform.outputs.tf_actions_output }} 26 | - name: 'Pull specific database name from outputs' 27 | run: | 28 | apt-get install jq 29 | DBNAME=$(echo ${{ steps.terraform.outputs.tf_actions_output }} | jq -r '.database.value.name') 30 | echo $DBNAME 31 | ``` 32 | 33 | In this example the `tf_actions_output` would contain the following content. 34 | 35 | ```json 36 | { 37 | "database": { 38 | "value": { 39 | "name": "test-database" 40 | } 41 | } 42 | } 43 | ``` 44 | -------------------------------------------------------------------------------- /examples/tainting.md: -------------------------------------------------------------------------------- 1 | # Terragrunt Tainting 2 | 3 | Resources to taint can be specified using the `args` with option. 4 | 5 | ```yaml 6 | name: 'Terragrunt GitHub Actions' 7 | on: 8 | - pull_request 9 | jobs: 10 | terraform: 11 | name: 'Terragrunt' 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: 'Checkout' 15 | uses: actions/checkout@master 16 | - name: 'Terragrunt Init' 17 | uses: the-commons-project/terraform-github-actions@master 18 | with: 19 | tf_actions_version: 0.12.13 20 | tg_actions_version: 'latest' 21 | tf_actions_subcommand: 'init' 22 | tf_actions_working_dir: '.' 23 | tf_actions_comment: true 24 | env: 25 | TF_WORKSPACE: dev 26 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 27 | - name: 'Terraform Taint' 28 | uses: the-commons-project/terraform-github-actions@master 29 | with: 30 | tf_actions_version: 0.12.13 31 | tg_actions_version: 'latest' 32 | tf_actions_subcommand: 'taint' 33 | tf_actions_working_dir: '.' 34 | tf_actions_comment: true 35 | args: 'aws_instance.host' 36 | env: 37 | TF_WORKSPACE: dev 38 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 39 | ``` 40 | 41 | Multiple resources can be specified by separating with spaces: `args: 'aws_instance.host1 aws_instance.host2'`. 42 | -------------------------------------------------------------------------------- /examples/terraform-versions.md: -------------------------------------------------------------------------------- 1 | # Terragrunt Versions 2 | 3 | Specify the version of Terraform to be installed using the `tf_actions_version` input. Here, Terraform 0.12.13 is being used. 4 | 5 | Specify the version of Terragrunt to be installed and executed using the `tg_actions_version` input. Here v0.22.3 will be used. 6 | 7 | ```yaml 8 | name: 'Terragrunt GitHub Actions' 9 | on: 10 | - pull_request 11 | jobs: 12 | terraform: 13 | name: 'Terragrunt' 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: 'Checkout' 17 | uses: actions/checkout@master 18 | - name: 'Terragrunt Init' 19 | uses: the-commons-project/terraform-github-actions@master 20 | with: 21 | tf_actions_version: 0.12.13 22 | tg_actions_version: 'v0.22.3' 23 | tf_actions_subcommand: 'init' 24 | tf_actions_working_dir: '.' 25 | tf_actions_comment: true 26 | env: 27 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 28 | ``` 29 | -------------------------------------------------------------------------------- /examples/variables.md: -------------------------------------------------------------------------------- 1 | # Terragrunt Variables 2 | 3 | Variables can be configured directly in the GitHub Actions workflow YAML a few ways. 4 | 5 | ## Using Arguments 6 | 7 | This example shows how to pass variables using the `-var` argument. 8 | 9 | ```yaml 10 | name: 'Terragrunt GitHub Actions' 11 | on: 12 | - pull_request 13 | jobs: 14 | terraform: 15 | name: 'Terragrunt' 16 | runs-on: ubuntu-latest 17 | steps: 18 | - name: 'Checkout' 19 | uses: actions/checkout@master 20 | - name: 'Terragrunt Init' 21 | uses: the-commons-project/terraform-github-actions@master 22 | with: 23 | tf_actions_version: 0.12.13 24 | tg_actions_version: 'latest' 25 | tf_actions_subcommand: 'init' 26 | tf_actions_working_dir: '.' 27 | tf_actions_comment: true 28 | args: '-var="env=dev"' 29 | env: 30 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 31 | ``` 32 | 33 | This example shows how to use a variable file using the `-var-file` argument. 34 | 35 | ```yaml 36 | name: 'Terragrunt GitHub Actions' 37 | on: 38 | - pull_request 39 | jobs: 40 | terraform: 41 | name: 'Terragrunt' 42 | runs-on: ubuntu-latest 43 | steps: 44 | - name: 'Checkout' 45 | uses: actions/checkout@master 46 | - name: 'Terragrunt Init' 47 | uses: the-commons-project/terraform-github-actions@master 48 | with: 49 | tf_actions_version: 0.12.13 50 | tg_actions_version: 'latest' 51 | tf_actions_subcommand: 'init' 52 | tf_actions_working_dir: '.' 53 | tf_actions_comment: true 54 | args: '-var-file="dev.tfvars"' 55 | env: 56 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 57 | ``` 58 | 59 | 60 | ## Using an Environment Variable 61 | 62 | The `TF_VAR_name` environment variable can be used to define a value for a variable. When using `TF_VAR_name`,`name` is the name of the Terraform variable as declared in the Terraform files. 63 | 64 | Here, the Terraform variable `env` is set to the value `dev`. 65 | 66 | ```yaml 67 | name: 'Terragrunt GitHub Actions' 68 | on: 69 | - pull_request 70 | jobs: 71 | terraform: 72 | name: 'Terragrunt' 73 | runs-on: ubuntu-latest 74 | steps: 75 | - name: 'Checkout' 76 | uses: actions/checkout@master 77 | - name: 'Terragrunt Init' 78 | uses: the-commons-project/terraform-github-actions@master 79 | with: 80 | tf_actions_version: 0.12.13 81 | tg_actions_version: 'latest' 82 | tf_actions_subcommand: 'init' 83 | tf_actions_working_dir: '.' 84 | tf_actions_comment: true 85 | env: 86 | TF_VAR_env: 'dev' 87 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 88 | ``` 89 | -------------------------------------------------------------------------------- /examples/working-directory.md: -------------------------------------------------------------------------------- 1 | # Working Directory 2 | 3 | Terragrunt GitHub Actions only supports running in a single working directory at a time. The working directory is set using the `tf_actions_working_dir` input. By default, the working directory is set to `.` which refers to the root of the GitHub repository. 4 | 5 | ```yaml 6 | name: 'Terragrunt GitHub Actions' 7 | on: 8 | - pull_request 9 | jobs: 10 | root: 11 | name: 'Terragrunt (root)' 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: 'Checkout' 15 | uses: actions/checkout@master 16 | - name: 'Terragrunt Init' 17 | uses: the-commons-project/terragrunt-github-actions@master 18 | with: 19 | tf_actions_version: 0.12.13 20 | tg_actions_version: 'latest' 21 | tf_actions_subcommand: 'init' 22 | tf_actions_working_dir: '.' 23 | tf_actions_comment: true 24 | env: 25 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 26 | ``` 27 | 28 | If you need to run the Terragrunt Actions in multiple directories, you have to create separate jobs for each working directory. 29 | 30 | ```yaml 31 | name: 'Terragrunt GitHub Actions' 32 | on: 33 | - pull_request 34 | jobs: 35 | directory1: 36 | name: 'Terragrunt (directory1)' 37 | runs-on: ubuntu-latest 38 | steps: 39 | - name: 'Checkout' 40 | uses: actions/checkout@master 41 | - name: 'Terragrunt Init' 42 | uses: the-commons-project/terragrunt-github-actions@master 43 | with: 44 | tf_actions_version: 0.12.13 45 | tg_actions_version: 'latest' 46 | tf_actions_subcommand: 'init' 47 | tf_actions_working_dir: 'directory1' 48 | tf_actions_comment: true 49 | env: 50 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 51 | directory2: 52 | name: 'Terragrunt (directory2)' 53 | runs-on: ubuntu-latest 54 | steps: 55 | - name: 'Checkout' 56 | uses: actions/checkout@master 57 | - name: 'Terragrunt Init' 58 | uses: the-commons-project/terragrunt-github-actions@master 59 | with: 60 | tf_actions_version: 0.12.13 61 | tg_actions_version: 'latest' 62 | tf_actions_subcommand: 'init' 63 | tf_actions_working_dir: 'directory2' 64 | tf_actions_comment: true 65 | env: 66 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 67 | ``` 68 | -------------------------------------------------------------------------------- /examples/workspaces.md: -------------------------------------------------------------------------------- 1 | # Terragrunt Workspaces 2 | 3 | The workspace can be specified using the `TF_WORKSPACE` environment variable. 4 | 5 | ```yaml 6 | name: 'Terragrunt GitHub Actions' 7 | on: 8 | - pull_request 9 | jobs: 10 | terraform: 11 | name: 'Terragrunt' 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: 'Checkout' 15 | uses: actions/checkout@master 16 | - name: 'Terragrunt Init' 17 | uses: the-commons-project/terraform-github-actions@master 18 | with: 19 | tf_actions_version: 0.12.13 20 | tg_actions_version: 'latest' 21 | tg_actions_subcommand: 'init' 22 | tg_actions_working_dir: '.' 23 | tg_actions_comment: true 24 | env: 25 | TF_WORKSPACE: dev 26 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 27 | ``` 28 | 29 | If using the `remote` backend with the `name` argument, the configured workspace will be created for you. If using the `remote` backend with the `prefix` argument, the configured workspace must already exist and will not be created for you. 30 | -------------------------------------------------------------------------------- /src/main.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function stripColors { 4 | echo "${1}" | sed 's/\x1b\[[0-9;]*m//g' 5 | } 6 | 7 | function hasPrefix { 8 | case ${2} in 9 | "${1}"*) 10 | true 11 | ;; 12 | *) 13 | false 14 | ;; 15 | esac 16 | } 17 | 18 | function parseInputs { 19 | # Required inputs 20 | if [ "${INPUT_TF_ACTIONS_VERSION}" != "" ]; then 21 | tfVersion=${INPUT_TF_ACTIONS_VERSION} 22 | else 23 | echo "Input terraform_version cannot be empty" 24 | exit 1 25 | fi 26 | 27 | if [ "${INPUT_TG_ACTIONS_VERSION}" != "" ]; then 28 | tgVersion=${INPUT_TG_ACTIONS_VERSION} 29 | else 30 | echo "Input terragrunt_version cannot be empty" 31 | exit 1 32 | fi 33 | 34 | if [ "${INPUT_TF_ACTIONS_SUBCOMMAND}" != "" ]; then 35 | tfSubcommand=${INPUT_TF_ACTIONS_SUBCOMMAND} 36 | else 37 | echo "Input terraform_subcommand cannot be empty" 38 | exit 1 39 | fi 40 | 41 | # Optional inputs 42 | tfWorkingDir="." 43 | if [[ -n "${INPUT_TF_ACTIONS_WORKING_DIR}" ]]; then 44 | tfWorkingDir=${INPUT_TF_ACTIONS_WORKING_DIR} 45 | fi 46 | 47 | tfBinary="terragrunt" 48 | if [[ -n "${INPUT_TF_ACTIONS_BINARY}" ]]; then 49 | tfBinary=${INPUT_TF_ACTIONS_BINARY} 50 | fi 51 | 52 | tfComment=0 53 | if [ "${INPUT_TF_ACTIONS_COMMENT}" == "1" ] || [ "${INPUT_TF_ACTIONS_COMMENT}" == "true" ]; then 54 | tfComment=1 55 | fi 56 | 57 | tfCLICredentialsHostname="" 58 | if [ "${INPUT_TF_ACTIONS_CLI_CREDENTIALS_HOSTNAME}" != "" ]; then 59 | tfCLICredentialsHostname=${INPUT_TF_ACTIONS_CLI_CREDENTIALS_HOSTNAME} 60 | fi 61 | 62 | tfCLICredentialsToken="" 63 | if [ "${INPUT_TF_ACTIONS_CLI_CREDENTIALS_TOKEN}" != "" ]; then 64 | tfCLICredentialsToken=${INPUT_TF_ACTIONS_CLI_CREDENTIALS_TOKEN} 65 | fi 66 | 67 | tfFmtWrite=0 68 | if [ "${INPUT_TF_ACTIONS_FMT_WRITE}" == "1" ] || [ "${INPUT_TF_ACTIONS_FMT_WRITE}" == "true" ]; then 69 | tfFmtWrite=1 70 | fi 71 | 72 | tfWorkspace="default" 73 | if [ -n "${TF_WORKSPACE}" ]; then 74 | tfWorkspace="${TF_WORKSPACE}" 75 | fi 76 | } 77 | 78 | function configureCLICredentials { 79 | if [[ ! -f "${HOME}/.terraformrc" ]] && [[ "${tfCLICredentialsToken}" != "" ]]; then 80 | cat > ${HOME}/.terraformrc << EOF 81 | credentials "${tfCLICredentialsHostname}" { 82 | token = "${tfCLICredentialsToken}" 83 | } 84 | EOF 85 | fi 86 | } 87 | 88 | function installTerraform { 89 | if [[ "${tfVersion}" == "latest" ]]; then 90 | echo "Checking the latest version of Terraform" 91 | tfVersion=$(curl -sL https://releases.hashicorp.com/terraform/index.json | jq -r '.versions[].version' | grep -v '[-].*' | sort -rV | head -n 1) 92 | 93 | if [[ -z "${tfVersion}" ]]; then 94 | echo "Failed to fetch the latest version" 95 | exit 1 96 | fi 97 | fi 98 | 99 | url="https://releases.hashicorp.com/terraform/${tfVersion}/terraform_${tfVersion}_linux_amd64.zip" 100 | 101 | echo "Downloading Terraform v${tfVersion}" 102 | curl -s -S -L -o /tmp/terraform_${tfVersion} ${url} 103 | if [ "${?}" -ne 0 ]; then 104 | echo "Failed to download Terraform v${tfVersion}" 105 | exit 1 106 | fi 107 | echo "Successfully downloaded Terraform v${tfVersion}" 108 | 109 | echo "Unzipping Terraform v${tfVersion}" 110 | unzip -d /usr/local/bin /tmp/terraform_${tfVersion} &> /dev/null 111 | if [ "${?}" -ne 0 ]; then 112 | echo "Failed to unzip Terraform v${tfVersion}" 113 | exit 1 114 | fi 115 | echo "Successfully unzipped Terraform v${tfVersion}" 116 | } 117 | 118 | function installTerragrunt { 119 | if [[ "${tgVersion}" == "latest" ]]; then 120 | echo "Checking the latest version of Terragrunt" 121 | latestURL=$(curl -Ls -o /dev/null -w %{url_effective} https://github.com/gruntwork-io/terragrunt/releases/latest) 122 | tgVersion=${latestURL##*/} 123 | 124 | if [[ -z "${tgVersion}" ]]; then 125 | echo "Failed to fetch the latest version" 126 | exit 1 127 | fi 128 | fi 129 | 130 | url="https://github.com/gruntwork-io/terragrunt/releases/download/${tgVersion}/terragrunt_linux_amd64" 131 | 132 | echo "Downloading Terragrunt ${tgVersion}" 133 | curl -s -S -L -o /tmp/terragrunt ${url} 134 | if [ "${?}" -ne 0 ]; then 135 | echo "Failed to download Terragrunt ${tgVersion}" 136 | exit 1 137 | fi 138 | echo "Successfully downloaded Terragrunt ${tgVersion}" 139 | 140 | echo "Moving Terragrunt ${tgVersion} to PATH" 141 | chmod +x /tmp/terragrunt 142 | mv /tmp/terragrunt /usr/local/bin/terragrunt 143 | if [ "${?}" -ne 0 ]; then 144 | echo "Failed to move Terragrunt ${tgVersion}" 145 | exit 1 146 | fi 147 | echo "Successfully moved Terragrunt ${tgVersion}" 148 | } 149 | 150 | function main { 151 | # Source the other files to gain access to their functions 152 | scriptDir=$(dirname ${0}) 153 | source ${scriptDir}/terragrunt_fmt.sh 154 | source ${scriptDir}/terragrunt_init.sh 155 | source ${scriptDir}/terragrunt_validate.sh 156 | source ${scriptDir}/terragrunt_plan.sh 157 | source ${scriptDir}/terragrunt_apply.sh 158 | source ${scriptDir}/terragrunt_output.sh 159 | source ${scriptDir}/terragrunt_import.sh 160 | source ${scriptDir}/terragrunt_taint.sh 161 | source ${scriptDir}/terragrunt_destroy.sh 162 | 163 | parseInputs 164 | configureCLICredentials 165 | installTerraform 166 | cd ${GITHUB_WORKSPACE}/${tfWorkingDir} 167 | 168 | case "${tfSubcommand}" in 169 | fmt) 170 | installTerragrunt 171 | terragruntFmt ${*} 172 | ;; 173 | init) 174 | installTerragrunt 175 | terragruntInit ${*} 176 | ;; 177 | validate) 178 | installTerragrunt 179 | terragruntValidate ${*} 180 | ;; 181 | plan) 182 | installTerragrunt 183 | terragruntPlan ${*} 184 | ;; 185 | apply) 186 | installTerragrunt 187 | terragruntApply ${*} 188 | ;; 189 | output) 190 | installTerragrunt 191 | terragruntOutput ${*} 192 | ;; 193 | import) 194 | installTerragrunt 195 | terragruntImport ${*} 196 | ;; 197 | taint) 198 | installTerragrunt 199 | terragruntTaint ${*} 200 | ;; 201 | destroy) 202 | installTerragrunt 203 | terragruntDestroy ${*} 204 | ;; 205 | *) 206 | echo "Error: Must provide a valid value for terragrunt_subcommand" 207 | exit 1 208 | ;; 209 | esac 210 | } 211 | 212 | main "${*}" 213 | -------------------------------------------------------------------------------- /src/terragrunt_apply.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terragruntApply { 4 | # Gather the output of `terragrunt apply`. 5 | echo "apply: info: applying Terragrunt configuration in ${tfWorkingDir}" 6 | applyOutput=$(${tfBinary} apply -auto-approve -input=false ${*} 2>&1) 7 | applyExitCode=${?} 8 | applyCommentStatus="Failed" 9 | 10 | # Exit code of 0 indicates success. Print the output and exit. 11 | if [ ${applyExitCode} -eq 0 ]; then 12 | echo "apply: info: successfully applied Terragrunt configuration in ${tfWorkingDir}" 13 | echo "${applyOutput}" 14 | echo 15 | applyCommentStatus="Success" 16 | fi 17 | 18 | # Exit code of !0 indicates failure. 19 | if [ ${applyExitCode} -ne 0 ]; then 20 | echo "apply: error: failed to apply Terragrunt configuration in ${tfWorkingDir}" 21 | echo "${applyOutput}" 22 | echo 23 | fi 24 | 25 | # Comment on the pull request if necessary. 26 | if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ]; then 27 | applyCommentWrapper="#### \`${tfBinary} apply\` ${applyCommentStatus} 28 |
Show Output 29 | 30 | \`\`\` 31 | ${applyOutput} 32 | \`\`\` 33 | 34 |
35 | 36 | *Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*" 37 | 38 | applyCommentWrapper=$(stripColors "${applyCommentWrapper}") 39 | echo "apply: info: creating JSON" 40 | applyPayload=$(echo "${applyCommentWrapper}" | jq -R --slurp '{body: .}') 41 | applyCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url) 42 | echo "apply: info: commenting on the pull request" 43 | echo "${applyPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${applyCommentsURL}" > /dev/null 44 | fi 45 | 46 | exit ${applyExitCode} 47 | } 48 | -------------------------------------------------------------------------------- /src/terragrunt_destroy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terragruntDestroy { 4 | # Gather the output of `terragrunt destroy`. 5 | echo "destroy: info: destroying Terragrunt-managed infrastructure in ${tfWorkingDir}" 6 | destroyOutput=$(${tfBinary} destroy -auto-approve -input=false ${*} 2>&1) 7 | destroyExitCode=${?} 8 | destroyCommentStatus="Failed" 9 | 10 | # Exit code of 0 indicates success. Print the output and exit. 11 | if [ ${destroyExitCode} -eq 0 ]; then 12 | echo "destroy: info: successfully destroyed Terragrunt-managed infrastructure in ${tfWorkingDir}" 13 | echo "${destroyOutput}" 14 | echo 15 | destroyCommentStatus="Success" 16 | fi 17 | 18 | # Exit code of !0 indicates failure. 19 | if [ ${destroyExitCode} -ne 0 ]; then 20 | echo "destroy: error: failed to destroy Terragrunt configuration in ${tfWorkingDir}" 21 | echo "${destroyOutput}" 22 | echo 23 | fi 24 | 25 | # Comment on the pull request if necessary. 26 | if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ]; then 27 | destroyCommentWrapper="#### \`${tfBinary} destroy\` ${destroyCommentStatus} 28 |
Show Output 29 | 30 | \`\`\` 31 | ${destroyOutput} 32 | \`\`\` 33 | 34 |
35 | 36 | *Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`*" 37 | 38 | destroyCommentWrapper=$(stripColors "${destroyCommentWrapper}") 39 | echo "destroy: info: creating JSON" 40 | destroyPayload=$(echo "${destroyCommentWrapper}" | jq -R --slurp '{body: .}') 41 | destroyCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url) 42 | echo "destroy: info: commenting on the pull request" 43 | echo "${destroyPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${destroyCommentsURL}" > /dev/null 44 | fi 45 | 46 | exit ${destroyExitCode} 47 | } 48 | -------------------------------------------------------------------------------- /src/terragrunt_fmt.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terragruntFmt { 4 | # Eliminate `-recursive` option for Terragrunt 0.11.x. 5 | fmtRecursive="-recursive" 6 | if hasPrefix "0.11" "${tfVersion}"; then 7 | fmtRecursive="" 8 | fi 9 | 10 | # Gather the output of `terragrunt fmt`. 11 | echo "fmt: info: checking if Terragrunt files in ${tfWorkingDir} are correctly formatted" 12 | fmtOutput=$(${tfBinary} fmt -check=true -write=false -diff ${fmtRecursive} ${*} 2>&1) 13 | fmtExitCode=${?} 14 | 15 | # Exit code of 0 indicates success. Print the output and exit. 16 | if [ ${fmtExitCode} -eq 0 ]; then 17 | echo "fmt: info: Terragrunt files in ${tfWorkingDir} are correctly formatted" 18 | echo "${fmtOutput}" 19 | echo 20 | exit ${fmtExitCode} 21 | fi 22 | 23 | # Exit code of 2 indicates a parse error. Print the output and exit. 24 | if [ ${fmtExitCode} -eq 2 ]; then 25 | echo "fmt: error: failed to parse Terragrunt files" 26 | echo "${fmtOutput}" 27 | echo 28 | exit ${fmtExitCode} 29 | fi 30 | 31 | # Exit code of !0 and !2 indicates failure. 32 | echo "fmt: error: Terragrunt files in ${tfWorkingDir} are incorrectly formatted" 33 | echo "${fmtOutput}" 34 | echo 35 | echo "fmt: error: the following files in ${tfWorkingDir} are incorrectly formatted" 36 | fmtFileList=$(${tfBinary} fmt -check=true -write=false -list ${fmtRecursive}) 37 | echo "${fmtFileList}" 38 | echo 39 | 40 | # Comment on the pull request if necessary. 41 | if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ]; then 42 | fmtComment="" 43 | for file in ${fmtFileList}; do 44 | fmtFileDiff=$(${tfBinary} fmt -check=true -write=false -diff "${file}" | sed -n '/@@.*/,//{/@@.*/d;p}') 45 | fmtComment="${fmtComment} 46 |
${tfWorkingDir}/${file} 47 | 48 | \`\`\`diff 49 | ${fmtFileDiff} 50 | \`\`\` 51 | 52 |
" 53 | 54 | done 55 | 56 | fmtCommentWrapper="#### \`${tfBinary} fmt\` Failed 57 | ${fmtComment} 58 | 59 | *Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*" 60 | 61 | fmtCommentWrapper=$(stripColors "${fmtCommentWrapper}") 62 | echo "fmt: info: creating JSON" 63 | fmtPayload=$(echo "${fmtCommentWrapper}" | jq -R --slurp '{body: .}') 64 | fmtCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url) 65 | echo "fmt: info: commenting on the pull request" 66 | echo "${fmtPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${fmtCommentsURL}" > /dev/null 67 | fi 68 | 69 | # Write changes to branch 70 | echo "::set-output name=tf_actions_fmt_written::false" 71 | if [ "${tfFmtWrite}" == "1" ]; then 72 | echo "fmt: info: Terraform files in ${tfWorkingDir} will be formatted" 73 | terraform fmt -write=true ${fmtRecursive} "${*}" 74 | fmtExitCode=${?} 75 | echo "::set-output name=tf_actions_fmt_written::true" 76 | fi 77 | 78 | exit ${fmtExitCode} 79 | } 80 | -------------------------------------------------------------------------------- /src/terragrunt_import.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terragruntImport { 4 | # Gather the output of `terragrunt import`. 5 | echo "import: info: importing Terragrunt configuration in ${tfWorkingDir}" 6 | importOutput=$(${tfBinary} import -input=false ${*} 2>&1) 7 | importExitCode=${?} 8 | importCommentStatus="Failed" 9 | 10 | # Exit code of 0 indicates success with no changes. Print the output and exit. 11 | if [ ${importExitCode} -eq 0 ]; then 12 | echo "import: info: successfully imported Terragrunt configuration in ${tfWorkingDir}" 13 | echo "${importOutput}" 14 | echo 15 | exit ${importExitCode} 16 | fi 17 | 18 | # Exit code of !0 indicates failure. 19 | if [ ${importExitCode} -ne 0 ]; then 20 | echo "import: error: failed to import Terragrunt configuration in ${tfWorkingDir}" 21 | echo "${importOutput}" 22 | echo 23 | fi 24 | 25 | # Comment on the pull request if necessary. 26 | if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ] && [ "${importCommentStatus}" == "Failed" ]; then 27 | importCommentWrapper="#### \`${tfBinary} import\` ${importCommentStatus} 28 |
Show Output 29 | 30 | \`\`\` 31 | ${importOutput} 32 | \`\`\` 33 | 34 |
35 | 36 | *Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*" 37 | 38 | importCommentWrapper=$(stripColors "${importCommentWrapper}") 39 | echo "import: info: creating JSON" 40 | importPayload=$(echo "${importCommentWrapper}" | jq -R --slurp '{body: .}') 41 | importCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url) 42 | echo "import: info: commenting on the pull request" 43 | echo "${importPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${importCommentsURL}" > /dev/null 44 | fi 45 | 46 | exit ${importExitCode} 47 | } 48 | -------------------------------------------------------------------------------- /src/terragrunt_init.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terragruntInit { 4 | # Gather the output of `terragrunt init`. 5 | echo "init: info: initializing Terragrunt configuration in ${tfWorkingDir}" 6 | initOutput=$(${tfBinary} init -input=false ${*} 2>&1) 7 | initExitCode=${?} 8 | 9 | # Exit code of 0 indicates success. Print the output and exit. 10 | if [ ${initExitCode} -eq 0 ]; then 11 | echo "init: info: successfully initialized Terragrunt configuration in ${tfWorkingDir}" 12 | echo "${initOutput}" 13 | echo 14 | exit ${initExitCode} 15 | fi 16 | 17 | # Exit code of !0 indicates failure. 18 | echo "init: error: failed to initialize Terragrunt configuration in ${tfWorkingDir}" 19 | echo "${initOutput}" 20 | echo 21 | 22 | # Comment on the pull request if necessary. 23 | if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ]; then 24 | initCommentWrapper="#### \`${tfBinary} init\` Failed 25 | 26 | \`\`\` 27 | ${initOutput} 28 | \`\`\` 29 | 30 | *Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*" 31 | 32 | initCommentWrapper=$(stripColors "${initCommentWrapper}") 33 | echo "init: info: creating JSON" 34 | initPayload=$(echo "${initCommentWrapper}" | jq -R --slurp '{body: .}') 35 | initCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url) 36 | echo "init: info: commenting on the pull request" 37 | echo "${initPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${initCommentsURL}" > /dev/null 38 | fi 39 | 40 | exit ${initExitCode} 41 | } 42 | -------------------------------------------------------------------------------- /src/terragrunt_output.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terragruntOutput { 4 | # Gather the output of `terragrunt output`. 5 | echo "output: info: gathering all the outputs for the Terragrunt configuration in ${tfWorkingDir}" 6 | outputOutput=$(${tfBinary} output -json ${*} 2>&1) 7 | outputExitCode=${?} 8 | 9 | # Exit code of 0 indicates success. Print the output and exit. 10 | if [ ${outputExitCode} -eq 0 ]; then 11 | echo "output: info: successfully gathered all the outputs for the Terragrunt configuration in ${tfWorkingDir}" 12 | echo "${outputOutput}" 13 | echo 14 | 15 | # https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/m-p/38372/highlight/true#M3322 16 | outputOutput="${outputOutput//'%'/'%25'}" 17 | outputOutput="${outputOutput//$'\n'/'%0A'}" 18 | outputOutput="${outputOutput//$'\r'/'%0D'}" 19 | 20 | echo "::set-output name=tf_actions_output::${outputOutput}" 21 | exit ${outputExitCode} 22 | fi 23 | 24 | # Exit code of !0 indicates failure. 25 | echo "output: error: failed to gather all the outputs for the Terragrunt configuration in ${tfWorkingDir}" 26 | echo "${outputOutput}" 27 | echo 28 | exit ${outputExitCode} 29 | } 30 | -------------------------------------------------------------------------------- /src/terragrunt_plan.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terragruntPlan { 4 | # Gather the output of `terragrunt plan`. 5 | echo "plan: info: planning Terragrunt configuration in ${tfWorkingDir}" 6 | planOutput=$(${tfBinary} plan -detailed-exitcode -input=false ${*} 2>&1) 7 | planExitCode=${?} 8 | planHasChanges=false 9 | planCommentStatus="Failed" 10 | 11 | # Exit code of 0 indicates success with no changes. Print the output and exit. 12 | if [ ${planExitCode} -eq 0 ]; then 13 | echo "plan: info: successfully planned Terragrunt configuration in ${tfWorkingDir}" 14 | echo "${planOutput}" 15 | echo 16 | echo ::set-output name=tf_actions_plan_has_changes::${planHasChanges} 17 | exit ${planExitCode} 18 | fi 19 | 20 | # Exit code of 2 indicates success with changes. Print the output, change the 21 | # exit code to 0, and mark that the plan has changes. 22 | if [ ${planExitCode} -eq 2 ]; then 23 | planExitCode=0 24 | planHasChanges=true 25 | planCommentStatus="Success" 26 | echo "plan: info: successfully planned Terragrunt configuration in ${tfWorkingDir}" 27 | echo "${planOutput}" 28 | echo 29 | if echo "${planOutput}" | egrep '^-{72}$' &> /dev/null; then 30 | planOutput=$(echo "${planOutput}" | sed -n -r '/-{72}/,/-{72}/{ /-{72}/d; p }') 31 | fi 32 | planOutput=$(echo "${planOutput}" | sed -r -e 's/^ \+/\+/g' | sed -r -e 's/^ ~/~/g' | sed -r -e 's/^ -/-/g') 33 | 34 | # If output is longer than max length (65536 characters), keep last part 35 | planOutput=$(echo "${planOutput}" | tail -c 65000 ) 36 | fi 37 | 38 | # Exit code of !0 indicates failure. 39 | if [ ${planExitCode} -ne 0 ]; then 40 | echo "plan: error: failed to plan Terragrunt configuration in ${tfWorkingDir}" 41 | echo "${planOutput}" 42 | echo 43 | fi 44 | 45 | # Comment on the pull request if necessary. 46 | if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ] && ([ "${planHasChanges}" == "true" ] || [ "${planCommentStatus}" == "Failed" ]); then 47 | planCommentWrapper="#### \`${tfBinary} plan\` ${planCommentStatus} 48 |
Show Output 49 | 50 | \`\`\` 51 | ${planOutput} 52 | \`\`\` 53 | 54 |
55 | 56 | *Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*" 57 | 58 | planCommentWrapper=$(stripColors "${planCommentWrapper}") 59 | echo "plan: info: creating JSON" 60 | planPayload=$(echo "${planCommentWrapper}" | jq -R --slurp '{body: .}') 61 | planCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url) 62 | echo "plan: info: commenting on the pull request" 63 | echo "${planPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${planCommentsURL}" > /dev/null 64 | fi 65 | 66 | echo ::set-output name=tf_actions_plan_has_changes::${planHasChanges} 67 | 68 | # https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/m-p/38372/highlight/true#M3322 69 | planOutput="${planOutput//'%'/'%25'}" 70 | planOutput="${planOutput//$'\n'/'%0A'}" 71 | planOutput="${planOutput//$'\r'/'%0D'}" 72 | 73 | echo "::set-output name=tf_actions_plan_output::${planOutput}" 74 | exit ${planExitCode} 75 | } 76 | -------------------------------------------------------------------------------- /src/terragrunt_taint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terragruntTaint { 4 | # Gather the output of `terragrunt taint`. 5 | echo "taint: info: tainting terragrunt configuration in ${tfWorkingDir}" 6 | #taintOutput=$(terragrunt taint ${*} 2>&1) 7 | taintOutput=$(for resource in ${*}; do ${tfBinary} taint -allow-missing $resource; done 2>&1) 8 | taintExitCode=${?} 9 | taintCommentStatus="Failed" 10 | 11 | # Exit code of 0 indicates success with no changes. Print the output and exit. 12 | if [ ${taintExitCode} -eq 0 ]; then 13 | taintCommentStatus="Success" 14 | echo "taint: info: successfully tainted Terragrunt configuration in ${tfWorkingDir}" 15 | echo "${taintOutput}" 16 | echo 17 | exit ${taintExitCode} 18 | fi 19 | 20 | # Exit code of !0 indicates failure. 21 | if [ ${taintExitCode} -ne 0 ]; then 22 | echo "taint: error: failed to taint Terragrunt configuration in ${tfWorkingDir}" 23 | echo "${taintOutput}" 24 | echo 25 | fi 26 | 27 | # Comment on the pull request if necessary. 28 | if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ]; then 29 | taintCommentWrapper="#### \`${tfBinary} taint\` ${taintCommentStatus} 30 |
Show Output 31 | 32 | \`\`\` 33 | ${taintOutput} 34 | \`\`\` 35 | 36 |
37 | 38 | *Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*" 39 | 40 | taintCommentWrapper=$(stripColors "${taintCommentWrapper}") 41 | echo "taint: info: creating JSON" 42 | taintPayload=$(echo "${taintCommentWrapper}" | jq -R --slurp '{body: .}') 43 | taintCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url) 44 | echo "taint: info: commenting on the pull request" 45 | echo "${taintPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${taintCommentsURL}" > /dev/null 46 | fi 47 | 48 | exit ${taintExitCode} 49 | } 50 | -------------------------------------------------------------------------------- /src/terragrunt_validate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terragruntValidate { 4 | # Gather the output of `terragrunt validate`. 5 | echo "validate: info: validating Terragrunt configuration in ${tfWorkingDir}" 6 | validateOutput=$(${tfBinary} validate ${*} 2>&1) 7 | validateExitCode=${?} 8 | 9 | # Exit code of 0 indicates success. Print the output and exit. 10 | if [ ${validateExitCode} -eq 0 ]; then 11 | echo "validate: info: successfully validated Terragrunt configuration in ${tfWorkingDir}" 12 | echo "${validateOutput}" 13 | echo 14 | exit ${validateExitCode} 15 | fi 16 | 17 | # Exit code of !0 indicates failure. 18 | echo "validate: error: failed to validate Terragrunt configuration in ${tfWorkingDir}" 19 | echo "${validateOutput}" 20 | echo 21 | 22 | # Comment on the pull request if necessary. 23 | if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ]; then 24 | validateCommentWrapper="#### \`${tfBinary} validate\` Failed 25 | 26 | \`\`\` 27 | ${validateOutput} 28 | \`\`\` 29 | 30 | *Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*" 31 | 32 | validateCommentWrapper=$(stripColors "${validateCommentWrapper}") 33 | echo "validate: info: creating JSON" 34 | validatePayload=$(echo "${validateCommentWrapper}" | jq -R --slurp '{body: .}') 35 | validateCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url) 36 | echo "validate: info: commenting on the pull request" 37 | echo "${validatePayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${validateCommentsURL}" > /dev/null 38 | fi 39 | 40 | exit ${validateExitCode} 41 | } 42 | --------------------------------------------------------------------------------