1name: CD
  2
  3on:
  4  push:
  5    branches:
  6      - main
  7  pull_request:
  8    
  9
 10jobs:
 11  cd:
 12    strategy:
 13      matrix:
 14        go-version: [~1.16]
 15    runs-on: ubuntu-latest
 16    env:
 17      GO111MODULE: "on"
 18      CONTAINER_REPO: "ghcr.io/${{ github.repository }}"
 19      ENVIRONMENT: development
 20      AWS_DEFAULT_REGION: us-east-1
 21      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
 22      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
 23
 24    steps:
 25    - name: Install Go
 26      uses: actions/setup-go@v1
 27      with:
 28        go-version: ${{ matrix.go-version }}
 29
 30    - name: Checkout code
 31      uses: actions/checkout@v2
 32      with:
 33        fetch-depth: 0
 34
 35    # Remove this later
 36    - name: Clone internal repositories
 37      run: |
 38        git clone -b release https://${{ secrets.ACCESS_TOKEN }}@github.com/charmbracelet/charm-internal ../charm
 39        git clone -b master https://${{ secrets.ACCESS_TOKEN }}@github.com/charmbracelet/bubbletea-internal ../bubbletea
 40
 41    - name: Login to GitHub Container Registry
 42      uses: docker/login-action@v1
 43      if: github.event_name == 'push'
 44      with:
 45        registry: ghcr.io
 46        username: ${{ github.repository_owner }}
 47        password: ${{ secrets.GITHUB_TOKEN }}
 48
 49    - name: Build Docker images using GoReleaser
 50      uses: goreleaser/goreleaser-action@master
 51      if: github.event_name == 'push'
 52      with:
 53        version: latest
 54        # https://github.com/goreleaser/goreleaser/discussions/1534
 55        args: -f .goreleaser.yml --snapshot
 56
 57    - name: Push Docker images
 58      if: github.event_name == 'push'
 59      run: |
 60        docker push $CONTAINER_REPO:snapshot
 61        docker push $CONTAINER_REPO:$GITHUB_SHA-snapshot
 62
 63    - name: Setup Terraform
 64      uses: hashicorp/setup-terraform@v1
 65      with:
 66        # terraform_version: 0.13.0
 67        cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
 68
 69    - name: Terraform Variables
 70      id: tfvars
 71      run: |
 72        TF_VARS=$(cat <<EOF
 73          -var "environment=$ENVIRONMENT" \
 74          -var "aws_region=$AWS_DEFAULT_REGION" \
 75          -var "app_image=$CONTAINER_REPO:$GITHUB_SHA-snapshot"
 76        EOF
 77        )
 78        echo "::set-output name=vars::$TF_VARS"
 79
 80    - name: Terraform Format
 81      id: fmt
 82      run: terraform fmt -check
 83
 84    - name: Terraform Init
 85      id: init
 86      run: terraform init
 87
 88    - name: Terraform Validate
 89      id: validate
 90      run: terraform validate -no-color
 91
 92    - name: Terraform Plan
 93      id: plan
 94      if: github.event_name == 'pull_request'
 95      run: terraform plan -no-color ${{ steps.tfvars.outputs.vars }}
 96      continue-on-error: true
 97
 98    - name: Find Comment
 99      if: github.event_name == 'pull_request'
100      uses: peter-evans/find-comment@v1.2.0
101      id: fc
102      with:
103        issue-number: ${{ github.event.pull_request.number }}
104        comment-author: github-actions[bot]
105        body-includes: Terraform Summary
106
107    - name: Update Pull Request
108      uses: actions/github-script@0.9.0
109      if: github.event_name == 'pull_request'
110      env:
111        PLAN: "${{ steps.plan.outputs.stdout }}"
112        COMMENT_ID: "${{ steps.fc.outputs.comment-id }}"
113      with:
114        github-token: ${{ secrets.GITHUB_TOKEN }}
115        script: |
116          const output = `## Terraform Summary
117          - Terraform Format and Style 🖌 \`${{ steps.fmt.outcome }}\`
118          - Terraform Initialization ⚙️ \`${{ steps.init.outcome }}\`
119          - Terraform Plan 📖 \`${{ steps.plan.outcome }}\`
120          - Terraform Validation 🤖 \`${{ steps.validate.outcome }}\`
121
122          <details><summary>Show Plan</summary>
123
124          \`\`\`\n
125          ${process.env.PLAN}
126          \`\`\`
127
128          </details>
129
130          *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
131
132          if (process.env.COMMENT_ID) {
133            github.issues.updateComment({
134              owner: context.repo.owner,
135              repo: context.repo.repo,
136              comment_id: process.env.COMMENT_ID,
137              body: output
138            })
139          } else {
140            github.issues.createComment({
141              issue_number: context.issue.number,
142              owner: context.repo.owner,
143              repo: context.repo.repo,
144              body: output
145            })
146          }
147
148    - name: Terraform Plan Status
149      if: steps.plan.outcome == 'failure'
150      run: exit 1
151
152
153    - name: Terraform Apply
154      if: github.ref == 'refs/heads/main' && github.event_name == 'push'
155      run: terraform apply -auto-approve ${{ steps.tfvars.outputs.vars }}
156