From 75e5571afbe59466b0a8c79ff4ba78adffb8ddf0 Mon Sep 17 00:00:00 2001 From: Igor Rodionov Date: Wed, 13 Dec 2023 18:49:40 +0300 Subject: [PATCH] Move to GitOps Config File (#29) ## What * Use configuration file ## Why * Make DRY workflows --- .github/workflows/integration-tests.yml | 53 ++-- README.md | 243 ++++++++---------- README.yaml | 79 +++++- action.yml | 119 +++++---- docs/github-action.md | 13 +- atmos.yaml => tests/atmos.yaml | 2 +- .../components}/terraform/foobar/context.tf | 0 .../components}/terraform/foobar/main.tf | 1 + .../components}/terraform/foobar/variables.tf | 0 {stacks => tests/stacks}/catalog/foobar.yaml | 0 .../foo/plat/sandbox/plat-ue2-sandbox.yaml | 0 11 files changed, 282 insertions(+), 228 deletions(-) rename atmos.yaml => tests/atmos.yaml (99%) rename {components => tests/components}/terraform/foobar/context.tf (100%) rename {components => tests/components}/terraform/foobar/main.tf (83%) rename {components => tests/components}/terraform/foobar/variables.tf (100%) rename {stacks => tests/stacks}/catalog/foobar.yaml (100%) rename {stacks => tests/stacks}/orgs/foo/plat/sandbox/plat-ue2-sandbox.yaml (100%) diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index d743cc9..aea51d4 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -18,19 +18,28 @@ jobs: setup: runs-on: ubuntu-latest steps: - - name: Plan Atmos Component - uses: cloudposse/github-action-atmos-terraform-plan@main - with: - component: "foobar" - stack: "plat-ue2-sandbox" - component-path: "components/terraform/foobar" - terraform-plan-role: ${{ secrets.TERRAFORM_APPLY_ROLE }} - terraform-state-role: ${{ secrets.TERRAFORM_STATE_ROLE }} + - shell: bash + run: | + mkdir -p ${{ runner.temp }} + cat > ${{ runner.temp }}/atmos-gitops.yaml < ${{ runner.temp }}/atmos-gitops.yaml <][share_email] -[][share_googleplus] -[][share_facebook] -[][share_reddit] -[][share_linkedin] -[][share_twitter] - - It's 100% Open Source and licensed under the [APACHE2](LICENSE). @@ -66,8 +58,6 @@ Before running this action, first create and store a planfile with the companion - - ## Usage @@ -80,8 +70,28 @@ Then the action will run `terraform apply` against that component with another r to allow GitHub to assume roles in AWS and then deploying both a Terraform Apply role and a Terraform State role. For Cloud Posse documentation on setting up GitHub OIDC, see our [`github-oidc-provider` component](https://docs.cloudposse.com/components/library/aws/github-oidc-provider/). -In order to retrieve Terraform State, we configure an S3 Bucket to store plan files and a DynamoDB table to track plan metadata. Both will need to be deployed before running -this action. For more on setting up those components, see the `gitops` component (__documentation pending__). This action will then use the [github-action-terraform-plan-storage](https://github.com/cloudposse/github-action-terraform-plan-storage) action to update these resources. +In order to retrieve Terraform Plan Files (not to be confused with Terraform State files, e.g. `tfstate`), we configure an S3 Bucket to store plan files and a DynamoDB table to track plan metadata. Both need to be deployed before running +this action. For more on setting up those components, see the [`gitops` component](https://docs.cloudposse.com/components/library/aws/gitops/). This action will then use the [github-action-terraform-plan-storage](https://github.com/cloudposse/github-action-terraform-plan-storage) action to update these resources. + +### Config + +The action expects the atmos gitops configuration file to be present in the repository in `./.github/config/atmos-gitops.yaml`. +The config should have the following structure: + +```yaml + atmos-version: 1.45.3 + atmos-config-path: ./rootfs/usr/local/etc/atmos/ + terraform-state-bucket: cptest-core-ue2-auto-gitops + terraform-state-table: cptest-core-ue2-auto-gitops + terraform-state-role: arn:aws:iam::xxxxxxxxxxxx:role/cptest-core-ue2-auto-gitops-gha + terraform-plan-role: arn:aws:iam::yyyyyyyyyyyy:role/cptest-core-gbl-identity-gitops + terraform-apply-role: arn:aws:iam::yyyyyyyyyyyy:role/cptest-core-gbl-identity-gitops + terraform-version: 1.5.2 + aws-region: us-east-2 + enable-infracost: false + sort-by: .stack_slug + group-by: .stack_slug | split("-") | [.[0], .[2]] | join("-") +``` ### Workflow example @@ -105,18 +115,56 @@ this action. For more on setting up those components, see the `gitops` component plan: runs-on: ubuntu-latest steps: - - name: Plan Atmos Component - uses: cloudposse/github-action-atmos-terraform-apply@v1 + - name: Terraform Apply + uses: cloudposse/github-action-atmos-terraform-apply@v2 with: component: "foobar" stack: "plat-ue2-sandbox" - component-path: "components/terraform/s3-bucket" - terraform-apply-role: "arn:aws:iam::111111111111:role/acme-core-gbl-identity-gitops" - terraform-state-bucket: "acme-core-ue2-auto-gitops" - terraform-state-role: "arn:aws:iam::999999999999:role/acme-core-ue2-auto-gitops-gha" - terraform-state-table: "acme-core-ue2-auto-gitops" - aws-region: "us-east-2" +``` + +### Migrating from `v1` to `v2` + +1. `v2` drops the `component-path` variable and instead fetches if directly from the [`atmos.yaml` file](https://atmos.tools/cli/configuration/) automatically. Simply remove the `component-path` argument from your invocations of the `cloudposse/github-action-atmos-terraform-apply` action. +2. `v2` moves most of the `inputs` to the Atmos GitOps config path `./.github/config/atmos-gitops.yaml`. Simply create this file, transfer your settings to it, then remove the corresponding arguments from your invocations of the `cloudposse/github-action-atmos-terraform-apply` action. +| name | +|--------------------------| +| `atmos-version` | +| `atmos-config-path` | +| `terraform-state-bucket` | +| `terraform-state-table` | +| `terraform-state-role` | +| `terraform-plan-role` | +| `terraform-apply-role` | +| `terraform-version` | +| `aws-region` | +| `enable-infracost` | + + +If you want the same behavior in `v2` as in `v1` you should create config `./.github/config/atmos-gitops.yaml` with the same variables as in `v1` inputs. +```yaml + - name: Terraform apply + uses: cloudposse/github-action-atmos-terraform-apply@v2 + with: + atmos-gitops-config-path: ./.github/config/atmos-gitops.yaml + component: "foobar" + stack: "plat-ue2-sandbox" +``` + +Which would produce the same behavior as in `v1`, doing this: + +```yaml + - name: Terraform apply + uses: cloudposse/github-action-atmos-terraform-apply@v1 + with: + component: "foobar" + stack: "plat-ue2-sandbox" + component-path: "components/terraform/s3-bucket" + terraform-apply-role: "arn:aws:iam::111111111111:role/acme-core-gbl-identity-gitops" + terraform-state-bucket: "acme-core-ue2-auto-gitops" + terraform-state-role: "arn:aws:iam::999999999999:role/acme-core-ue2-auto-gitops-gha" + terraform-state-table: "acme-core-ue2-auto-gitops" + aws-region: "us-east-2" ``` @@ -130,23 +178,14 @@ this action. For more on setting up those components, see the `gitops` component | Name | Description | Default | Required | |------|-------------|---------|----------| -| atmos-config-path | The path to the atmos.yaml file | atmos.yaml | false | -| atmos-version | Atmos version to use for vendoring. Default 'latest' | latest | false | -| aws-region | AWS region for assuming identity. | us-east-1 | false | +| atmos-gitops-config-path | The path to the atmos-gitops.yaml file | ./.github/config/atmos-gitops.yaml | false | | branding-logo-image | Branding logo image url | https://cloudposse.com/logo-300x69.svg | false | | branding-logo-url | Branding logo url | https://cloudposse.com/ | false | -| commit-sha | Commit SHA to apply. Default: github.sha | ${{ github.sha }} | true | | component | The name of the component to apply. | N/A | true | -| component-path | The path to the base component. Atmos defines this value as component\_path. | N/A | true | | debug | Enable action debug mode. Default: 'false' | false | false | -| enable-infracost | Whether to enable infracost summary. Requires secret `infracost-api-key` to be specified. Default: 'false | false | false | | infracost-api-key | Infracost API key | N/A | false | +| sha | Commit SHA to apply. Default: github.sha | ${{ github.event.pull\_request.head.sha }} | true | | stack | The stack name for the given component. | N/A | true | -| terraform-apply-role | The AWS role to be used to apply Terraform. | N/A | true | -| terraform-state-bucket | The S3 Bucket where the planfiles are stored. | N/A | true | -| terraform-state-role | The AWS role to be used to retrieve the planfile from AWS. | N/A | true | -| terraform-state-table | The DynamoDB table where planfile metadata is stored. | N/A | true | -| terraform-version | The version of Terraform CLI to install. Instead of full version string you can also specify constraint string starting with "<" (for example `<1.13.0`) to install the latest version satisfying the constraint. A value of `latest` will install the latest version of Terraform CLI. Defaults to `latest`. | latest | false | | token | Used to pull node distributions for Atmos from Cloud Posse's GitHub repository. Since there's a default, this is typically not supplied by the user. When running this action on github.com, the default value is sufficient. When running on GHES, you can pass a personal access token for github.com if you are experiencing rate limiting. | ${{ github.server\_url == 'https://github.com' && github.token \|\| '' }} | false | @@ -158,15 +197,6 @@ this action. For more on setting up those components, see the `gitops` component - -## Share the Love - -Like this project? Please give it a ★ on [our GitHub](https://github.com/cloudposse/github-action-atmos-terraform-apply)! (it helps us **a lot**) - -Are you using this project or any of our other projects? Consider [leaving a testimonial][testimonial]. =) - - - ## Related Projects Check out these related projects. @@ -181,81 +211,70 @@ For additional context, refer to some of these links. - [github-action-terraform-plan-storage](https://github.com/cloudposse/github-action-terraform-plan-storage) - GitHub Action to store Terraform plans -## Help - -**Got a question?** We got answers. +## ✨ Contributing -File a GitHub [issue](https://github.com/cloudposse/github-action-atmos-terraform-apply/issues), send us an [email][email] or join our [Slack Community][slack]. +This project is under active development, and we encourage contributions from our community. +Many thanks to our outstanding contributors: -[![README Commercial Support][readme_commercial_support_img]][readme_commercial_support_link] + + + -## DevOps Accelerator for Startups +### 🐛 Bug Reports & Feature Requests +Please use the [issue tracker](https://github.com/cloudposse/github-action-atmos-terraform-apply/issues) to report any bugs or file feature requests. -We are a [**DevOps Accelerator**][commercial_support]. We'll help you build your cloud infrastructure from the ground up so you can own it. Then we'll show you how to operate it and stick around for as long as you need us. +### 💻 Developing -[![Learn More](https://img.shields.io/badge/learn%20more-success.svg?style=for-the-badge)][commercial_support] +If you are interested in being a contributor and want to get involved in developing this project or [help out](https://cpco.io/help-out) with our other projects, we would love to hear from you! Shoot us an [email][email]. -Work directly with our team of DevOps experts via email, slack, and video conferencing. +In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow. -We deliver 10x the value for a fraction of the cost of a full-time engineer. Our track record is not even funny. If you want things done right and you need it done FAST, then we're your best bet. + 1. **Fork** the repo on GitHub + 2. **Clone** the project to your own machine + 3. **Commit** changes to your own branch + 4. **Push** your work back up to your fork + 5. Submit a **Pull Request** so that we can review your changes -- **Reference Architecture.** You'll get everything you need from the ground up built using 100% infrastructure as code. -- **Release Engineering.** You'll have end-to-end CI/CD with unlimited staging environments. -- **Site Reliability Engineering.** You'll have total visibility into your apps and microservices. -- **Security Baseline.** You'll have built-in governance with accountability and audit logs for all changes. -- **GitOps.** You'll be able to operate your infrastructure via Pull Requests. -- **Training.** You'll receive hands-on training so your team can operate what we build. -- **Questions.** You'll have a direct line of communication between our teams via a Shared Slack channel. -- **Troubleshooting.** You'll get help to triage when things aren't working. -- **Code Reviews.** You'll receive constructive feedback on Pull Requests. -- **Bug Fixes.** We'll rapidly work with you to fix any bugs in our projects. +**NOTE:** Be sure to merge the latest changes from "upstream" before making a pull request! -## Slack Community +### 🌎 Slack Community Join our [Open Source Community][slack] on Slack. It's **FREE** for everyone! Our "SweetOps" community is where you get to talk with others who share a similar vision for how to rollout and manage infrastructure. This is the best place to talk shop, ask questions, solicit feedback, and work together as a community to build totally *sweet* infrastructure. -## Discourse Forums - -Participate in our [Discourse Forums][discourse]. Here you'll find answers to commonly asked questions. Most questions will be related to the enormous number of projects we support on our GitHub. Come here to collaborate on answers, find solutions, and get ideas about the products and services we value. It only takes a minute to get started! Just sign in with SSO using your GitHub account. - -## Newsletter +### 📰 Newsletter Sign up for [our newsletter][newsletter] that covers everything on our technology radar. Receive updates on what we're up to on GitHub as well as awesome new projects we discover. -## Office Hours +### 📆 Office Hours [Join us every Wednesday via Zoom][office_hours] for our weekly "Lunch & Learn" sessions. It's **FREE** for everyone! -[![zoom](https://img.cloudposse.com/fit-in/200x200/https://cloudposse.com/wp-content/uploads/2019/08/Powered-by-Zoom.png")][office_hours] - -## Contributing - -### Bug Reports & Feature Requests +## About -Please use the [issue tracker](https://github.com/cloudposse/github-action-atmos-terraform-apply/issues) to report any bugs or file feature requests. - -### Developing - -If you are interested in being a contributor and want to get involved in developing this project or [help out](https://cpco.io/help-out) with our other projects, we would love to hear from you! Shoot us an [email][email]. - -In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow. - - 1. **Fork** the repo on GitHub - 2. **Clone** the project to your own machine - 3. **Commit** changes to your own branch - 4. **Push** your work back up to your fork - 5. Submit a **Pull Request** so that we can review your changes - -**NOTE:** Be sure to merge the latest changes from "upstream" before making a pull request! +This project is maintained and funded by [Cloud Posse, LLC][website]. + +We are a [**DevOps Accelerator**][commercial_support]. We'll help you build your cloud infrastructure from the ground up so you can own it. Then we'll show you how to operate it and stick around for as long as you need us. -## Copyright +[![Learn More](https://img.shields.io/badge/learn%20more-success.svg?style=for-the-badge)][commercial_support] -Copyright © 2017-2023 [Cloud Posse, LLC](https://cpco.io/copyright) +Work directly with our team of DevOps experts via email, slack, and video conferencing. +We deliver 10x the value for a fraction of the cost of a full-time engineer. Our track record is not even funny. If you want things done right and you need it done FAST, then we're your best bet. +- **Reference Architecture.** You'll get everything you need from the ground up built using 100% infrastructure as code. +- **Release Engineering.** You'll have end-to-end CI/CD with unlimited staging environments. +- **Site Reliability Engineering.** You'll have total visibility into your apps and microservices. +- **Security Baseline.** You'll have built-in governance with accountability and audit logs for all changes. +- **GitOps.** You'll be able to operate your infrastructure via Pull Requests. +- **Training.** You'll receive hands-on training so your team can operate what we build. +- **Questions.** You'll have a direct line of communication between our teams via a Shared Slack channel. +- **Troubleshooting.** You'll get help to triage when things aren't working. +- **Code Reviews.** You'll receive constructive feedback on Pull Requests. +- **Bug Fixes.** We'll rapidly work with you to fix any bugs in our projects. +[![README Commercial Support][readme_commercial_support_img]][readme_commercial_support_link] ## License [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) @@ -281,44 +300,11 @@ specific language governing permissions and limitations under the License. ``` - - - - - - - - ## Trademarks All other trademarks referenced herein are the property of their respective owners. - -## About - -This project is maintained and funded by [Cloud Posse, LLC][website]. Like it? Please let us know by [leaving a testimonial][testimonial]! - -[![Cloud Posse][logo]][website] - -We're a [DevOps Professional Services][hire] company based in Los Angeles, CA. We ❤️ [Open Source Software][we_love_open_source]. - -We offer [paid support][commercial_support] on all of our projects. - -Check out [our other projects][github], [follow us on twitter][twitter], [apply for a job][jobs], or [hire us][hire] to help with your cloud strategy and implementation. - - - -### Contributors - - -| [![Erik Osterman][osterman_avatar]][osterman_homepage]
[Erik Osterman][osterman_homepage] | [![Daniel Miller][milldr_avatar]][milldr_homepage]
[Daniel Miller][milldr_homepage] | -|---|---| - - - [osterman_homepage]: https://github.com/osterman - [osterman_avatar]: https://img.cloudposse.com/150x150/https://github.com/osterman.png - [milldr_homepage]: https://github.com/milldr - [milldr_avatar]: https://img.cloudposse.com/150x150/https://github.com/milldr.png - +--- +Copyright © 2017-2023 [Cloud Posse, LLC](https://cpco.io/copyright) [![README Footer][readme_footer_img]][readme_footer_link] [![Beacon][beacon]][website] @@ -329,12 +315,9 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply [jobs]: https://cpco.io/jobs?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=jobs [hire]: https://cpco.io/hire?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=hire [slack]: https://cpco.io/slack?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=slack - [linkedin]: https://cpco.io/linkedin?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=linkedin [twitter]: https://cpco.io/twitter?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=twitter - [testimonial]: https://cpco.io/leave-testimonial?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=testimonial [office_hours]: https://cloudposse.com/office-hours?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=office_hours [newsletter]: https://cpco.io/newsletter?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=newsletter - [discourse]: https://ask.sweetops.com/?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=discourse [email]: https://cpco.io/email?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=email [commercial_support]: https://cpco.io/commercial-support?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=commercial_support [we_love_open_source]: https://cpco.io/we-love-open-source?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=we_love_open_source @@ -345,11 +328,5 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply [readme_footer_link]: https://cloudposse.com/readme/footer/link?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=readme_footer_link [readme_commercial_support_img]: https://cloudposse.com/readme/commercial-support/img [readme_commercial_support_link]: https://cloudposse.com/readme/commercial-support/link?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/github-action-atmos-terraform-apply&utm_content=readme_commercial_support_link - [share_twitter]: https://twitter.com/intent/tweet/?text=github-action-atmos-terraform-apply&url=https://github.com/cloudposse/github-action-atmos-terraform-apply - [share_linkedin]: https://www.linkedin.com/shareArticle?mini=true&title=github-action-atmos-terraform-apply&url=https://github.com/cloudposse/github-action-atmos-terraform-apply - [share_reddit]: https://reddit.com/submit/?url=https://github.com/cloudposse/github-action-atmos-terraform-apply - [share_facebook]: https://facebook.com/sharer/sharer.php?u=https://github.com/cloudposse/github-action-atmos-terraform-apply - [share_googleplus]: https://plus.google.com/share?url=https://github.com/cloudposse/github-action-atmos-terraform-apply - [share_email]: mailto:?subject=github-action-atmos-terraform-apply&body=https://github.com/cloudposse/github-action-atmos-terraform-apply [beacon]: https://ga-beacon.cloudposse.com/UA-76589703-4/cloudposse/github-action-atmos-terraform-apply?pixel&cs=github&cm=readme&an=github-action-atmos-terraform-apply diff --git a/README.yaml b/README.yaml index 8b4915d..1084605 100644 --- a/README.yaml +++ b/README.yaml @@ -50,9 +50,29 @@ usage: |- to allow GitHub to assume roles in AWS and then deploying both a Terraform Apply role and a Terraform State role. For Cloud Posse documentation on setting up GitHub OIDC, see our [`github-oidc-provider` component](https://docs.cloudposse.com/components/library/aws/github-oidc-provider/). - In order to retrieve Terraform State, we configure an S3 Bucket to store plan files and a DynamoDB table to track plan metadata. Both will need to be deployed before running - this action. For more on setting up those components, see the `gitops` component (__documentation pending__). This action will then use the [github-action-terraform-plan-storage](https://github.com/cloudposse/github-action-terraform-plan-storage) action to update these resources. + In order to retrieve Terraform Plan Files (not to be confused with Terraform State files, e.g. `tfstate`), we configure an S3 Bucket to store plan files and a DynamoDB table to track plan metadata. Both need to be deployed before running + this action. For more on setting up those components, see the [`gitops` component](https://docs.cloudposse.com/components/library/aws/gitops/). This action will then use the [github-action-terraform-plan-storage](https://github.com/cloudposse/github-action-terraform-plan-storage) action to update these resources. + ### Config + + The action expects the atmos gitops configuration file to be present in the repository in `./.github/config/atmos-gitops.yaml`. + The config should have the following structure: + + ```yaml + atmos-version: 1.45.3 + atmos-config-path: ./rootfs/usr/local/etc/atmos/ + terraform-state-bucket: cptest-core-ue2-auto-gitops + terraform-state-table: cptest-core-ue2-auto-gitops + terraform-state-role: arn:aws:iam::xxxxxxxxxxxx:role/cptest-core-ue2-auto-gitops-gha + terraform-plan-role: arn:aws:iam::yyyyyyyyyyyy:role/cptest-core-gbl-identity-gitops + terraform-apply-role: arn:aws:iam::yyyyyyyyyyyy:role/cptest-core-gbl-identity-gitops + terraform-version: 1.5.2 + aws-region: us-east-2 + enable-infracost: false + sort-by: .stack_slug + group-by: .stack_slug | split("-") | [.[0], .[2]] | join("-") + ``` + ### Workflow example ```yaml @@ -75,20 +95,59 @@ usage: |- plan: runs-on: ubuntu-latest steps: - - name: Plan Atmos Component - uses: cloudposse/github-action-atmos-terraform-apply@v1 + - name: Terraform Apply + uses: cloudposse/github-action-atmos-terraform-apply@v2 with: component: "foobar" stack: "plat-ue2-sandbox" - component-path: "components/terraform/s3-bucket" - terraform-apply-role: "arn:aws:iam::111111111111:role/acme-core-gbl-identity-gitops" - terraform-state-bucket: "acme-core-ue2-auto-gitops" - terraform-state-role: "arn:aws:iam::999999999999:role/acme-core-ue2-auto-gitops-gha" - terraform-state-table: "acme-core-ue2-auto-gitops" - aws-region: "us-east-2" + ``` + ### Migrating from `v1` to `v2` + + 1. `v2` drops the `component-path` variable and instead fetches if directly from the [`atmos.yaml` file](https://atmos.tools/cli/configuration/) automatically. Simply remove the `component-path` argument from your invocations of the `cloudposse/github-action-atmos-terraform-apply` action. + 2. `v2` moves most of the `inputs` to the Atmos GitOps config path `./.github/config/atmos-gitops.yaml`. Simply create this file, transfer your settings to it, then remove the corresponding arguments from your invocations of the `cloudposse/github-action-atmos-terraform-apply` action. + | name | + |--------------------------| + | `atmos-version` | + | `atmos-config-path` | + | `terraform-state-bucket` | + | `terraform-state-table` | + | `terraform-state-role` | + | `terraform-plan-role` | + | `terraform-apply-role` | + | `terraform-version` | + | `aws-region` | + | `enable-infracost` | + + + If you want the same behavior in `v2` as in `v1` you should create config `./.github/config/atmos-gitops.yaml` with the same variables as in `v1` inputs. + + ```yaml + - name: Terraform apply + uses: cloudposse/github-action-atmos-terraform-apply@v2 + with: + atmos-gitops-config-path: ./.github/config/atmos-gitops.yaml + component: "foobar" + stack: "plat-ue2-sandbox" + ``` + + Which would produce the same behavior as in `v1`, doing this: + + ```yaml + - name: Terraform apply + uses: cloudposse/github-action-atmos-terraform-apply@v1 + with: + component: "foobar" + stack: "plat-ue2-sandbox" + component-path: "components/terraform/s3-bucket" + terraform-apply-role: "arn:aws:iam::111111111111:role/acme-core-gbl-identity-gitops" + terraform-state-bucket: "acme-core-ue2-auto-gitops" + terraform-state-role: "arn:aws:iam::999999999999:role/acme-core-ue2-auto-gitops-gha" + terraform-state-table: "acme-core-ue2-auto-gitops" + aws-region: "us-east-2" ``` + include: - "docs/github-action.md" diff --git a/action.yml b/action.yml index f15cae3..631b7cd 100644 --- a/action.yml +++ b/action.yml @@ -11,45 +11,14 @@ inputs: stack: description: "The stack name for the given component." required: true - component-path: - description: "The path to the base component. Atmos defines this value as component_path." - required: true sha: description: "Commit SHA to apply. Default: github.sha" required: true default: "${{ github.event.pull_request.head.sha }}" - terraform-apply-role: - description: "The AWS role to be used to apply Terraform." - required: true - terraform-state-role: - description: "The AWS role to be used to retrieve the planfile from AWS." - required: true - terraform-state-bucket: - description: "The S3 Bucket where the planfiles are stored." - required: true - terraform-state-table: - description: "The DynamoDB table where planfile metadata is stored." - required: true - aws-region: - description: "AWS region for assuming identity." - required: false - default: "us-east-1" - atmos-version: - description: "Atmos version to use for vendoring. Default 'latest'" - required: false - default: 'latest' - atmos-config-path: - description: "The path to the atmos.yaml file" - required: false - default: atmos.yaml - terraform-version: - description: 'The version of Terraform CLI to install. Instead of full version string you can also specify constraint string starting with "<" (for example `<1.13.0`) to install the latest version satisfying the constraint. A value of `latest` will install the latest version of Terraform CLI. Defaults to `latest`.' - default: 'latest' - required: false - enable-infracost: - description: "Whether to enable infracost summary. Requires secret `infracost-api-key` to be specified. Default: 'false" - default: 'false' + atmos-gitops-config-path: + description: The path to the atmos-gitops.yaml file required: false + default: ./.github/config/atmos-gitops.yaml infracost-api-key: description: "Infracost API key" required: false @@ -82,16 +51,25 @@ runs: - name: Checkout uses: actions/checkout@v3 - - name: Set vars + - name: Read Atmos GitOps config + ## We have to reference cloudposse fork of https://github.com/blablacar/action-config-levels + ## before https://github.com/blablacar/action-config-levels/pull/16 would be merged + uses: cloudposse/github-action-config-levels@nodejs20 + id: config + with: + output_properties: true + patterns: | + - ${{ inputs.atmos-gitops-config-path }} + + - name: Set atmos cli config path vars shell: bash run: |- - echo "ATMOS_CLI_CONFIG_PATH=$(realpath ${{inputs.atmos-config-path}})" >> $GITHUB_ENV - echo "ATMOS_BASE_PATH=${{ github.workspace }}" >> $GITHUB_ENV + echo "ATMOS_CLI_CONFIG_PATH=$(realpath ${{ steps.config.outputs.atmos-config-path }})" >> $GITHUB_ENV - name: Install Terraform uses: hashicorp/setup-terraform@v2 with: - terraform_version: ${{ inputs.terraform-version }} + terraform_version: ${{ steps.config.outputs.terraform-version }} terraform_wrapper: false - name: Install Node @@ -102,17 +80,17 @@ runs: - name: Install Atmos uses: cloudposse/github-action-setup-atmos@v1 with: - atmos-version: ${{ inputs.atmos-version }} + atmos-version: ${{ steps.config.outputs.atmos-version }} token: ${{ inputs.token }} install-wrapper: false - name: Filter Atmos Settings Value - uses: cloudposse/github-action-atmos-get-setting@v0 - id: atmos-settings + uses: cloudposse/github-action-atmos-get-setting@v1 + id: atmos-github-actions-enabled with: component: ${{ inputs.component }} stack: ${{ inputs.stack }} - settings-path: github.actions_enabled + settings-path: settings.github.actions_enabled - name: Define Job Control State Variables shell: bash @@ -124,12 +102,38 @@ runs: - name: Check If GitHub Actions is Enabled For Component shell: bash run: | - if [[ "${{ steps.atmos-settings.outputs.value }}" == "true" ]]; then + if [[ "${{ steps.atmos-github-actions-enabled.outputs.value }}" == "true" ]]; then echo "ACTIONS_ENABLED=true" >> $GITHUB_ENV else echo "ACTIONS_ENABLED=false" >> $GITHUB_ENV fi + - name: Find component path + if: env.ACTIONS_ENABLED == 'true' + uses: cloudposse/github-action-atmos-get-setting@v1 + id: component-path + with: + component: ${{ inputs.component }} + stack: ${{ inputs.stack }} + settings-path: component_info.component_path + + - name: Find atmos cli base path + if: env.ACTIONS_ENABLED == 'true' + uses: cloudposse/github-action-atmos-get-setting@v1 + id: base-path + with: + component: ${{ inputs.component }} + stack: ${{ inputs.stack }} + settings-path: atmos_cli_config.base_path + + - name: Set atmos cli base path vars + if: env.ACTIONS_ENABLED == 'true' + shell: bash + run: |- + # Set ATMOS_BASE_PATH allow `cloudposse/utils` provider to read atmos config from the correct path + ATMOS_BASE_PATH="${{ steps.base-path.outputs.value }}" + echo "ATMOS_BASE_PATH=$(realpath ${ATMOS_BASE_PATH:-./})" >> $GITHUB_ENV + - name: Install tfcmt if: env.ACTIONS_ENABLED == 'true' uses: jaxxstorm/action-install-gh-release@v1.10.0 @@ -143,14 +147,16 @@ runs: shell: bash run: | STACK_NAME=$(echo "${{ inputs.stack }}" | sed 's#/#_#g') + COMPONENT_PATH=${{ steps.component-path.outputs.value }} COMPONENT_NAME=$(echo "${{ inputs.component }}" | sed 's#/#_#g') COMPONENT_SLUG="$STACK_NAME-$COMPONENT_NAME" - PLAN_FILE="$GITHUB_WORKSPACE/${{ inputs.component-path}}/$COMPONENT_SLUG-${{ inputs.sha }}.planfile" - LOCK_FILE="$GITHUB_WORKSPACE/${{ inputs.component-path}}/.terraform.lock.hcl" + PLAN_FILE="$( realpath ${COMPONENT_PATH})/$COMPONENT_SLUG-${{ inputs.sha }}.planfile" + LOCK_FILE="$( realpath ${COMPONENT_PATH})/.terraform.lock.hcl" echo "stack_name=$STACK_NAME" >> $GITHUB_OUTPUT echo "component_name=$COMPONENT_NAME" >> $GITHUB_OUTPUT echo "component_slug=$COMPONENT_SLUG" >> $GITHUB_OUTPUT + echo "component_path=${COMPONENT_PATH}" >> $GITHUB_OUTPUT echo "plan_file=$PLAN_FILE" >> $GITHUB_OUTPUT echo "lock_file=$LOCK_FILE" >> $GITHUB_OUTPUT @@ -158,8 +164,8 @@ runs: if: env.ACTIONS_ENABLED == 'true' uses: aws-actions/configure-aws-credentials@v2.2.0 with: - aws-region: ${{ inputs.aws-region }} - role-to-assume: ${{ inputs.terraform-state-role }} + aws-region: ${{ steps.config.outputs.aws-region }} + role-to-assume: ${{ steps.config.outputs.terraform-state-role }} role-session-name: "atmos-terraform-state-gitops" mask-aws-account-id: "no" @@ -174,8 +180,8 @@ runs: commitSHA: ${{ inputs.sha }} component: ${{ inputs.component }} stack: ${{ inputs.stack }} - tableName: ${{ inputs.terraform-state-table }} - bucketName: ${{ inputs.terraform-state-bucket }} + tableName: ${{ steps.config.outputs.terraform-state-table }} + bucketName: ${{ steps.config.outputs.terraform-state-bucket }} - name: Retrieve Lockfile if: env.ACTIONS_ENABLED == 'true' @@ -187,15 +193,15 @@ runs: commitSHA: ${{ inputs.sha }} component: ${{ inputs.component }} stack: "${{ inputs.stack }}-lockfile" - tableName: ${{ inputs.terraform-state-table }} - bucketName: ${{ inputs.terraform-state-bucket }} + tableName: ${{ steps.config.outputs.terraform-state-table }} + bucketName: ${{ steps.config.outputs.terraform-state-bucket }} - name: Configure AWS Credentials if: env.ACTIONS_ENABLED == 'true' uses: aws-actions/configure-aws-credentials@v2.2.0 with: - aws-region: ${{ inputs.aws-region }} - role-to-assume: ${{ inputs.terraform-apply-role }} + aws-region: ${{ steps.config.outputs.aws-region }} + role-to-assume: ${{ steps.config.outputs.terraform-apply-role }} role-session-name: "atmos-terraform-apply-gitops" mask-aws-account-id: "no" @@ -210,7 +216,7 @@ runs: if: env.ACTIONS_ENABLED == 'true' shell: bash run: | - if [[ "${{ inputs.enable-infracost }}" == "true" ]]; then + if [[ "${{ steps.config.outputs.enable-infracost }}" == "true" ]]; then echo "INFRACOST_ENABLED=true" >> $GITHUB_ENV else echo "INFRACOST_ENABLED=false" >> $GITHUB_ENV @@ -250,7 +256,7 @@ runs: id: infracost-diff shell: bash run: | - if [[ "${{ inputs.enable-infracost }}" == "true" ]]; then + if [[ "${{ steps.config.outputs.enable-infracost }}" == "true" ]]; then INFRACOST_DIFF_TOTAL_MONTHLY_COST=$(cat /tmp/infracost.json | jq --raw-output .diffTotalMonthlyCost) INFRACOST_DETAILS_DIFF_BREAKDOWN="$(cat /tmp/infracost.txt | base64 --wrap 0)" else @@ -266,9 +272,8 @@ runs: id: apply shell: bash continue-on-error: true + working-directory: ./${{ steps.vars.outputs.component_path }} run: | - cd ${{ inputs.component-path }} - EXIT_CODE=0 tfcmt \ diff --git a/docs/github-action.md b/docs/github-action.md index 6b7b052..171bbac 100644 --- a/docs/github-action.md +++ b/docs/github-action.md @@ -4,23 +4,14 @@ | Name | Description | Default | Required | |------|-------------|---------|----------| -| atmos-config-path | The path to the atmos.yaml file | atmos.yaml | false | -| atmos-version | Atmos version to use for vendoring. Default 'latest' | latest | false | -| aws-region | AWS region for assuming identity. | us-east-1 | false | +| atmos-gitops-config-path | The path to the atmos-gitops.yaml file | ./.github/config/atmos-gitops.yaml | false | | branding-logo-image | Branding logo image url | https://cloudposse.com/logo-300x69.svg | false | | branding-logo-url | Branding logo url | https://cloudposse.com/ | false | -| commit-sha | Commit SHA to apply. Default: github.sha | ${{ github.sha }} | true | | component | The name of the component to apply. | N/A | true | -| component-path | The path to the base component. Atmos defines this value as component\_path. | N/A | true | | debug | Enable action debug mode. Default: 'false' | false | false | -| enable-infracost | Whether to enable infracost summary. Requires secret `infracost-api-key` to be specified. Default: 'false | false | false | | infracost-api-key | Infracost API key | N/A | false | +| sha | Commit SHA to apply. Default: github.sha | ${{ github.event.pull\_request.head.sha }} | true | | stack | The stack name for the given component. | N/A | true | -| terraform-apply-role | The AWS role to be used to apply Terraform. | N/A | true | -| terraform-state-bucket | The S3 Bucket where the planfiles are stored. | N/A | true | -| terraform-state-role | The AWS role to be used to retrieve the planfile from AWS. | N/A | true | -| terraform-state-table | The DynamoDB table where planfile metadata is stored. | N/A | true | -| terraform-version | The version of Terraform CLI to install. Instead of full version string you can also specify constraint string starting with "<" (for example `<1.13.0`) to install the latest version satisfying the constraint. A value of `latest` will install the latest version of Terraform CLI. Defaults to `latest`. | latest | false | | token | Used to pull node distributions for Atmos from Cloud Posse's GitHub repository. Since there's a default, this is typically not supplied by the user. When running this action on github.com, the default value is sufficient. When running on GHES, you can pass a personal access token for github.com if you are experiencing rate limiting. | ${{ github.server\_url == 'https://github.com' && github.token \|\| '' }} | false | diff --git a/atmos.yaml b/tests/atmos.yaml similarity index 99% rename from atmos.yaml rename to tests/atmos.yaml index d8fa3e7..8ee70cf 100644 --- a/atmos.yaml +++ b/tests/atmos.yaml @@ -15,7 +15,7 @@ # are independent settings (supporting both absolute and relative paths). # If `base_path` is provided, `components.terraform.base_path`, `components.helmfile.base_path`, `stacks.base_path` and `workflows.base_path` # are considered paths relative to `base_path`. -base_path: "" +base_path: "./tests" components: terraform: diff --git a/components/terraform/foobar/context.tf b/tests/components/terraform/foobar/context.tf similarity index 100% rename from components/terraform/foobar/context.tf rename to tests/components/terraform/foobar/context.tf diff --git a/components/terraform/foobar/main.tf b/tests/components/terraform/foobar/main.tf similarity index 83% rename from components/terraform/foobar/main.tf rename to tests/components/terraform/foobar/main.tf index e8fdb07..5fc3957 100644 --- a/components/terraform/foobar/main.tf +++ b/tests/components/terraform/foobar/main.tf @@ -2,6 +2,7 @@ resource "random_id" "foo" { keepers = { # Generate a new id each time we switch to a new seed seed = "${module.this.id}-${var.example}" + timestamp = "${timestamp()}" } byte_length = 8 } diff --git a/components/terraform/foobar/variables.tf b/tests/components/terraform/foobar/variables.tf similarity index 100% rename from components/terraform/foobar/variables.tf rename to tests/components/terraform/foobar/variables.tf diff --git a/stacks/catalog/foobar.yaml b/tests/stacks/catalog/foobar.yaml similarity index 100% rename from stacks/catalog/foobar.yaml rename to tests/stacks/catalog/foobar.yaml diff --git a/stacks/orgs/foo/plat/sandbox/plat-ue2-sandbox.yaml b/tests/stacks/orgs/foo/plat/sandbox/plat-ue2-sandbox.yaml similarity index 100% rename from stacks/orgs/foo/plat/sandbox/plat-ue2-sandbox.yaml rename to tests/stacks/orgs/foo/plat/sandbox/plat-ue2-sandbox.yaml