This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
EventAPI CI #577
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: EventAPI CI | |
on: | |
push: | |
branches: | |
- master | |
- dev | |
pull_request: | |
types: | |
- opened | |
- synchronize | |
- reopened | |
- labeled | |
workflow_dispatch: | |
inputs: | |
deploy: | |
description: "Which environment to deploy to" | |
required: true | |
default: "none" | |
type: choice | |
options: | |
- prod | |
- test | |
- none | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
env: | |
DEPLOY: ${{ (inputs.deploy != 'none' && inputs.deploy) || ((github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'prod') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'master') && 'prod') || ((github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'test') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'dev') || (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'staged')) && 'test') || 'none' }} | |
jobs: | |
ci: | |
name: EventAPI Lint & Build | |
runs-on: aws-runner | |
env: | |
GOLANGCI_LINT_CACHE: /home/runner/.cache/golangci-lint | |
concurrency: | |
group: ${{ github.workflow }}-ci-${{ github.ref }} | |
cancel-in-progress: true | |
steps: | |
- name: Set up Go | |
uses: actions/setup-go@v3 | |
with: | |
go-version: 1.21.3 | |
- uses: actions/setup-node@v3 | |
with: | |
node-version: "18" | |
- name: Install Yarn | |
run: npm install -g yarn | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
submodules: recursive | |
- id: cache-paths | |
run: | | |
echo "::set-output name=go-build::$(go env GOCACHE)" | |
echo "::set-output name=go-mod::$(go env GOMODCACHE)" | |
- name: Go Build Cache | |
uses: actions/cache@v3 | |
with: | |
path: ${{ steps.cache-paths.outputs.go-build }} | |
key: ${{ runner.os }}-go-build-${{ hashFiles('go.sum') }} | |
- name: Go Mod Cache | |
uses: actions/cache@v3 | |
with: | |
path: ${{ steps.cache-paths.outputs.go-mod }} | |
key: ${{ runner.os }}-go-mod-${{ hashFiles('go.sum') }} | |
- name: GoLint Cache | |
uses: actions/cache@v3 | |
with: | |
path: ${{ env.GOLANGCI_LINT_CACHE }} | |
key: ${{ runner.os }}-go-lint-ci | |
- name: Node Modules Cache | |
uses: actions/cache@v3 | |
with: | |
path: node_modules | |
key: ${{ runner.os }}-node_modules-${{ hashFiles('yarn.lock') }} | |
- name: Install Dependencies | |
run: make deps | |
- name: Install Developer Dependencies | |
run: make dev_deps | |
- name: Run Linter | |
run: make lint | |
- name: Build App | |
run: make build | |
- name: Make build context | |
if: env.DEPLOY != 'none' | |
run: | | |
docker context create builders | |
- name: Setup buildx | |
uses: docker/setup-buildx-action@v2 | |
if: env.DEPLOY != 'none' | |
with: | |
install: true | |
endpoint: builders | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ github.token }} | |
- name: Build docker image | |
uses: docker/build-push-action@v3 | |
if: env.DEPLOY != 'none' | |
with: | |
context: . | |
file: docker/partial.Dockerfile | |
tags: | | |
ghcr.io/seventv/eventapi:${{ env.DEPLOY }}-${{ github.sha }} | |
ghcr.io/seventv/eventapi:${{ env.DEPLOY }}-latest | |
push: true | |
validate: | |
name: EventAPI Deploy Validation | |
needs: ci | |
runs-on: ubuntu-latest | |
permissions: | |
pull-requests: write | |
defaults: | |
run: | |
working-directory: ./terraform | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: "Setup Terraform" | |
uses: hashicorp/setup-terraform@v1 | |
with: | |
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} | |
- name: "Terraform Init" | |
id: init | |
env: | |
TF_WORKSPACE: ${{ env.DEPLOY }} | |
run: terraform init | |
continue-on-error: true | |
- name: "Terraform Workspace" | |
run: terraform workspace select -or-create=true ${{ env.DEPLOY }} | |
- name: Terraform fmt | |
id: fmt | |
run: terraform fmt -check | |
continue-on-error: true | |
- name: Terraform Validate | |
id: validate | |
run: terraform validate -no-color | |
- name: Terraform Variables | |
run: | | |
cat <<EOF > *.auto.tfvars | |
image_url="ghcr.io/seventv/eventapi:${{ env.DEPLOY }}-${{ github.sha }}" | |
image_pull_policy="IfNotPresent" | |
EOF | |
- name: "Terraform Plan" | |
id: plan | |
run: terraform plan -no-color | |
- uses: actions/github-script@v6 | |
if: github.event_name == 'pull_request' | |
env: | |
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}" | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
// 1. Retrieve existing bot comments for the PR | |
const { data: comments } = await github.rest.issues.listComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
}) | |
const botComment = comments.find(comment => { | |
return comment.user.type === 'Bot' && comment.body.includes('Terraform Format and Style') | |
}) | |
// 2. Prepare format of the comment | |
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\` | |
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\` | |
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\` | |
<details><summary>Validation Output</summary> | |
\`\`\`\n | |
${{ steps.validate.outputs.stdout }} | |
\`\`\` | |
</details> | |
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\` | |
<details><summary>Show Plan</summary> | |
\`\`\`\n | |
${process.env.PLAN} | |
\`\`\` | |
</details> | |
*Actor: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: \`${{ github.workflow }}\`*`; | |
// 3. If we have a comment, update it, otherwise create a new one | |
if (botComment) { | |
github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: botComment.id, | |
body: output | |
}) | |
} else { | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output | |
}) | |
} | |
- name: "Terraform Apply" | |
if: env.deploy != 'none' | |
id: apply | |
run: terraform apply -no-color -auto-approve | |
continue-on-error: true |