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      SMOOTHIE_REPO_KEYS: "${{ secrets.SMOOTHIE_REPO_KEYS }}"
 24
 25    steps:
 26    - name: Install Go
 27      uses: actions/setup-go@v1
 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        TF_VARS=$(cat <<EOF
 77        -var environment="$ENVIRONMENT" \
 78        -var aws_region="$AWS_DEFAULT_REGION" \
 79        -var app_image="$CONTAINER_REPO:$GITHUB_SHA-snapshot" \
 80        -var authorization_keys="$(echo -n $SMOOTHIE_REPO_KEYS)"
 81        EOF
 82        )
 83        echo "::set-output name=vars::$TF_VARS"
 84
 85    - name: Terraform Format
 86      id: fmt
 87      run: terraform fmt -check
 88
 89    - name: Terraform Init
 90      id: init
 91      run: terraform init
 92
 93    - name: Terraform Validate
 94      id: validate
 95      run: terraform validate -no-color
 96
 97    - name: Terraform Plan
 98      id: plan
 99      if: github.event_name == 'pull_request'
100      run: terraform plan -no-color ${{ steps.tfvars.outputs.vars }}
101      continue-on-error: true
102
103    - name: Find Comment
104      if: github.event_name == 'pull_request'
105      uses: peter-evans/find-comment@v1.2.0
106      id: fc
107      with:
108        issue-number: ${{ github.event.pull_request.number }}
109        comment-author: github-actions[bot]
110        body-includes: Terraform Summary
111
112    - name: Update Pull Request
113      uses: actions/github-script@0.9.0
114      if: github.event_name == 'pull_request'
115      env:
116        PLAN: "${{ steps.plan.outputs.stdout }}"
117        COMMENT_ID: "${{ steps.fc.outputs.comment-id }}"
118      with:
119        github-token: ${{ secrets.GITHUB_TOKEN }}
120        script: |
121          const output = `## Terraform Summary
122          - Terraform Format and Style 🖌 \`${{ steps.fmt.outcome }}\`
123          - Terraform Initialization ⚙️ \`${{ steps.init.outcome }}\`
124          - Terraform Plan 📖 \`${{ steps.plan.outcome }}\`
125          - Terraform Validation 🤖 \`${{ steps.validate.outcome }}\`
126
127          <details><summary>Show Plan</summary>
128
129          \`\`\`\n
130          ${process.env.PLAN}
131          \`\`\`
132
133          </details>
134
135          *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
136
137          if (process.env.COMMENT_ID) {
138            github.issues.updateComment({
139              owner: context.repo.owner,
140              repo: context.repo.repo,
141              comment_id: process.env.COMMENT_ID,
142              body: output
143            })
144          } else {
145            github.issues.createComment({
146              issue_number: context.issue.number,
147              owner: context.repo.owner,
148              repo: context.repo.repo,
149              body: output
150            })
151          }
152
153    - name: Terraform Plan Status
154      if: steps.plan.outcome == 'failure'
155      run: exit 1
156
157
158    - name: Terraform Apply
159      if: github.ref == 'refs/heads/main' && github.event_name == 'push'
160      run: terraform apply -auto-approve ${{ steps.tfvars.outputs.vars }}
161
162
163  slack-workflow-status:
164    if: always()
165    name: Post Workflow Status To Slack
166    needs:
167      - cd
168    runs-on: ubuntu-latest
169    steps:
170      - name: Slack Workflow Notification
171        uses: Gamesight/slack-workflow-status@master
172        with:
173          # Required Input
174          repo_token: ${{ secrets.GITHUB_TOKEN }}
175          slack_webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}
176          # Optional Input
177          channel: '#devops'
178          name: "${{ github.repository }} 🥤 workflow"
179          icon_emoji: ':cup_with_straw:'
180          icon_url: 'https://avatars.githubusercontent.com/u/57376114?s=200&v=4'