├── 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 ├── terraform_apply.sh ├── terraform_destroy.sh ├── terraform_fmt.sh ├── terraform_import.sh ├── terraform_init.sh ├── terraform_output.sh ├── terraform_plan.sh ├── terraform_taint.sh └── terraform_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 | # hashicorp/terraform-github-actions 2 | 3 | This `hashicorp/terraform-github-actions` repository is no longer actively developed or maintained. It has been superseded by the [hashicorp/setup-terraform](https://github.com/hashicorp/setup-terraform) GitHub action. 4 | 5 | Thank you to all of the users and contributors that made this repository possible. Hope to see you using the new `hashicorp/setup-terraform` GitHub action! 6 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'Terraform GitHub Actions' 2 | description: 'Runs Terraform commands via GitHub Actions.' 3 | author: 'HashiCorp, Inc. Terraform Team ' 4 | branding: 5 | icon: 'terminal' 6 | color: 'purple' 7 | inputs: 8 | tf_actions_subcommand: 9 | description: 'Terraform subcommand to execute.' 10 | required: true 11 | tf_actions_version: 12 | description: 'Terraform version to install.' 13 | required: true 14 | tf_actions_cli_credentials_hostname: 15 | description: 'Hostname for the CLI credentials file.' 16 | default: 'app.terraform.io' 17 | tf_actions_cli_credentials_token: 18 | description: 'Token for the CLI credentials file.' 19 | tf_actions_comment: 20 | description: 'Whether or not to comment on pull requests.' 21 | default: true 22 | tf_actions_working_dir: 23 | description: 'Terraform working directory.' 24 | default: '.' 25 | tf_actions_fmt_write: 26 | description: 'Write Terraform fmt changes to source files.' 27 | default: false 28 | outputs: 29 | tf_actions_output: 30 | description: 'The Terraform outputs in JSON format.' 31 | tf_actions_plan_has_changes: 32 | description: 'Whether or not the Terraform plan contained changes.' 33 | tf_actions_plan_output: 34 | description: 'The Terraform plan output.' 35 | tf_actions_fmt_written: 36 | description: 'Whether or not the Terraform formatting was written to source files.' 37 | runs: 38 | using: 'docker' 39 | image: './Dockerfile' 40 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | # Examples 2 | 3 | Here are some examples showing common workflows using Terraform 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 Terraform subcommand. Using this `args` attribute will place the arguments at the end of the entire `terraform` 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 `terraform init` command. 8 | 9 | ```yaml 10 | name: 'Terraform GitHub Actions' 11 | on: 12 | - pull_request 13 | jobs: 14 | terraform: 15 | name: 'Terraform' 16 | runs-on: ubuntu-latest 17 | steps: 18 | - name: 'Checkout' 19 | uses: actions/checkout@master 20 | - name: 'Terraform Init' 21 | uses: hashicorp/terraform-github-actions@master 22 | with: 23 | tf_actions_version: 0.12.13 24 | tf_actions_subcommand: 'init' 25 | tf_actions_working_dir: '.' 26 | tf_actions_comment: true 27 | args: '-var="env=dev"' 28 | env: 29 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 30 | ``` 31 | 32 | 33 | ## Using Environment Variables 34 | 35 | Terraform 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 `terraform init` command. 36 | 37 | ```yaml 38 | name: 'Terraform GitHub Actions' 39 | on: 40 | - pull_request 41 | jobs: 42 | terraform: 43 | name: 'Terraform' 44 | runs-on: ubuntu-latest 45 | steps: 46 | - name: 'Checkout' 47 | uses: actions/checkout@master 48 | - name: 'Terraform Init' 49 | uses: hashicorp/terraform-github-actions@master 50 | with: 51 | tf_actions_version: 0.12.13 52 | tf_actions_subcommand: 'init' 53 | tf_actions_working_dir: '.' 54 | tf_actions_comment: true 55 | env: 56 | TF_CLI_ARGS_init: '-var="env=dev"' 57 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 58 | ``` 59 | -------------------------------------------------------------------------------- /examples/backends.md: -------------------------------------------------------------------------------- 1 | # Terraform Backends 2 | 3 | Terraform 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: 'Terraform GitHub Actions' 9 | on: 10 | - pull_request 11 | jobs: 12 | terraform: 13 | name: 'Terraform' 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: 'Checkout' 17 | uses: actions/checkout@master 18 | - name: 'Terraform Init' 19 | uses: hashicorp/terraform-github-actions@master 20 | with: 21 | tf_actions_version: 0.12.13 22 | tf_actions_subcommand: 'init' 23 | tf_actions_working_dir: '.' 24 | tf_actions_comment: true 25 | args: '-backend-config="token=${{ secrets.TF_API_TOKEN }}" -backend-config="organization=CHANGE_ME"' 26 | env: 27 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 28 | ``` 29 | -------------------------------------------------------------------------------- /examples/credentials-file.md: -------------------------------------------------------------------------------- 1 | # Terraform 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: 'Terraform GitHub Actions' 9 | on: 10 | - pull_request 11 | jobs: 12 | terraform: 13 | name: 'Terraform' 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: 'Checkout' 17 | uses: actions/checkout@master 18 | - name: 'Terraform Init' 19 | uses: hashicorp/terraform-github-actions@master 20 | with: 21 | tf_actions_version: 0.12.13 22 | tf_actions_subcommand: 'init' 23 | tf_actions_working_dir: '.' 24 | tf_actions_comment: true 25 | tf_actions_cli_credentials_hostname: app.terraform.io 26 | tf_actions_cli_credentials_token: ${{ secrets.TF_API_TOKEN }} 27 | env: 28 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 29 | - name: 'Terraform Plan' 30 | uses: hashicorp/terraform-github-actions@master 31 | with: 32 | tf_actions_version: 0.12.13 33 | tf_actions_subcommand: 'plan' 34 | tf_actions_working_dir: '.' 35 | ``` 36 | -------------------------------------------------------------------------------- /examples/output.md: -------------------------------------------------------------------------------- 1 | # Terraform 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: 'Terraform GitHub Actions' 7 | on: 8 | - push 9 | jobs: 10 | terraform: 11 | name: 'Terraform' 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: 'Checkout' 15 | uses: actions/checkout@master 16 | - name: 'Terraform Output' 17 | id: terraform 18 | uses: hashicorp/terraform-github-actions@master 19 | with: 20 | tf_actions_version: 0.12.13 21 | tf_actions_subcommand: 'output' 22 | tf_actions_working_dir: '.' 23 | - name: 'Use Terraform Output' 24 | run: echo ${{ steps.terraform.outputs.tf_actions_output }} 25 | - name: 'Pull specific database name from outputs' 26 | run: | 27 | apt-get install jq 28 | DBNAME=$(echo ${{ steps.terraform.outputs.tf_actions_output }} | jq -r '.database.value.name') 29 | echo $DBNAME 30 | ``` 31 | 32 | In this example the `tf_actions_output` would contain the following content. 33 | 34 | ```json 35 | { 36 | "database": { 37 | "value": { 38 | "name": "test-database" 39 | } 40 | } 41 | } 42 | ``` 43 | -------------------------------------------------------------------------------- /examples/tainting.md: -------------------------------------------------------------------------------- 1 | # Terraform Tainting 2 | 3 | Resources to taint can be specified using the `args` with option. 4 | 5 | ```yaml 6 | name: 'Terraform GitHub Actions' 7 | on: 8 | - pull_request 9 | jobs: 10 | terraform: 11 | name: 'Terraform' 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: 'Checkout' 15 | uses: actions/checkout@master 16 | - name: 'Terraform Init' 17 | uses: hashicorp/terraform-github-actions@master 18 | with: 19 | tf_actions_version: 0.12.13 20 | tf_actions_subcommand: 'init' 21 | tf_actions_working_dir: '.' 22 | tf_actions_comment: true 23 | env: 24 | TF_WORKSPACE: dev 25 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 26 | - name: 'Terraform Taint' 27 | uses: hashicorp/terraform-github-actions@master 28 | with: 29 | tf_actions_version: 0.12.13 30 | tf_actions_subcommand: 'taint' 31 | tf_actions_working_dir: '.' 32 | tf_actions_comment: true 33 | args: 'aws_instance.host' 34 | env: 35 | TF_WORKSPACE: dev 36 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 37 | ``` 38 | 39 | Multiple resources can be specified by separating with spaces: `args: 'aws_instance.host1 aws_instance.host2'`. 40 | -------------------------------------------------------------------------------- /examples/terraform-versions.md: -------------------------------------------------------------------------------- 1 | # Terraform Versions 2 | 3 | Specify the version of Terraform to be executed using the `tf_actions_version` input. Here, Terraform 0.12.13 is being used. 4 | 5 | ```yaml 6 | name: 'Terraform GitHub Actions' 7 | on: 8 | - pull_request 9 | jobs: 10 | terraform: 11 | name: 'Terraform' 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: 'Checkout' 15 | uses: actions/checkout@master 16 | - name: 'Terraform Init' 17 | uses: hashicorp/terraform-github-actions@master 18 | with: 19 | tf_actions_version: 0.12.13 20 | tf_actions_subcommand: 'init' 21 | tf_actions_working_dir: '.' 22 | tf_actions_comment: true 23 | env: 24 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 25 | ``` 26 | -------------------------------------------------------------------------------- /examples/variables.md: -------------------------------------------------------------------------------- 1 | # Terraform 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: 'Terraform GitHub Actions' 11 | on: 12 | - pull_request 13 | jobs: 14 | terraform: 15 | name: 'Terraform' 16 | runs-on: ubuntu-latest 17 | steps: 18 | - name: 'Checkout' 19 | uses: actions/checkout@master 20 | - name: 'Terraform Init' 21 | uses: hashicorp/terraform-github-actions@master 22 | with: 23 | tf_actions_version: 0.12.13 24 | tf_actions_subcommand: 'init' 25 | tf_actions_working_dir: '.' 26 | tf_actions_comment: true 27 | args: '-var="env=dev"' 28 | env: 29 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 30 | ``` 31 | 32 | This example shows how to use a variable file using the `-var-file` argument. 33 | 34 | ```yaml 35 | name: 'Terraform GitHub Actions' 36 | on: 37 | - pull_request 38 | jobs: 39 | terraform: 40 | name: 'Terraform' 41 | runs-on: ubuntu-latest 42 | steps: 43 | - name: 'Checkout' 44 | uses: actions/checkout@master 45 | - name: 'Terraform Init' 46 | uses: hashicorp/terraform-github-actions@master 47 | with: 48 | tf_actions_version: 0.12.13 49 | tf_actions_subcommand: 'init' 50 | tf_actions_working_dir: '.' 51 | tf_actions_comment: true 52 | args: '-var-file="dev.tfvars"' 53 | env: 54 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 55 | ``` 56 | 57 | 58 | ## Using an Environment Variable 59 | 60 | 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. 61 | 62 | Here, the Terraform variable `env` is set to the value `dev`. 63 | 64 | ```yaml 65 | name: 'Terraform GitHub Actions' 66 | on: 67 | - pull_request 68 | jobs: 69 | terraform: 70 | name: 'Terraform' 71 | runs-on: ubuntu-latest 72 | steps: 73 | - name: 'Checkout' 74 | uses: actions/checkout@master 75 | - name: 'Terraform Init' 76 | uses: hashicorp/terraform-github-actions@master 77 | with: 78 | tf_actions_version: 0.12.13 79 | tf_actions_subcommand: 'init' 80 | tf_actions_working_dir: '.' 81 | tf_actions_comment: true 82 | env: 83 | TF_VAR_env: 'dev' 84 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 85 | ``` 86 | -------------------------------------------------------------------------------- /examples/working-directory.md: -------------------------------------------------------------------------------- 1 | # Working Directory 2 | 3 | Terraform 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: 'Terraform GitHub Actions' 7 | on: 8 | - pull_request 9 | jobs: 10 | root: 11 | name: 'Terraform (root)' 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: 'Checkout' 15 | uses: actions/checkout@master 16 | - name: 'Terraform Init' 17 | uses: hashicorp/terraform-github-actions@master 18 | with: 19 | tf_actions_version: 0.12.13 20 | tf_actions_subcommand: 'init' 21 | tf_actions_working_dir: '.' 22 | tf_actions_comment: true 23 | env: 24 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 25 | ``` 26 | 27 | If you need to run the Terraform Actions in multiple directories, you have to create separate jobs for each working directory. 28 | 29 | ```yaml 30 | name: 'Terraform GitHub Actions' 31 | on: 32 | - pull_request 33 | jobs: 34 | directory1: 35 | name: 'Terraform (directory1)' 36 | runs-on: ubuntu-latest 37 | steps: 38 | - name: 'Checkout' 39 | uses: actions/checkout@master 40 | - name: 'Terraform Init' 41 | uses: hashicorp/terraform-github-actions@master 42 | with: 43 | tf_actions_version: 0.12.13 44 | tf_actions_subcommand: 'init' 45 | tf_actions_working_dir: 'directory1' 46 | tf_actions_comment: true 47 | env: 48 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 49 | directory2: 50 | name: 'Terraform (directory2)' 51 | runs-on: ubuntu-latest 52 | steps: 53 | - name: 'Checkout' 54 | uses: actions/checkout@master 55 | - name: 'Terraform Init' 56 | uses: hashicorp/terraform-github-actions@master 57 | with: 58 | tf_actions_version: 0.12.13 59 | tf_actions_subcommand: 'init' 60 | tf_actions_working_dir: 'directory2' 61 | tf_actions_comment: true 62 | env: 63 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 64 | ``` 65 | -------------------------------------------------------------------------------- /examples/workspaces.md: -------------------------------------------------------------------------------- 1 | # Terraform Workspaces 2 | 3 | The workspace can be specified using the `TF_WORKSPACE` environment variable. 4 | 5 | ```yaml 6 | name: 'Terraform GitHub Actions' 7 | on: 8 | - pull_request 9 | jobs: 10 | terraform: 11 | name: 'Terraform' 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: 'Checkout' 15 | uses: actions/checkout@master 16 | - name: 'Terraform Init' 17 | uses: hashicorp/terraform-github-actions@master 18 | with: 19 | tf_actions_version: 0.12.13 20 | tf_actions_subcommand: 'init' 21 | tf_actions_working_dir: '.' 22 | tf_actions_comment: true 23 | env: 24 | TF_WORKSPACE: dev 25 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 26 | ``` 27 | 28 | 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. 29 | -------------------------------------------------------------------------------- /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_TF_ACTIONS_SUBCOMMAND}" != "" ]; then 28 | tfSubcommand=${INPUT_TF_ACTIONS_SUBCOMMAND} 29 | else 30 | echo "Input terraform_subcommand cannot be empty" 31 | exit 1 32 | fi 33 | 34 | # Optional inputs 35 | tfWorkingDir="." 36 | if [[ -n "${INPUT_TF_ACTIONS_WORKING_DIR}" ]]; then 37 | tfWorkingDir=${INPUT_TF_ACTIONS_WORKING_DIR} 38 | fi 39 | 40 | tfComment=0 41 | if [ "${INPUT_TF_ACTIONS_COMMENT}" == "1" ] || [ "${INPUT_TF_ACTIONS_COMMENT}" == "true" ]; then 42 | tfComment=1 43 | fi 44 | 45 | tfCLICredentialsHostname="" 46 | if [ "${INPUT_TF_ACTIONS_CLI_CREDENTIALS_HOSTNAME}" != "" ]; then 47 | tfCLICredentialsHostname=${INPUT_TF_ACTIONS_CLI_CREDENTIALS_HOSTNAME} 48 | fi 49 | 50 | tfCLICredentialsToken="" 51 | if [ "${INPUT_TF_ACTIONS_CLI_CREDENTIALS_TOKEN}" != "" ]; then 52 | tfCLICredentialsToken=${INPUT_TF_ACTIONS_CLI_CREDENTIALS_TOKEN} 53 | fi 54 | 55 | tfFmtWrite=0 56 | if [ "${INPUT_TF_ACTIONS_FMT_WRITE}" == "1" ] || [ "${INPUT_TF_ACTIONS_FMT_WRITE}" == "true" ]; then 57 | tfFmtWrite=1 58 | fi 59 | 60 | tfWorkspace="default" 61 | if [ -n "${TF_WORKSPACE}" ]; then 62 | tfWorkspace="${TF_WORKSPACE}" 63 | fi 64 | } 65 | 66 | function configureCLICredentials { 67 | if [[ ! -f "${HOME}/.terraformrc" ]] && [[ "${tfCLICredentialsToken}" != "" ]]; then 68 | cat > ${HOME}/.terraformrc << EOF 69 | credentials "${tfCLICredentialsHostname}" { 70 | token = "${tfCLICredentialsToken}" 71 | } 72 | EOF 73 | fi 74 | } 75 | 76 | function installTerraform { 77 | if [[ "${tfVersion}" == "latest" ]]; then 78 | echo "Checking the latest version of Terraform" 79 | tfVersion=$(curl -sL https://releases.hashicorp.com/terraform/index.json | jq -r '.versions[].version' | grep -v '[-].*' | sort -rV | head -n 1) 80 | 81 | if [[ -z "${tfVersion}" ]]; then 82 | echo "Failed to fetch the latest version" 83 | exit 1 84 | fi 85 | fi 86 | 87 | url="https://releases.hashicorp.com/terraform/${tfVersion}/terraform_${tfVersion}_linux_amd64.zip" 88 | 89 | echo "Downloading Terraform v${tfVersion}" 90 | curl -s -S -L -o /tmp/terraform_${tfVersion} ${url} 91 | if [ "${?}" -ne 0 ]; then 92 | echo "Failed to download Terraform v${tfVersion}" 93 | exit 1 94 | fi 95 | echo "Successfully downloaded Terraform v${tfVersion}" 96 | 97 | echo "Unzipping Terraform v${tfVersion}" 98 | unzip -d /usr/local/bin /tmp/terraform_${tfVersion} &> /dev/null 99 | if [ "${?}" -ne 0 ]; then 100 | echo "Failed to unzip Terraform v${tfVersion}" 101 | exit 1 102 | fi 103 | echo "Successfully unzipped Terraform v${tfVersion}" 104 | } 105 | 106 | function main { 107 | # Source the other files to gain access to their functions 108 | scriptDir=$(dirname ${0}) 109 | source ${scriptDir}/terraform_fmt.sh 110 | source ${scriptDir}/terraform_init.sh 111 | source ${scriptDir}/terraform_validate.sh 112 | source ${scriptDir}/terraform_plan.sh 113 | source ${scriptDir}/terraform_apply.sh 114 | source ${scriptDir}/terraform_output.sh 115 | source ${scriptDir}/terraform_import.sh 116 | source ${scriptDir}/terraform_taint.sh 117 | source ${scriptDir}/terraform_destroy.sh 118 | 119 | parseInputs 120 | configureCLICredentials 121 | cd ${GITHUB_WORKSPACE}/${tfWorkingDir} 122 | 123 | case "${tfSubcommand}" in 124 | fmt) 125 | installTerraform 126 | terraformFmt ${*} 127 | ;; 128 | init) 129 | installTerraform 130 | terraformInit ${*} 131 | ;; 132 | validate) 133 | installTerraform 134 | terraformValidate ${*} 135 | ;; 136 | plan) 137 | installTerraform 138 | terraformPlan ${*} 139 | ;; 140 | apply) 141 | installTerraform 142 | terraformApply ${*} 143 | ;; 144 | output) 145 | installTerraform 146 | terraformOutput ${*} 147 | ;; 148 | import) 149 | installTerraform 150 | terraformImport ${*} 151 | ;; 152 | taint) 153 | installTerraform 154 | terraformTaint ${*} 155 | ;; 156 | destroy) 157 | installTerraform 158 | terraformDestroy ${*} 159 | ;; 160 | *) 161 | echo "Error: Must provide a valid value for terraform_subcommand" 162 | exit 1 163 | ;; 164 | esac 165 | } 166 | 167 | main "${*}" 168 | -------------------------------------------------------------------------------- /src/terraform_apply.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terraformApply { 4 | # Gather the output of `terraform apply`. 5 | echo "apply: info: applying Terraform configuration in ${tfWorkingDir}" 6 | applyOutput=$(terraform 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 Terraform 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 Terraform 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="#### \`terraform 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/terraform_destroy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terraformDestroy { 4 | # Gather the output of `terraform destroy`. 5 | echo "destroy: info: destroying Terraform-managed infrastructure in ${tfWorkingDir}" 6 | destroyOutput=$(terraform 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 Terraform-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 Terraform 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="#### \`terraform 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/terraform_fmt.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terraformFmt { 4 | # Eliminate `-recursive` option for Terraform 0.11.x. 5 | fmtRecursive="-recursive" 6 | if hasPrefix "0.11" "${tfVersion}"; then 7 | fmtRecursive="" 8 | fi 9 | 10 | # Gather the output of `terraform fmt`. 11 | echo "fmt: info: checking if Terraform files in ${tfWorkingDir} are correctly formatted" 12 | fmtOutput=$(terraform 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: Terraform 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 Terraform 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: Terraform 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=$(terraform 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=$(terraform 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="#### \`terraform 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/terraform_import.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terraformImport { 4 | # Gather the output of `terraform import`. 5 | echo "import: info: importing Terraform configuration in ${tfWorkingDir}" 6 | importOutput=$(terraform 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 Terraform 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 Terraform 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="#### \`terraform 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/terraform_init.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terraformInit { 4 | # Gather the output of `terraform init`. 5 | echo "init: info: initializing Terraform configuration in ${tfWorkingDir}" 6 | initOutput=$(terraform 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 Terraform 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 Terraform 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="#### \`terraform 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/terraform_output.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terraformOutput { 4 | # Gather the output of `terraform output`. 5 | echo "output: info: gathering all the outputs for the Terraform configuration in ${tfWorkingDir}" 6 | outputOutput=$(terraform 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 Terraform 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 Terraform configuration in ${tfWorkingDir}" 26 | echo "${outputOutput}" 27 | echo 28 | exit ${outputExitCode} 29 | } 30 | -------------------------------------------------------------------------------- /src/terraform_plan.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terraformPlan { 4 | # Gather the output of `terraform plan`. 5 | echo "plan: info: planning Terraform configuration in ${tfWorkingDir}" 6 | planOutput=$(terraform 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 Terraform 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 Terraform 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 Terraform 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="#### \`terraform 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/terraform_taint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terraformTaint { 4 | # Gather the output of `terraform taint`. 5 | echo "taint: info: tainting Terraform configuration in ${tfWorkingDir}" 6 | #taintOutput=$(terraform taint ${*} 2>&1) 7 | taintOutput=$(for resource in ${*}; do terraform 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 Terraform 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 Terraform 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="#### \`terraform 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/terraform_validate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function terraformValidate { 4 | # Gather the output of `terraform validate`. 5 | echo "validate: info: validating Terraform configuration in ${tfWorkingDir}" 6 | validateOutput=$(terraform 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 Terraform 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 Terraform 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="#### \`terraform 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 | --------------------------------------------------------------------------------