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.17]
 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      SOFT_SERVE_REPO_KEYS: "${{ secrets.SOFT_SERVE_REPO_KEYS }}"
 24
 25    steps:
 26    - name: Install Go
 27      uses: actions/setup-go@v2
 28      with:
 29        go-version: ${{ matrix.go-version }}
 30
 31    - name: Checkout code
 32      uses: actions/checkout@v2
 33      with:
 34        fetch-depth: 0
 35
 36    # Remove this later
 37    - name: Clone internal repositories
 38      run: |
 39        git clone -b release https://${{ secrets.ACCESS_TOKEN }}@github.com/charmbracelet/charm-internal ../charm
 40        git clone -b master https://${{ secrets.ACCESS_TOKEN }}@github.com/charmbracelet/bubbletea-internal ../bubbletea
 41        git clone -b master https://${{ secrets.ACCESS_TOKEN }}@github.com/charmbracelet/wish ../wish
 42
 43    - name: Login to GitHub Container Registry
 44      uses: docker/login-action@v1
 45      if: github.event_name == 'push'
 46      with:
 47        registry: ghcr.io
 48        username: ${{ github.repository_owner }}
 49        password: ${{ secrets.GITHUB_TOKEN }}
 50
 51    - name: Build Docker images using GoReleaser
 52      uses: goreleaser/goreleaser-action@master
 53      if: github.event_name == 'push'
 54      with:
 55        version: latest
 56        # https://github.com/goreleaser/goreleaser/discussions/1534
 57        args: -f .goreleaser.yml --snapshot
 58
 59    # Must add GH Actions write access
 60    # https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions
 61    - name: Push Docker images
 62      if: github.event_name == 'push'
 63      run: |
 64        docker push $CONTAINER_REPO:snapshot
 65        docker push $CONTAINER_REPO:$GITHUB_SHA-snapshot
 66
 67    - name: Setup Terraform
 68      uses: hashicorp/setup-terraform@v1
 69      with:
 70        # terraform_version: 0.13.0
 71        cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
 72
 73    - name: Terraform Variables
 74      id: tfvars
 75      run: |
 76        cat <<EOF >live.auto.tfvars
 77        environment        = "$ENVIRONMENT"
 78        aws_region         = "$AWS_DEFAULT_REGION"
 79        app_image          = "$CONTAINER_REPO:$GITHUB_SHA-snapshot"
 80        authorization_keys = <<EOT
 81        $SOFT_SERVE_REPO_KEYS
 82        EOT
 83        EOF
 84        TF_VARS="-var-file=live.auto.tfvars"
 85        echo "::set-output name=vars::$TF_VARS"
 86
 87    - name: Terraform Format
 88      id: fmt
 89      run: terraform fmt -check
 90
 91    - name: Terraform Init
 92      id: init
 93      run: terraform init
 94
 95    - name: Terraform Validate
 96      id: validate
 97      run: terraform validate -no-color
 98
 99    - name: Terraform Plan
100      id: plan
101      if: github.event_name == 'pull_request'
102      run: terraform plan -no-color ${{ steps.tfvars.outputs.vars }}
103      continue-on-error: true
104
105    - name: Find Comment
106      if: github.event_name == 'pull_request'
107      uses: peter-evans/find-comment@v1.2.0
108      id: fc
109      with:
110        issue-number: ${{ github.event.pull_request.number }}
111        comment-author: github-actions[bot]
112        body-includes: Terraform Summary
113
114    - name: Update Pull Request
115      uses: actions/github-script@0.9.0
116      if: github.event_name == 'pull_request'
117      env:
118        PLAN: "${{ steps.plan.outputs.stdout }}"
119        COMMENT_ID: "${{ steps.fc.outputs.comment-id }}"
120      with:
121        github-token: ${{ secrets.GITHUB_TOKEN }}
122        script: |
123          const output = `## Terraform Summary
124          - Terraform Format and Style 🖌 \`${{ steps.fmt.outcome }}\`
125          - Terraform Initialization ⚙️ \`${{ steps.init.outcome }}\`
126          - Terraform Plan 📖 \`${{ steps.plan.outcome }}\`
127          - Terraform Validation 🤖 \`${{ steps.validate.outcome }}\`
128
129          <details><summary>Show Plan</summary>
130
131          \`\`\`\n
132          ${process.env.PLAN}
133          \`\`\`
134
135          </details>
136
137          *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
138
139          if (process.env.COMMENT_ID) {
140            github.issues.updateComment({
141              owner: context.repo.owner,
142              repo: context.repo.repo,
143              comment_id: process.env.COMMENT_ID,
144              body: output
145            })
146          } else {
147            github.issues.createComment({
148              issue_number: context.issue.number,
149              owner: context.repo.owner,
150              repo: context.repo.repo,
151              body: output
152            })
153          }
154
155    - name: Terraform Plan Status
156      if: steps.plan.outcome == 'failure'
157      run: exit 1
158
159
160    - name: Terraform Apply
161      if: github.ref == 'refs/heads/main' && github.event_name == 'push'
162      run: terraform apply -auto-approve ${{ steps.tfvars.outputs.vars }}
163
164
165  slack-workflow-status:
166    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
167    name: Post Workflow Status To Slack
168    needs:
169      - cd
170    runs-on: ubuntu-latest
171    steps:
172      - name: Slack Workflow Notification
173        uses: Gamesight/slack-workflow-status@master
174        with:
175          # Required Input
176          repo_token: ${{ secrets.GITHUB_TOKEN }}
177          slack_webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}
178          # Optional Input
179          channel: '#devops'
180          name: "${{ github.repository }} 🥤 workflow"
181          icon_emoji: ':cup_with_straw:'
182          icon_url: 'https://avatars.githubusercontent.com/u/57376114?s=200&v=4'