Skip to content

Commit

Permalink
Merge pull request #4 from tomarv2/develop
Browse files Browse the repository at this point in the history
Pulling refs/heads/develop into main
  • Loading branch information
tomarv2 authored Mar 28, 2022
2 parents e2529b4 + 4882ffb commit d516f1e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 57 deletions.
68 changes: 57 additions & 11 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ on:
pull_request:
push:
branches:
- main
- develop
- main

jobs:
# Min Terraform version(s)
getDirectories:
name: Get root directories
runs-on: ubuntu-latest
Expand All @@ -31,7 +30,7 @@ jobs:
directories: ${{ steps.matrix.outputs.directories }}

preCommitMinVersions:
name: Min TF validate
name: Min validate
needs: getDirectories
runs-on: ubuntu-latest
strategy:
Expand All @@ -58,11 +57,9 @@ jobs:
with:
terraform_version: ${{ steps.minMax.outputs.minVersion }}

- name: Install pre-commit dependencies
- name: Install pre-commit
run: pip install pre-commit


# Max Terraform version
getBaseVersion:
name: Module max TF version
runs-on: ubuntu-latest
Expand All @@ -78,7 +75,7 @@ jobs:
maxVersion: ${{ steps.minMax.outputs.maxVersion }}

preCommitMaxVersion:
name: Max TF pre-commit
name: Max pre-commit
runs-on: ubuntu-latest
needs: getBaseVersion
strategy:
Expand All @@ -105,10 +102,59 @@ jobs:
run: |
pip install pre-commit
pip install checkov
curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E "https://.+?-v1.0.1-linux-amd64" | head -n1)" > terraform-docs && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/
- name: Execute pre-commit
# Run all pre-commit checks on max version supported
if: ${{ matrix.version == needs.getBaseVersion.outputs.maxVersion }}
run: pre-commit run --color=always --show-diff-on-failure --all-files
continue-on-error: true # To avoid pre-commit failure
run: |
pre-commit run --color=always --show-diff-on-failure --all-files
- name: Get current branch name
id: vars
run: |
echo ::set-output name=branch_name::${GITHUB_REF##*/}
- name: "Get branch name and save to env"
env:
IS_PR: ${{ github.EVENT_NAME == 'pull_request' }}
run: |
if ${IS_PR}; then
BRANCH_NAME="${GITHUB_HEAD_REF}"
else
BRANCH_NAME="${GITHUB_REF##*/}"
fi
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }}

- name: Render terraform docs and commit changes
if: ${{ env.BRANCH_NAME }} == 'develop'
uses: terraform-docs/gh-actions@main
with:
working-dir: .
output-file: README.md
output-method: inject
git-push: "true"

- name: Commit pre-commit modified files
if: ${{ env.BRANCH_NAME }} == 'develop'
run: |
git config --local user.email "terraform+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git diff-index --quiet HEAD || (git add -A && git commit -m'[bot] update files' --allow-empty && git push -f)
- uses: actions/checkout@v2
- name: Create PR
continue-on-error: true
uses: repo-sync/pull-request@v2
if: ${{ env.BRANCH_NAME }} == 'develop'
with:
source_branch: "develop"
destination_branch: "main"
pr_title: "Pulling ${{ github.ref }} into main"
pr_body: ":crown: *Automated PR*"
pr_label: "auto-pr"
pr_allow_empty: false
github_token: ${{ secrets.GITHUB_TOKEN }}
32 changes: 32 additions & 0 deletions .github/workflows/state-actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: "Mark or close stale issues and PRs"
on:
schedule:
- cron: "0 0 10 * *"

jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v4
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
# Staling issues and PR's
days-before-stale: 30
stale-issue-label: stale
stale-pr-label: stale
stale-issue-message: |
This issue has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this issue will be closed in 10 days
stale-pr-message: |
This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days
# Not stale if have this labels or part of milestone
exempt-issue-labels: bug,wip,on-hold
exempt-pr-labels: bug,wip,on-hold
exempt-all-milestones: true
# Close issue operations
# Label will be automatically removed if the issues are no longer closed nor locked.
days-before-close: 10
delete-branch: false
close-issue-message: This issue was automatically closed because of stale in 10 days
close-pr-message: This PR was automatically closed because of stale in 10 days
24 changes: 11 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ override.tf.json
# example: *tfplan*

.idea/
.vscode/
.terraform.lock.hcl

# go
Expand Down Expand Up @@ -81,15 +82,10 @@ _testmain.go

# Python
# Editors
.vscode/
.idea/

# Vagrant
.vagrant/

# Mac/OSX
.DS_Store

# Windows
Thumbs.db

Expand All @@ -99,9 +95,6 @@ __pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
Expand Down Expand Up @@ -206,9 +199,14 @@ venv.bak/
.dmypy.json
dmypy.json

# ignore test related file(s)
**/test**
**.

# ignore terraform external modules
# Terraform external modules
**/.external_modules

# Test files and directories
**/[Tt]est/*
**/[Tt]ests/*
**/*[Tt]est*/*
**/*[Tt]ests*/*

# remote_backend.tf file
**/remote_backend.tf
16 changes: 11 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: git://github.com/antonbabenko/pre-commit-terraform
rev: v1.48.0
rev: v1.60.0
hooks:
- id: terraform_fmt
- id: terraform_tflint
Expand All @@ -14,19 +14,25 @@ repos:
- '--args=--only=terraform_typed_variables'
- '--args=--only=terraform_module_pinned_source'
- '--args=--only=terraform_naming_convention'
- '--args=--only=terraform_required_providers'
- '--args=--only=terraform_standard_module_structure'
- '--args=--only=terraform_workspace_remote'

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
rev: v4.1.0
hooks:
- id: trailing-whitespace
- id: check-merge-conflict
- id: end-of-file-fixer
- id: check-docstring-first
- id: check-yaml
- id: debug-statements
- id: double-quote-string-fixer
- id: name-tests-test
- id: requirements-txt-fixer

- repo: https://github.com/bridgecrewio/checkov.git
rev: '2.0.682'
rev: '2.0.914'
hooks:
- id: checkov
verbose: true
args: [-d, '.', --framework, 'terraform']
args: [-d, '.', --framework,'terraform']
56 changes: 28 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
<img src="https://img.shields.io/github/v/tag/tomarv2/terraform-azure-msi" /></a>
in your releases)

## Usage
### Usage

### Option 1:
#### Option 1:

```
terrafrom init
Expand All @@ -37,9 +37,9 @@ terraform destroy -var='teamid=tryme' -var='prjid=project1'
```
**Note:** With this option please take care of remote state storage

### Option 2:
#### Option 2:

#### Recommended method (stores remote state in storage using `prjid` and `teamid` to create directory structure):
##### Recommended method (stores remote state in S3 using `prjid` and `teamid` to create directory structure):

- Create python 3.8+ virtual environment
```
Expand All @@ -53,39 +53,37 @@ pip install tfremote --upgrade

- Set below environment variables:
```
export TF_AZURE_STORAGE_ACCOUNT=tfstatexxxxx # Output of remote_state.sh
export TF_AZURE_CONTAINER=tfstate # Output of remote_state.sh
export ARM_ACCESS_KEY=xxxxxxxxxx # Output of remote_state.sh
export TF_AWS_BUCKET=<remote state bucket name>
export TF_AWS_BUCKET_REGION=us-west-2
export TF_AWS_PROFILE=<profile from ~/.ws/credentials>
```

- Updated `examples` directory to required values
or

- Set below environment variables:
```
export TF_AWS_BUCKET=<remote state bucket name>
export TF_AWS_BUCKET_REGION=us-west-2
export AWS_ACCESS_KEY_ID=<aws_access_key_id>
export AWS_SECRET_ACCESS_KEY=<aws_secret_access_key>
```

- Updated `examples` directory with required values.

- Run and verify the output before deploying:
```
tf -c=azure plan -var='teamid=foo' -var='prjid=bar'
tf -c=aws plan -var='teamid=foo' -var='prjid=bar'
```

- Run below to deploy:
```
tf -c=azure apply -var='teamid=foo' -var='prjid=bar'
tf -c=aws apply -var='teamid=foo' -var='prjid=bar'
```

- Run below to destroy:
```
tf -c=azure destroy -var='teamid=foo' -var='prjid=bar'
tf -c=aws destroy -var='teamid=foo' -var='prjid=bar'
```
**NOTE:**

- Read more on [tfremote](https://github.com/tomarv2/tfremote)

### [Authenticate with Azure](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs)

Terraform supports a number of different methods for authenticating to Azure:

- Authenticating to Azure using the Azure CLI
- Authenticating to Azure using Managed Service Identity
- Authenticating to Azure using a Service Principal and a Client Certificate
- Authenticating to Azure using a Service Principal and a Client Secret

**Note:** Read more on [tfremote](https://github.com/tomarv2/tfremote)
Please refer to examples directory [link](examples) for references.
Expand Down Expand Up @@ -117,6 +115,7 @@ module "msi" {

Please refer to examples directory [link](examples) for references.

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
Expand All @@ -128,7 +127,7 @@ Please refer to examples directory [link](examples) for references.

| Name | Version |
|------|---------|
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | 2.48.0 |
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | ~> 2.98 |

## Modules

Expand All @@ -144,11 +143,11 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_add_msi"></a> [add\_msi](#input\_add\_msi) | feature flag to deploy this resource or not | `bool` | `false` | no |
| <a name="input_msi_depends_on"></a> [msi\_depends\_on](#input\_msi\_depends\_on) | n/a | `string` | `null` | no |
| <a name="input_msi_location"></a> [msi\_location](#input\_msi\_location) | n/a | `string` | `"westus2"` | no |
| <a name="input_add_msi"></a> [add\_msi](#input\_add\_msi) | feature flag to deploy this resource or not | `bool` | `true` | no |
| <a name="input_extra_tags"></a> [extra\_tags](#input\_extra\_tags) | Additional tags to associate | `map(string)` | `{}` | no |
| <a name="input_location"></a> [location](#input\_location) | Specifies the supported Azure location where the resource exists | `string` | `"westus2"` | no |
| <a name="input_prjid"></a> [prjid](#input\_prjid) | Name of the project/stack e.g: mystack, nifieks, demoaci. Should not be changed after running 'tf apply' | `string` | n/a | yes |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | Resource Group Name | `string` | n/a | yes |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | Name of the resource group | `string` | n/a | yes |
| <a name="input_teamid"></a> [teamid](#input\_teamid) | Name of the team/group e.g. devops, dataengineering. Should not be changed after running 'tf apply' | `string` | n/a | yes |

## Outputs
Expand All @@ -158,3 +157,4 @@ No modules.
| <a name="output_msi_id"></a> [msi\_id](#output\_msi\_id) | The ID of the MSI created |
| <a name="output_msi_name"></a> [msi\_name](#output\_msi\_name) | MSI name |
| <a name="output_msi_principal_id"></a> [msi\_principal\_id](#output\_msi\_principal\_id) | The ID of the Principal (User, Group or Service Principal) to assign the Role Definition |
<!-- END_TF_DOCS -->

0 comments on commit d516f1e

Please sign in to comment.