cd.yml

  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    # Must add GH Actions write access
 58    # https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions
 59    - name: Push Docker images
 60      if: github.event_name == 'push'
 61      run: |
 62        docker push $CONTAINER_REPO:snapshot
 63        docker push $CONTAINER_REPO:$GITHUB_SHA-snapshot
 64
 65    - name: Setup Terraform
 66      uses: hashicorp/setup-terraform@v1
 67      with:
 68        # terraform_version: 0.13.0
 69        cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
 70
 71    - name: Terraform Variables
 72      id: tfvars
 73      run: |
 74        TF_VARS=$(cat <<EOF
 75          -var "environment=$ENVIRONMENT" \
 76          -var "aws_region=$AWS_DEFAULT_REGION" \
 77          -var "app_image=$CONTAINER_REPO:$GITHUB_SHA-snapshot"
 78        EOF
 79        )
 80        echo "::set-output name=vars::$TF_VARS"
 81
 82    - name: Terraform Format
 83      id: fmt
 84      run: terraform fmt -check
 85
 86    - name: Terraform Init
 87      id: init
 88      run: terraform init
 89
 90    - name: Terraform Validate
 91      id: validate
 92      run: terraform validate -no-color
 93
 94    - name: Terraform Plan
 95      id: plan
 96      if: github.event_name == 'pull_request'
 97      run: terraform plan -no-color ${{ steps.tfvars.outputs.vars }}
 98      continue-on-error: true
 99
100    - name: Find Comment
101      if: github.event_name == 'pull_request'
102      uses: peter-evans/find-comment@v1.2.0
103      id: fc
104      with:
105        issue-number: ${{ github.event.pull_request.number }}
106        comment-author: github-actions[bot]
107        body-includes: Terraform Summary
108
109    - name: Update Pull Request
110      uses: actions/github-script@0.9.0
111      if: github.event_name == 'pull_request'
112      env:
113        PLAN: "${{ steps.plan.outputs.stdout }}"
114        COMMENT_ID: "${{ steps.fc.outputs.comment-id }}"
115      with:
116        github-token: ${{ secrets.GITHUB_TOKEN }}
117        script: |
118          const output = `## Terraform Summary
119          - Terraform Format and Style 🖌 \`${{ steps.fmt.outcome }}\`
120          - Terraform Initialization ⚙️ \`${{ steps.init.outcome }}\`
121          - Terraform Plan 📖 \`${{ steps.plan.outcome }}\`
122          - Terraform Validation 🤖 \`${{ steps.validate.outcome }}\`
123
124          <details><summary>Show Plan</summary>
125
126          \`\`\`\n
127          ${process.env.PLAN}
128          \`\`\`
129
130          </details>
131
132          *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
133
134          if (process.env.COMMENT_ID) {
135            github.issues.updateComment({
136              owner: context.repo.owner,
137              repo: context.repo.repo,
138              comment_id: process.env.COMMENT_ID,
139              body: output
140            })
141          } else {
142            github.issues.createComment({
143              issue_number: context.issue.number,
144              owner: context.repo.owner,
145              repo: context.repo.repo,
146              body: output
147            })
148          }
149
150    - name: Terraform Plan Status
151      if: steps.plan.outcome == 'failure'
152      run: exit 1
153
154
155    - name: Terraform Apply
156      if: github.ref == 'refs/heads/main' && github.event_name == 'push'
157      run: terraform apply -auto-approve ${{ steps.tfvars.outputs.vars }}
158