Skip to content

Commit

Permalink
Convert to TF 0.12. Add tests. Add Codefresh test pipeline (#27)
Browse files Browse the repository at this point in the history
* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline
  • Loading branch information
aknysh authored Oct 30, 2019
1 parent 7d05c8d commit 4c0f5db
Show file tree
Hide file tree
Showing 22 changed files with 1,406 additions and 292 deletions.
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

96 changes: 56 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[![Cloud Posse][logo]](https://cpco.io/homepage)

# terraform-aws-ecs-codepipeline [![Build Status](https://travis-ci.org/cloudposse/terraform-aws-ecs-codepipeline.svg?branch=master)](https://travis-ci.org/cloudposse/terraform-aws-ecs-codepipeline) [![Latest Release](https://img.shields.io/github/release/cloudposse/terraform-aws-ecs-codepipeline.svg)](https://github.com/cloudposse/terraform-aws-ecs-codepipeline/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com)
# terraform-aws-ecs-codepipeline [![Codefresh Build Status](https://g.codefresh.io/api/badges/pipeline/cloudposse/terraform-modules%2Fterraform-aws-ecs-codepipeline?type=cf-1)](https://g.codefresh.io/public/accounts/cloudposse/pipelines/5db7b474041f802a8893f018) [![Latest Release](https://img.shields.io/github/release/cloudposse/terraform-aws-ecs-codepipeline.svg)](https://github.com/cloudposse/terraform-aws-ecs-codepipeline/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com)


Terraform Module for CI/CD with AWS Code Pipeline using GitHub webhook triggers and Code Build for ECS.
Expand Down Expand Up @@ -48,22 +48,27 @@ Instead pin to the release tag (e.g. `?ref=tags/x.y.z`) of one of our [latest re



For a complete example, see [examples/complete](examples/complete).

For automated tests of the complete example using `bats` and `Terratest`, see [test](test).

### Trigger on GitHub Push

In this example, we'll trigger the pipeline anytime the `master` branch is updated.
```hcl
module "ecs_push_pipeline" {
source = "git::https://github.com/cloudposse/terraform-aws-ecs-codepipeline.git?ref=master"
name = "app"
namespace = "eg"
stage = "staging"
github_oauth_token = "xxxxxxxxxxxxxx"
repo_owner = "cloudposse"
repo_name = "example"
branch = "master"
service_name = "example"
ecs_cluster_name = "example-ecs-cluster"
privileged_mode = "true"
source = "git::https://github.com/cloudposse/terraform-aws-ecs-codepipeline.git?ref=master"
name = "app"
namespace = "eg"
stage = "staging"
github_oauth_token = "xxxxxxxxxxxxxx"
github_webhooks_token = "xxxxxxxxxxxxxx"
repo_owner = "cloudposse"
repo_name = "example"
branch = "master"
service_name = "example"
ecs_cluster_name = "eg-staging-example-cluster"
privileged_mode = "true"
}
```

Expand All @@ -78,17 +83,19 @@ module "ecs_release_pipeline" {
namespace = "eg"
stage = "staging"
github_oauth_token = "xxxxxxxxxxxxxx"
github_webhooks_token = "xxxxxxxxxxxxxx"
repo_owner = "cloudposse"
repo_name = "example"
branch = "master"
service_name = "example"
ecs_cluster_name = "example-ecs-cluster"
ecs_cluster_name = "eg-staging-example-cluster"
privileged_mode = "true"
github_webhook_events = ["release"]
webhook_filter_json_path = "$.action"
webhook_filter_match_equals = "published"
}
```

(Thanks to [Stack Overflow](https://stackoverflow.com/questions/52516087/trigger-aws-codepipeline-by-github-release-webhook#comment91997146_52524711))


Expand Down Expand Up @@ -147,46 +154,55 @@ Available targets:
| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| attributes | Additional attributes (e.g. `policy` or `role`) | list | `<list>` | no |
| attributes | Additional attributes (_e.g._ "1") | list(string) | `<list>` | no |
| aws_account_id | AWS Account ID. Used as CodeBuild ENV variable when building Docker images. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html) | string | `` | no |
| aws_region | AWS Region, e.g. us-east-1. Used as CodeBuild ENV variable when building Docker images. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html) | string | `` | no |
| badge_enabled | Generates a publicly-accessible URL for the projects build badge. Available as badge_url attribute when enabled. | string | `false` | no |
| badge_enabled | Generates a publicly-accessible URL for the projects build badge. Available as badge_url attribute when enabled | bool | `false` | no |
| branch | Branch of the GitHub repository, _e.g._ `master` | string | - | yes |
| build_compute_type | `CodeBuild` instance size. Possible values are: `BUILD_GENERAL1_SMALL` `BUILD_GENERAL1_MEDIUM` `BUILD_GENERAL1_LARGE` | string | `BUILD_GENERAL1_SMALL` | no |
| build_image | Docker image for build environment, _e.g._ `aws/codebuild/docker:docker:17.09.0` | string | `aws/codebuild/docker:17.09.0` | no |
| build_timeout | How long in minutes, from 5 to 480 (8 hours), for AWS CodeBuild to wait until timing out any related build that does not get marked as completed. | string | `60` | no |
| build_timeout | How long in minutes, from 5 to 480 (8 hours), for AWS CodeBuild to wait until timing out any related build that does not get marked as completed | number | `60` | no |
| buildspec | Declaration to use for building the project. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html) | string | `` | no |
| delimiter | Delimiter to be used between `name`, `namespace`, `stage`, etc. | string | `-` | no |
| delimiter | Delimiter between `namespace`, `stage`, `name` and `attributes` | string | `-` | no |
| ecs_cluster_name | ECS Cluster Name | string | - | yes |
| enabled | Enable `CodePipeline` creation | string | `true` | no |
| environment_variables | A list of maps, that contain both the key 'name' and the key 'value' to be used as additional environment variables for the build. | list | `<list>` | no |
| enabled | Enable `CodePipeline` creation | bool | `true` | no |
| environment_variables | A list of maps, that contain both the key 'name' and the key 'value' to be used as additional environment variables for the build | object | `<list>` | no |
| github_oauth_token | GitHub OAuth Token with permissions to access private repositories | string | - | yes |
| github_webhook_events | A list of events which should trigger the webhook. See a list of [available events](https://developer.github.com/v3/activity/events/types/) | list | `<list>` | no |
| github_webhook_events | A list of events which should trigger the webhook. See a list of [available events](https://developer.github.com/v3/activity/events/types/) | list(string) | `<list>` | no |
| github_webhooks_token | GitHub OAuth Token with permissions to create webhooks. If not provided, can be sourced from the `GITHUB_TOKEN` environment variable | string | `` | no |
| image_repo_name | ECR repository name to store the Docker image built by this module. Used as CodeBuild ENV variable when building Docker images. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html) | string | `UNSET` | no |
| image_repo_name | ECR repository name to store the Docker image built by this module. Used as CodeBuild ENV variable when building Docker images. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html) | string | - | yes |
| image_tag | Docker image tag in the ECR repository, e.g. 'latest'. Used as CodeBuild ENV variable when building Docker images. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html) | string | `latest` | no |
| name | Solution name, e.g. 'app' or 'jenkins' | string | `app` | no |
| namespace | Namespace, which could be your organization name, e.g. 'cp' or 'cloudposse' | string | `global` | no |
| poll_source_changes | Periodically check the location of your source content and run the pipeline if changes are detected | string | `false` | no |
| privileged_mode | If set to true, enables running the Docker daemon inside a Docker container on the CodeBuild instance. Used when building Docker images | string | `false` | no |
| repo_name | GitHub repository name of the application to be built and deployed to ECS. | string | - | yes |
| repo_owner | GitHub Organization or Username. | string | - | yes |
| s3_bucket_force_destroy | A boolean that indicates all objects should be deleted from the CodePipeline artifact store S3 bucket so that the bucket can be destroyed without error | string | `false` | no |
| name | Name of the application | string | - | yes |
| namespace | Namespace (e.g. `eg` or `cp`) | string | `` | no |
| poll_source_changes | Periodically check the location of your source content and run the pipeline if changes are detected | bool | `false` | no |
| privileged_mode | If set to true, enables running the Docker daemon inside a Docker container on the CodeBuild instance. Used when building Docker images | bool | `false` | no |
| region | AWS Region, e.g. us-east-1. Used as CodeBuild ENV variable when building Docker images. [For more info](http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html) | string | - | yes |
| repo_name | GitHub repository name of the application to be built and deployed to ECS | string | - | yes |
| repo_owner | GitHub Organization or Username | string | - | yes |
| s3_bucket_force_destroy | A boolean that indicates all objects should be deleted from the CodePipeline artifact store S3 bucket so that the bucket can be destroyed without error | bool | `false` | no |
| service_name | ECS Service Name | string | - | yes |
| stage | Stage, e.g. 'prod', 'staging', 'dev', or 'test' | string | `default` | no |
| tags | Additional tags (e.g. `map('BusinessUnit', 'XYZ')` | map | `<map>` | no |
| webhook_authentication | The type of authentication to use. One of IP, GITHUB_HMAC, or UNAUTHENTICATED. | string | `GITHUB_HMAC` | no |
| webhook_enabled | Set to false to prevent the module from creating any webhook resources | string | `true` | no |
| webhook_filter_json_path | The JSON path to filter on. | string | `$.ref` | no |
| stage | Stage (e.g. `prod`, `dev`, `staging`) | string | `` | no |
| tags | Additional tags (_e.g._ { BusinessUnit : ABC }) | map(string) | `<map>` | no |
| webhook_authentication | The type of authentication to use. One of IP, GITHUB_HMAC, or UNAUTHENTICATED | string | `GITHUB_HMAC` | no |
| webhook_enabled | Set to false to prevent the module from creating any webhook resources | bool | `true` | no |
| webhook_filter_json_path | The JSON path to filter on | string | `$.ref` | no |
| webhook_filter_match_equals | The value to match on (e.g. refs/heads/{Branch}) | string | `refs/heads/{Branch}` | no |
| webhook_target_action | The name of the action in a pipeline you want to connect to the webhook. The action must be from the source (first) stage of the pipeline. | string | `Source` | no |
| webhook_target_action | The name of the action in a pipeline you want to connect to the webhook. The action must be from the source (first) stage of the pipeline | string | `Source` | no |

## Outputs

| Name | Description |
|------|-------------|
| badge_url | The URL of the build badge when badge_enabled is enabled |
| webhook_id | The CodePipeline webhook's ARN. |
| codebuild_badge_url | The URL of the build badge when badge_enabled is enabled |
| codebuild_cache_bucket_arn | CodeBuild cache S3 bucket ARN |
| codebuild_cache_bucket_name | CodeBuild cache S3 bucket name |
| codebuild_project_id | CodeBuild project ID |
| codebuild_project_name | CodeBuild project name |
| codebuild_role_arn | CodeBuild IAM Role ARN |
| codebuild_role_id | CodeBuild IAM Role ID |
| codepipeline_arn | CodePipeline ARN |
| codepipeline_id | CodePipeline ID |
| webhook_id | The CodePipeline webhook's ID |
| webhook_url | The CodePipeline webhook's URL. POST events to this endpoint to trigger the target |


Expand Down Expand Up @@ -342,13 +358,13 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply
|---|---|---|---|

[osterman_homepage]: https://github.com/osterman
[osterman_avatar]: https://github.com/osterman.png?size=150
[osterman_avatar]: https://img.cloudposse.com/150x150/https://github.com/osterman.png
[goruha_homepage]: https://github.com/goruha
[goruha_avatar]: https://github.com/goruha.png?size=150
[goruha_avatar]: https://img.cloudposse.com/150x150/https://github.com/goruha.png
[aknysh_homepage]: https://github.com/aknysh
[aknysh_avatar]: https://github.com/aknysh.png?size=150
[aknysh_avatar]: https://img.cloudposse.com/150x150/https://github.com/aknysh.png
[sarkis_homepage]: https://github.com/sarkis
[sarkis_avatar]: https://github.com/sarkis.png?size=150
[sarkis_avatar]: https://img.cloudposse.com/150x150/https://github.com/sarkis.png



Expand Down
38 changes: 22 additions & 16 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ github_repo: cloudposse/terraform-aws-ecs-codepipeline

# Badges to display
badges:
- name: "Build Status"
image: "https://travis-ci.org/cloudposse/terraform-aws-ecs-codepipeline.svg?branch=master"
url: "https://travis-ci.org/cloudposse/terraform-aws-ecs-codepipeline"
- name: "Codefresh Build Status"
image: "https://g.codefresh.io/api/badges/pipeline/cloudposse/terraform-modules%2Fterraform-aws-ecs-codepipeline?type=cf-1"
url: "https://g.codefresh.io/public/accounts/cloudposse/pipelines/5db7b474041f802a8893f018"
- name: "Latest Release"
image: "https://img.shields.io/github/release/cloudposse/terraform-aws-ecs-codepipeline.svg"
url: "https://github.com/cloudposse/terraform-aws-ecs-codepipeline/releases/latest"
Expand Down Expand Up @@ -58,22 +58,27 @@ description: |-
# How to use this project
usage: |-
For a complete example, see [examples/complete](examples/complete).
For automated tests of the complete example using `bats` and `Terratest`, see [test](test).
### Trigger on GitHub Push
In this example, we'll trigger the pipeline anytime the `master` branch is updated.
```hcl
module "ecs_push_pipeline" {
source = "git::https://github.com/cloudposse/terraform-aws-ecs-codepipeline.git?ref=master"
name = "app"
namespace = "eg"
stage = "staging"
github_oauth_token = "xxxxxxxxxxxxxx"
repo_owner = "cloudposse"
repo_name = "example"
branch = "master"
service_name = "example"
ecs_cluster_name = "example-ecs-cluster"
privileged_mode = "true"
source = "git::https://github.com/cloudposse/terraform-aws-ecs-codepipeline.git?ref=master"
name = "app"
namespace = "eg"
stage = "staging"
github_oauth_token = "xxxxxxxxxxxxxx"
github_webhooks_token = "xxxxxxxxxxxxxx"
repo_owner = "cloudposse"
repo_name = "example"
branch = "master"
service_name = "example"
ecs_cluster_name = "eg-staging-example-cluster"
privileged_mode = "true"
}
```
Expand All @@ -88,19 +93,20 @@ usage: |-
namespace = "eg"
stage = "staging"
github_oauth_token = "xxxxxxxxxxxxxx"
github_webhooks_token = "xxxxxxxxxxxxxx"
repo_owner = "cloudposse"
repo_name = "example"
branch = "master"
service_name = "example"
ecs_cluster_name = "example-ecs-cluster"
ecs_cluster_name = "eg-staging-example-cluster"
privileged_mode = "true"
github_webhook_events = ["release"]
webhook_filter_json_path = "$.action"
webhook_filter_match_equals = "published"
}
```
(Thanks to [Stack Overflow](https://stackoverflow.com/questions/52516087/trigger-aws-codepipeline-by-github-release-webhook#comment91997146_52524711))
(Thanks to [Stack Overflow](https://stackoverflow.com/questions/52516087/trigger-aws-codepipeline-by-github-release-webhook#comment91997146_52524711))
# Example usage
Expand Down
74 changes: 74 additions & 0 deletions codefresh/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
version: '1.0'

stages:
- Prepare
- Test

steps:
wait:
title: Wait
stage: Prepare
image: codefresh/cli:latest
commands:
- codefresh get builds --pipeline=${{CF_REPO_NAME}} --status running --limit 1000 -o json | jq --arg id ${{CF_BUILD_ID}} -ser 'flatten|.[-1].id==$id'
retry:
maxAttempts: 10
delay: 20
exponentialFactor: 1.1

main_clone:
title: "Clone repository"
type: git-clone
stage: Prepare
description: "Initialize"
repo: ${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}
git: CF-default
revision: ${{CF_REVISION}}

clean_init:
title: Prepare build-harness and test-harness
image: ${{TEST_IMAGE}}
stage: Prepare
commands:
- cf_export PATH="/usr/local/terraform/0.12/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
- make init
- git -C build-harness checkout master
- make -C test/ clean init TEST_HARNESS_BRANCH=master
- make -C test/src clean init
- find . -type d -name '.terraform' | xargs rm -rf
- find . -type f -name 'terraform.tfstate*' -exec rm -f {} \;

test:
type: "parallel"
title: "Run tests"
description: "Run all tests in parallel"
stage: Test
steps:
test_readme_lint:
title: "Test README.md updated"
stage: "Test"
image: ${{TEST_IMAGE}}
description: Test "readme/lint"
commands:
- make readme/lint

test_module:
title: Test module with bats
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/ module

test_examples_complete:
title: Test "examples/complete" with bats
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/ examples/complete

test_examples_complete_terratest:
title: Test "examples/complete" with terratest
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/src
Loading

0 comments on commit 4c0f5db

Please sign in to comment.