From 1780ca2327295697f786783279d8c5d2783f9c2b Mon Sep 17 00:00:00 2001 From: Gustavo Cunha Date: Tue, 17 Oct 2023 23:28:59 +0200 Subject: [PATCH] Rewrite github actions to use reusable workflows Also use docker image on staging deploy --- .github/workflows/cypress.yml | 400 ++++++++++++++--------------- .github/workflows/deploy.yml | 1 + .github/workflows/docker.yml | 77 +----- .github/workflows/lint.yml | 55 +--- .github/workflows/pull_request.yml | 117 +++++++++ Dockerfile | 2 +- bin/docker-entrypoint | 9 +- render.yaml | 9 +- 8 files changed, 353 insertions(+), 317 deletions(-) create mode 100644 .github/workflows/pull_request.yml diff --git a/.github/workflows/cypress.yml b/.github/workflows/cypress.yml index 6b5ba5010..87162b9aa 100644 --- a/.github/workflows/cypress.yml +++ b/.github/workflows/cypress.yml @@ -1,200 +1,200 @@ -name: End-to-end Tests - -on: - - pull_request - -env: - RAILS_ENV: development - DATABASE_URL: postgres://postgres:postgres@postgres:5432/gmmcalcombr_development - -jobs: - admin: - name: 'E2E: Admin' - 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 - - # languages - - 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/admin/**.js - 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 - path: spec/end-to-end/screenshots +# name: End-to-end Tests + +# on: +# - pull_request + +# env: +# RAILS_ENV: development +# DATABASE_URL: postgres://postgres:postgres@postgres:5432/gmmcalcombr_development + +# jobs: +# admin: +# name: 'E2E: Admin' +# 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 + +# # languages +# - 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/admin/**.js +# 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 +# path: spec/end-to-end/screenshots diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 4aba2ef15..cf6f8ec40 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -71,6 +71,7 @@ jobs: staging: name: 'Deploy: Staging' runs-on: ubuntu-20.04 + needs: docker_production if: github.event_name == 'push' diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 292a3be4c..e210fbeea 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,11 +1,19 @@ 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: + build: name: 'Docker: Build' runs-on: ubuntu-20.04 @@ -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 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b2ac66c0a..2c4938ad5 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,55 +1,16 @@ name: Lint on: - - pull_request + workflow_call: + inputs: + command: + type: string + required: true jobs: - bundler-audit: - name: 'Lint: Bundler Audit' + rubocop: 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 }} diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml new file mode 100644 index 000000000..11b7f9341 --- /dev/null +++ b/.github/workflows/pull_request.yml @@ -0,0 +1,117 @@ +name: Pipeline + +on: + - pull_request + +jobs: + build: + name: Build image + uses: gmmcal/gmmcal.com.br/.github/workflows/docker.yml@reusable + + rubocop: + name: 'Lint: Rubocop' + uses: gmmcal/gmmcal.com.br/.github/workflows/lint.yml@reusable + with: + command: bundle exec rubocop --config .rubocop.yml . + needs: build + + # reek: + # name: 'Lint: Reek' + # runs-on: ubuntu-20.04 + # needs: build + + # 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: build + + # steps: + # - name: Run brakeman + # run: | + # docker run --rm gmmcal/gmmcal:test bundle exec brakeman + + # scsslint: + # name: 'Lint: SCSSLint' + # runs-on: ubuntu-20.04 + # needs: build + + # 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: build + + # 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 + + # bundler-audit: + # name: 'Lint: Bundler Audit' + # 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 diff --git a/Dockerfile b/Dockerfile index ad6f37efb..9fc0221a1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -85,7 +85,7 @@ ENTRYPOINT ["/rails/bin/docker-entrypoint"] # Start the server by default, this can be overwritten at runtime EXPOSE 3000 -CMD ["./bin/rails", "server", "-b", "0.0.0.0"] +CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"] # Backend Test image FROM base as test diff --git a/bin/docker-entrypoint b/bin/docker-entrypoint index a07a6bf17..36c1a2b1a 100755 --- a/bin/docker-entrypoint +++ b/bin/docker-entrypoint @@ -1,12 +1,17 @@ #!/bin/bash -e -# If running the rails server then create or migrate existing database +# ./bin/rails server (executed on production image) if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then + ./bin/rails db:migrate +fi + +# bundle exec rails (executed on development image) +if [ "${3}" == "rails" ]; then ./bin/rails db:prepare ./bin/rails db:seed:all fi -# bundle exec rspec +# bundle exec rspec (executed on test image) if [ "${3}" == "rspec" ]; then ./bin/rails db:migrate fi diff --git a/render.yaml b/render.yaml index 90be3ddfc..5bd9905c9 100644 --- a/render.yaml +++ b/render.yaml @@ -20,6 +20,8 @@ services: pullRequestPreviewsEnabled: false plan: free region: frankfurt + domains: + - www.gustavocunha.dev envVars: - key: DATABASE_URL fromDatabase: @@ -34,12 +36,15 @@ services: - type: web name: gustavocunha-staging env: ruby - buildCommand: "bundle install; bundle exec rake assets:precompile; bundle exec rake assets:clean; bundle exec rake db:migrate db:seed:all db:cache:clear;" - startCommand: "bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-production}" + runtime: image + image: + url: gmmcal/gmmcal:latest autoDeploy: false pullRequestPreviewsEnabled: true plan: free region: frankfurt + domains: + - staging.gustavocunha.dev envVars: - key: DATABASE_URL fromDatabase: