Skip to content

Commit 27f2a6c

Browse files
authored
Merge pull request #93 from infinum/feature/github-actions
Task #419 - GitHub Actions workflows
2 parents 2ae9364 + 3802732 commit 27f2a6c

File tree

7 files changed

+304
-39
lines changed

7 files changed

+304
-39
lines changed

.github/workflows/build.yml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
name: Build
2+
3+
concurrency:
4+
group: ${{ github.workflow }}-${{ github.ref }}
5+
cancel-in-progress: true
6+
7+
on:
8+
workflow_call:
9+
inputs:
10+
# Selects the version of Postgres for running tests
11+
# See: https://github.com/docker-library/docs/blob/master/postgres/README.md#supported-tags-and-respective-dockerfile-links
12+
postgres_image:
13+
required: true
14+
type: string
15+
16+
# Determines whether to install Node and run `yarn install`
17+
use_node:
18+
required: false
19+
type: boolean
20+
default: true
21+
22+
# Sets BUNDLE_APP_CONFIG environment variable
23+
# See: https://bundler.io/man/bundle-config.1.html
24+
bundle_app_config:
25+
required: false
26+
type: string
27+
default: .bundle/ci-build
28+
29+
# Selects the runner on which the workflow will run
30+
# See: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
31+
runner:
32+
required: false
33+
type: string
34+
default: ubuntu-20.04
35+
36+
# Defines which scripts will run on CI
37+
# Format: space-delimited paths to scripts
38+
# Example: 'bin/audit bin/lint bin/test'
39+
ci_steps:
40+
required: true
41+
type: string
42+
secrets:
43+
VAULT_ADDR:
44+
required: true
45+
VAULT_AUTH_METHOD:
46+
required: true
47+
VAULT_AUTH_USER_ID:
48+
required: true
49+
VAULT_AUTH_APP_ID:
50+
required: true
51+
52+
jobs:
53+
build:
54+
name: 'Build'
55+
runs-on: ${{ inputs.runner }}
56+
env:
57+
BUNDLE_APP_CONFIG: ${{ inputs.bundle_app_config }}
58+
RUBOCOP_CACHE_ROOT: .rubocop-cache
59+
services:
60+
postgres:
61+
image: postgres:${{ inputs.postgres_image }}
62+
env:
63+
POSTGRES_HOST_AUTH_METHOD: trust
64+
ports:
65+
- 5432:5432
66+
options: --name=postgres
67+
steps:
68+
- name: Git checkout
69+
uses: actions/checkout@v2
70+
- name: Set up Ruby
71+
uses: ruby/setup-ruby@v1
72+
with:
73+
bundler-cache: true
74+
- name: Prepare RuboCop cache
75+
uses: actions/cache@v2
76+
with:
77+
path: ${{ env.RUBOCOP_CACHE_ROOT }}
78+
key: ${{ runner.os }}-rubocop-cache-${{ github.sha }}
79+
restore-keys: |
80+
${{ runner.os }}-rubocop-cache-
81+
- name: Set up Node
82+
uses: actions/setup-node@v2
83+
if: ${{ inputs.use_node }}
84+
with:
85+
node-version-file: '.node-version'
86+
- name: Prepare node_modules cache
87+
uses: actions/cache@v2
88+
if: ${{ inputs.use_node }}
89+
with:
90+
path: node_modules
91+
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
92+
restore-keys: |
93+
${{ runner.os }}-modules-
94+
- name: Install JS packages
95+
if: ${{ inputs.use_node }}
96+
run: yarn install --frozen-lockfile
97+
- name: Prepare CI
98+
run: bin/prepare_ci
99+
env:
100+
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
101+
VAULT_AUTH_METHOD: ${{ secrets.VAULT_AUTH_METHOD }}
102+
VAULT_AUTH_USER_ID: ${{ secrets.VAULT_AUTH_USER_ID }}
103+
VAULT_AUTH_APP_ID: ${{ secrets.VAULT_AUTH_APP_ID }}
104+
- name: Wait for Postgres to be ready
105+
run: until docker exec postgres pg_isready; do sleep 1; done
106+
- name: CI steps
107+
run: 'parallel --lb -k -j0 ::: ${{ inputs.ci_steps }}'

.github/workflows/deploy.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Deploy
2+
3+
concurrency:
4+
group: ${{ github.workflow }}-${{ github.ref }}
5+
6+
on:
7+
workflow_call:
8+
inputs:
9+
# Sets the Mina environment (e.g. staging, production)
10+
# A task by the same name must exist in config/deploy.rb
11+
environment:
12+
required: true
13+
type: string
14+
15+
# Sets the Git branch which will be checked out
16+
branch:
17+
required: true
18+
type: string
19+
20+
# Determines who can manually trigger the workflow
21+
# Example: "@github_username1 @github_username2"
22+
# See: https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow
23+
deployers:
24+
required: false
25+
type: string
26+
default: ''
27+
28+
# Sets BUNDLE_APP_CONFIG environment variable
29+
# See: https://bundler.io/man/bundle-config.1.html
30+
bundle_app_config:
31+
required: false
32+
type: string
33+
default: .bundle/ci-deploy
34+
35+
# Selects the runner on which the workflow will run
36+
# See: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
37+
runner:
38+
required: false
39+
type: string
40+
default: ubuntu-20.04
41+
secrets:
42+
SSH_PRIVATE_KEY:
43+
required: true
44+
45+
jobs:
46+
deploy:
47+
name: Deploy
48+
runs-on: ${{ inputs.runner }}
49+
env:
50+
BUNDLE_APP_CONFIG: ${{ inputs.bundle_app_config }}
51+
if: ${{ github.event_name == 'workflow_dispatch' && contains(inputs.deployers, format('@{0}', github.actor)) || github.event.workflow_run.conclusion == 'success' }}
52+
steps:
53+
- uses: actions/checkout@v2
54+
with:
55+
ref: ${{ inputs.branch }}
56+
- uses: ruby/setup-ruby@v1
57+
with:
58+
bundler-cache: true
59+
- uses: webfactory/ssh-agent@v0.5.4
60+
with:
61+
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
62+
- run: bin/deploy ${{ inputs.environment }}

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ then run if needed:
2323
rbenv global #{latest_ruby}
2424
```
2525

26+
### GitHub Actions
27+
28+
This template uses GitHub Actions for CI/CD. In order for workflows to work properly some [secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets) have to be set up.
29+
30+
For build workflow to work, the following secrets must exist (usually set up by DevOps):
31+
- `VAULT_ADDR`
32+
- `VAULT_AUTH_METHOD`
33+
- `VAULT_AUTH_USER_ID`
34+
- `VAULT_AUTH_APP_ID`
35+
36+
For deploy workflows, you need to generate private/public SSH key pairs for each environment. Public key should be added to the server to which you're deploying. Private key should be added as a secret to GitHub and named `SSH_PRIVATE_KEY_#{ENVIRONMENT}`, where `ENVIRONMENT` is replaced with an appropriate environment name (`STAGING`, `PRODUCTION`, etc.).
37+
38+
### Frontend
39+
40+
If your application will have a frontend (the template will ask you that), you must have Node installed on your machine. The template creates a `.node-version` file with the Node version set to the version you're currently running (check by executing `node -v`). Therefore, ensure that you have the latest [Active LTS](https://nodejs.org/en/about/releases/) version of Node running on your machine before using the template.
41+
2642
## Usage
2743

2844
```shell

build.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Build
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
name: Build
8+
uses: infinum/default_rails_template/.github/workflows/build.yml@v1
9+
with:
10+
postgres_image: '13.2'
11+
use_node: false
12+
ci_steps: 'bin/audit bin/lint bin/test'
13+
secrets:
14+
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
15+
VAULT_AUTH_METHOD: ${{ secrets.VAULT_AUTH_METHOD }}
16+
VAULT_AUTH_USER_ID: ${{ secrets.VAULT_AUTH_USER_ID }}
17+
VAULT_AUTH_APP_ID: ${{ secrets.VAULT_AUTH_APP_ID }}

deploy-production.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Deploy production
2+
3+
on:
4+
workflow_dispatch:
5+
# workflow_run: # UNCOMMENT THIS IF YOU WANT AUTOMATIC PRODUCTION DEPLOYS
6+
# workflows: [Build]
7+
# branches: [master]
8+
# types: [completed]
9+
10+
jobs:
11+
deploy:
12+
name: Deploy
13+
uses: infinum/default_rails_template/.github/workflows/deploy.yml@v1
14+
with:
15+
environment: production
16+
branch: master
17+
deployers: 'DEPLOY USERS GO HERE' # Example: '@github_username1 @github_username2'
18+
secrets:
19+
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY_PRODUCTION }}

deploy-staging.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Deploy staging
2+
3+
on:
4+
workflow_dispatch:
5+
workflow_run:
6+
workflows: [Build]
7+
branches: [staging]
8+
types: [completed]
9+
10+
jobs:
11+
deploy:
12+
name: Deploy
13+
uses: infinum/default_rails_template/.github/workflows/deploy.yml@v1
14+
with:
15+
environment: staging
16+
branch: staging
17+
deployers: 'DEPLOY USERS GO HERE' # Example: '@github_username1 @github_username2'
18+
secrets:
19+
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY_STAGING }}

0 commit comments

Comments
 (0)