|
| 1 | +name: Pre-Commit |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - master |
| 8 | + |
| 9 | +env: |
| 10 | + node-version: '14' |
| 11 | + python-version: '3.8' |
| 12 | + function-source-code-dir: ./source-code |
| 13 | + packaged-function-dir: ./packaged |
| 14 | + |
| 15 | +jobs: |
| 16 | + packageLambda: |
| 17 | + name: Package lambda function |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Checkout |
| 21 | + uses: actions/checkout@v2 |
| 22 | + - name: Install Node.js & npm |
| 23 | + uses: actions/setup-node@v2 |
| 24 | + with: |
| 25 | + node-version: ${{env.node-version}} |
| 26 | + check-latest: true |
| 27 | + - name: Install Python 3 |
| 28 | + uses: actions/setup-python@v2 |
| 29 | + with: |
| 30 | + python-version: ${{env.python-version}} |
| 31 | + - name: Install Serverless Framework |
| 32 | + run: npm install -g serverless |
| 33 | + - name: Install Node.js dependencies |
| 34 | + working-directory: ${{env.function-source-code-dir}} |
| 35 | + run: npm install |
| 36 | + - name: Create artifacts directory |
| 37 | + run: mkdir -p ${{env.packaged-function-dir}} |
| 38 | + - name: Package lambda function |
| 39 | + working-directory: ${{env.function-source-code-dir}} |
| 40 | + run: sls package --package ${{env.packaged-function-dir}} |
| 41 | + - name: Upload packaged lambda function to artifacts |
| 42 | + uses: actions/upload-artifact@v2 |
| 43 | + with: |
| 44 | + name: lambda |
| 45 | + path: ${{env.packaged-function-dir}}/*.zip |
| 46 | + |
| 47 | + # Min Terraform version(s) |
| 48 | + getDirectories: |
| 49 | + name: Get root directories |
| 50 | + runs-on: ubuntu-latest |
| 51 | + steps: |
| 52 | + - name: Checkout |
| 53 | + uses: actions/checkout@v2 |
| 54 | + - name: Install Python |
| 55 | + uses: actions/setup-python@v2 |
| 56 | + - name: Build matrix |
| 57 | + id: matrix |
| 58 | + run: | |
| 59 | + DIRS=$(python -c "import json; import glob; print(json.dumps([x.replace('/versions.tf', '') for x in glob.glob('./**/versions.tf', recursive=True)]))") |
| 60 | + echo "::set-output name=directories::$DIRS" |
| 61 | + outputs: |
| 62 | + directories: ${{ steps.matrix.outputs.directories }} |
| 63 | + |
| 64 | + preCommitMinVersions: |
| 65 | + name: Min TF validate |
| 66 | + needs: |
| 67 | + - getDirectories |
| 68 | + - packageLambda |
| 69 | + runs-on: ubuntu-latest |
| 70 | + strategy: |
| 71 | + matrix: |
| 72 | + directory: ${{ fromJson(needs.getDirectories.outputs.directories) }} |
| 73 | + steps: |
| 74 | + - name: Checkout |
| 75 | + uses: actions/checkout@v2 |
| 76 | + - name: Download artifcats (packaged lambda) |
| 77 | + uses: actions/download-artifact@v2 |
| 78 | + with: |
| 79 | + name: lambda |
| 80 | + - name: Create artifacts directory |
| 81 | + run: mkdir -p ${{env.packaged-function-dir}} && cp ./*.zip ${{env.packaged-function-dir}} |
| 82 | + - name: Install Python |
| 83 | + uses: actions/setup-python@v2 |
| 84 | + - name: Terraform min/max versions |
| 85 | + id: minMax |
| 86 | + |
| 87 | + with: |
| 88 | + directory: ${{ matrix.directory }} |
| 89 | + - name: Install Terraform v${{ steps.minMax.outputs.minVersion }} |
| 90 | + uses: hashicorp/setup-terraform@v1 |
| 91 | + with: |
| 92 | + terraform_version: ${{ steps.minMax.outputs.minVersion }} |
| 93 | + - name: Install pre-commit dependencies |
| 94 | + run: pip install pre-commit |
| 95 | + - name: Execute pre-commit |
| 96 | + # Run only validate pre-commit check on min version supported |
| 97 | + if: ${{ matrix.directory != '.' }} |
| 98 | + run: |
| 99 | + pre-commit run terraform_validate --color=always --show-diff-on-failure --files ${{ matrix.directory }}/* |
| 100 | + - name: Execute pre-commit |
| 101 | + # Run only validate pre-commit check on min version supported |
| 102 | + if: ${{ matrix.directory == '.' }} |
| 103 | + run: |
| 104 | + pre-commit run terraform_validate --color=always --show-diff-on-failure --files $(ls *.tf) |
| 105 | + |
| 106 | + # Max Terraform version |
| 107 | + getBaseVersion: |
| 108 | + name: Module max TF version |
| 109 | + runs-on: ubuntu-latest |
| 110 | + steps: |
| 111 | + - name: Checkout |
| 112 | + uses: actions/checkout@v2 |
| 113 | + - name: Terraform min/max versions |
| 114 | + id: minMax |
| 115 | + |
| 116 | + outputs: |
| 117 | + minVersion: ${{ steps.minMax.outputs.minVersion }} |
| 118 | + maxVersion: ${{ steps.minMax.outputs.maxVersion }} |
| 119 | + |
| 120 | + preCommitMaxVersion: |
| 121 | + name: Max TF pre-commit |
| 122 | + runs-on: ubuntu-latest |
| 123 | + needs: |
| 124 | + - getBaseVersion |
| 125 | + - packageLambda |
| 126 | + strategy: |
| 127 | + fail-fast: false |
| 128 | + matrix: |
| 129 | + version: |
| 130 | + - ${{ needs.getBaseVersion.outputs.maxVersion }} |
| 131 | + steps: |
| 132 | + - name: Checkout |
| 133 | + uses: actions/checkout@v2 |
| 134 | + - name: Download artifcats (packaged lambda) |
| 135 | + uses: actions/download-artifact@v2 |
| 136 | + with: |
| 137 | + name: lambda |
| 138 | + - name: Create artifacts directory |
| 139 | + run: mkdir -p ${{env.packaged-function-dir}} && cp ./*.zip ${{env.packaged-function-dir}} |
| 140 | + - name: Install Python |
| 141 | + uses: actions/setup-python@v2 |
| 142 | + - name: Install Terraform v${{ matrix.version }} |
| 143 | + uses: hashicorp/setup-terraform@v1 |
| 144 | + with: |
| 145 | + terraform_version: ${{ matrix.version }} |
| 146 | + - name: Install pre-commit dependencies |
| 147 | + run: | |
| 148 | + pip install pre-commit |
| 149 | + curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E "https://.+?-v0.12\..+?-linux-amd64" | head -n1)" > terraform-docs && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/ |
| 150 | + 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/ |
| 151 | + curl -L "$(curl -s https://api.github.com/repos/tfsec/tfsec/releases/latest | grep -o -E "https://.+?tfsec-linux-amd64")" > tfsec && chmod +x tfsec && sudo mv tfsec /usr/bin/ |
| 152 | + - name: Execute pre-commit |
| 153 | + # Run all pre-commit checks on max version supported |
| 154 | + if: ${{ matrix.version == needs.getBaseVersion.outputs.maxVersion }} |
| 155 | + run: pre-commit run --color=always --show-diff-on-failure --all-files |
0 commit comments