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