Skip to content

Commit

Permalink
Rewrite github actions to use reusable workflows
Browse files Browse the repository at this point in the history
Also use docker image on staging deploy
  • Loading branch information
gmmcal committed Oct 17, 2023
1 parent 1b52d2d commit 166b81b
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 275 deletions.
112 changes: 12 additions & 100 deletions .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
name: End-to-end Tests

on:
- pull_request
workflow_call:
inputs:
pattern:
type: string
required: true
artifact-name:
type: string
required: true

env:
RAILS_ENV: development
DATABASE_URL: postgres://postgres:postgres@postgres:5432/gmmcalcombr_development

jobs:
admin:
name: 'E2E: Admin'
e2e:
name: 'End-to-end'
runs-on: ubuntu-20.04

container:
Expand Down Expand Up @@ -95,106 +102,11 @@ jobs:
start: yarn server:start
wait-on: 'http://localhost:3000'
install: false
spec: spec/end-to-end/tests/admin/**.js
spec: ${{ inputs.pattern }}
browser: chrome

- uses: actions/upload-artifact@v3
if: failure()
with:
name: cypress-screenshots-backend
path: spec/end-to-end/screenshots

frontend:
name: 'E2E: Frontend'
runs-on: ubuntu-20.04

container:
image: ruby:3.2.2

services:
postgres:
image: postgres:16
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: gmmcalcombr_development
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- uses: actions/checkout@v3

- name: Install APT dependencies
run: |
curl -sL https://deb.nodesource.com/setup_18.x | bash -
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
apt-get -yqq install nodejs libpq-dev xvfb libgtk-3-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2
- name: Install Chrome browser
run: |
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list && \
apt-get update && \
apt-get install -y dbus-x11 google-chrome-stable && \
rm -rf /var/lib/apt/lists/*
- name: Install yarn
run: npm i -g yarn

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT

- uses: actions/cache@v3
with:
path: vendor/bundle
key: ${{ runner.os }}-gems-gmmcal-${{ hashFiles('/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gems-gmmcal-
- uses: actions/cache@v3
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-gmmcal-${{ hashFiles('/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-gmmcal-
# dependencies
- name: Run bundle install
run: |
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3
- name: Run yarn install
run: yarn install

- name: Setup database
run: |
bundle exec rails db:migrate
- name: Compile assets
run: |
bundle exec rails assets:precompile
- name: Load default DB data
run: |
bundle exec rails db:seed
# script
- name: Run tests
uses: cypress-io/github-action@v6
with:
start: yarn server:start
wait-on: 'http://localhost:3000'
install: false
spec: spec/end-to-end/tests/frontend/**.js
browser: chrome

- uses: actions/upload-artifact@v3
if: failure()
with:
name: cypress-screenshots-frontend
name: ${{ inputs.artifact-name }}
path: spec/end-to-end/screenshots
71 changes: 14 additions & 57 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,24 @@ on:
types: [published]

jobs:
docker_production:
name: 'Docker: Production'
runs-on: ubuntu-20.04

if: github.event_name == 'push'

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build test image
uses: docker/build-push-action@v5
with:
push: true
context: .
target: production
tags: "gmmcal/gmmcal:latest"
cache-from: type=registry,ref=gmmcal/gmmcal:buildcache
cache-to: type=registry,ref=gmmcal/gmmcal:buildcache,mode=max

docker_development:
name: 'Docker: Development'
runs-on: ubuntu-20.04

if: github.event_name == 'push'

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build test image
uses: docker/build-push-action@v5
with:
push: true
context: .
target: development
tags: "gmmcal/gmmcal:development"
cache-from: type=registry,ref=gmmcal/gmmcal:buildcache
cache-to: type=registry,ref=gmmcal/gmmcal:buildcache,mode=max
build_production:
name: 'Build: Production'
uses: gmmcal/gmmcal.com.br/.github/workflows/docker.yml@reusable
secrets: inherit
with:
target: production

build_development:
name: 'Build: Development'
uses: gmmcal/gmmcal.com.br/.github/workflows/docker.yml@reusable
secrets: inherit
with:
target: development

staging:
name: 'Deploy: Staging'
runs-on: ubuntu-20.04
needs: build_production

if: github.event_name == 'push'

Expand Down
79 changes: 13 additions & 66 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
name: Build

on:
- push
- pull_request
workflow_call:
inputs:
tag-name:
type: string
required: false
default: "gmmcal/gmmcal"
target:
type: string
required: false
default: test

jobs:
docker:
name: 'Docker: Build'
build:
name: 'Build Image'
runs-on: ubuntu-20.04

steps:
Expand All @@ -28,67 +36,6 @@ jobs:
push: true
context: .
target: test
tags: "gmmcal/gmmcal:test"
tags: ${{ inputs.tag-name }}:${{ inputs.target }}
cache-from: type=registry,ref=gmmcal/gmmcal:buildcache
cache-to: type=registry,ref=gmmcal/gmmcal:buildcache,mode=max

rubocop:
name: 'Lint: Rubocop'
runs-on: ubuntu-20.04
needs: docker

steps:
- name: Run rubocop
run: |
docker run --rm gmmcal/gmmcal:test bundle exec rubocop --config .rubocop.yml .
reek:
name: 'Lint: Reek'
runs-on: ubuntu-20.04
needs: docker

steps:
- name: Run reek
run: |
docker run --rm gmmcal/gmmcal:test bundle exec reek --config .reek.yml .
brakeman:
name: 'Lint: Brakeman'
runs-on: ubuntu-20.04
needs: docker

steps:
- name: Run brakeman
run: |
docker run --rm gmmcal/gmmcal:test bundle exec brakeman
scsslint:
name: 'Lint: SCSSLint'
runs-on: ubuntu-20.04
needs: docker

steps:
- name: Run SCSSLint
run: |
docker run --rm gmmcal/gmmcal:test bundle exec scss-lint --config .scss-lint.yml
tests:
name: 'Unit: Backend'
runs-on: ubuntu-20.04
needs: docker

services:
postgres:
image: postgres:16
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: gmmcalcombr_test
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- name: Run rubocop
run: |
docker run --network=${{ job.services.postgres.network }} -e DATABASE_URL='postgres://postgres:postgres@postgres:5432/gmmcalcombr_test' -e DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true --rm gmmcal/gmmcal:test
56 changes: 9 additions & 47 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,55 +1,17 @@
name: Lint

on:
- pull_request
workflow_call:
inputs:
command:
type: string
required: true

jobs:
bundler-audit:
name: 'Lint: Bundler Audit'
lint:
name: Lint
runs-on: ubuntu-20.04

container:
image: ruby:3.2.2

steps:
- uses: actions/checkout@v3

- name: Install Bundler Audit
run: gem install bundler-audit

- name: Update advisory database
run: bundler-audit update

- name: Run bundler-audit
run: bundler-audit

eslint:
name: 'Lint: ESLint'
runs-on: ubuntu-20.04

container:
image: node:18

steps:
- uses: actions/checkout@v4

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT

- uses: actions/cache@v3
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-gmmcal-${{ hashFiles('/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-gmmcal-
- name: Run yarn install
run: yarn install

- name: Run ESLint - Application
run: yarn eslint

- name: Run ESLint - Tests
run: yarn eslint:tests
- name: Execute linter
run: docker run --rm gmmcal/gmmcal:test ${{ inputs.command }}
Loading

0 comments on commit 166b81b

Please sign in to comment.