This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
103 lines (95 loc) · 3.66 KB
/
ci_cd.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Adapted from:
# - https://dev.to/vvo/a-rails-and-postgresql-setup-for-github-actions-ci-nc8;
# - https://github.community/t/postgres-error-could-not-connect-to-server-no-such-file-or-directory/18040/4;
# - https://dokku.com/docs/deployment/continuous-integration/github-actions/;
# - https://kevsoft.net/2020/06/10/running-github-action-steps-and-jobs-only-on-push-to-master.html.
name: CI/CD
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_HOST: localhost
RAILS_ENV: test
SHOULD_DEPLOY: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
on:
push:
branches:
- master
pull_request:
jobs:
build:
runs-on: ubuntu-latest
# Similar to docker-compose.yml but not the same.
services:
postgres:
image: postgres:12.3-alpine
ports:
- 5432:5432
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
# Needed because the postgres container does not provide a healthcheck.
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
# `fromJSON` needed, here and below, to convert string environment
# variable (which is always truthy) to boolean.
- if: fromJSON(env.SHOULD_DEPLOY)
name: Cancel all but latest deploy
uses: styfle/[email protected]
with:
all_but_latest: true
access_token: ${{ github.token }}
- name: Checkout repository
uses: actions/checkout@v3
with:
# Required to avoid error as "pushing a git repository from a shallow
# clone does not work" - see https://github.com/dokku/github-action.
fetch-depth: 0
- name: Install Ruby version specified in `.ruby-version`
# This will use by default the .ruby-version file in your repository.
uses: eregon/use-ruby-action@master
- name: Install required apt packages
run: |
sudo apt-get -y install libpq-dev
- name: Setup cache key and directory for gems cache
uses: actions/cache@v3
with:
path: vendor/bundle
key: ${{ runner.os }}-gem-use-ruby-${{ hashFiles('**/Gemfile.lock') }}
- name: Read Node.js version to install from `.nvmrc`
run: echo "##[set-output name=NVMRC;]$(cat .nvmrc)"
id: nvm
- name: Install required Node.js version
uses: actions/setup-node@v3
with:
node-version: "${{ steps.nvm.outputs.NVMRC }}"
- name: Get Yarn cache directory path
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Setup cache key and directory for node_modules cache
uses: actions/cache@v3
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
- name: Bundle install
run: |
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3
- name: Yarn install
run: yarn --frozen-lockfile
- name: Run RSpec tests
run: |
bundle exec rake db:setup
bundle exec rspec
- name: Run `standard` for Ruby
run: bundle exec standardrb
- name: Smoke test `yarn run build`
run: yarn run build
- if: fromJSON(env.SHOULD_DEPLOY)
name: Push to dokku
uses: dokku/[email protected]
with:
# Always force push so deploy can work no matter what was previously
# deployed.
git_push_flags: "--force"
git_remote_url: "ssh://[email protected]/~/todotxt/"
ssh_private_key: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }}