Skip to content

Commit

Permalink
Merge pull request #169 from kmc-jp/container
Browse files Browse the repository at this point in the history
Build container image on push
  • Loading branch information
hanazuki authored Apr 5, 2024
2 parents e7e5448 + 062eb8b commit 35c42b8
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 34 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tmp/
node_modules/
vendor/
.bundle/
.git/

Dockerfile*
63 changes: 37 additions & 26 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,48 +1,59 @@
name: Test
name: Test & Build

on:
push:
branches:
- master
pull_request:
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
- uses: actions/setup-node@v1
ruby-version: .ruby-version
bundler-cache: true
- uses: actions/setup-node@v4
with:
node-version: '14'
- uses: actions/cache@v1
with:
path: vendor/bundle
key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gem-
- uses: actions/cache@v1
with:
path: node_modules
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- name: bundle install
node-version: lts/*
cache: npm
- name: Install npm modules
run: |
bundle config set path vendor/bundle
bundle config set deployment true
bundle install -j3
- name: npm install
run: |
npm install
npm ci
- name: Copy config
run: |
sed 's/development:/production:/' config/guildbook.yml.example > config/guildbook.yml
- name: Build
- name: Build assets
run: |
npm run build
- name: Test
run: |
bundle exec rspec
build:
needs:
- test
runs-on: ubuntu-latest
steps:
- id: meta
name: Docker meta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/${{ github.repository_owner }}/guildbook
tags: |
type=raw,value={{sha}}-{{date 'YYYYMMDDHHmmss'}}
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ github.token }}
- uses: docker/build-push-action@v5
with:
push: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
tags: ${{ steps.meta.output.tags }}
labels: ${{ steps.meta.outputs.labels }}
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2
42 changes: 42 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
ARG RUBY=public.ecr.aws/sorah/ruby:3.2-bookworm
ARG RUBYDEV=public.ecr.aws/sorah/ruby:3.2-dev-bookworm
ARG NODE=public.ecr.aws/docker/library/node:lts-bookworm-slim

###
FROM $RUBYDEV as bundle

WORKDIR /app
COPY Gemfile Gemfile.lock .
RUN bundle config set --local deployment true && \
bundle config set --local without test:development
RUN --mount=type=cache,target=/tmp/bundler-cache \
BUNDLE_GLOBAL_GEM_CACHE=/tmp/bundler-cache \
bundle install --retry=3

###
FROM $NODE as webpack

WORKDIR /app
COPY package.json package-lock.json .
RUN --mount=type=cache,target=/root/.npm \
npm ci

COPY . .

RUN npm run build

###
FROM $RUBY

RUN apt-get update -qq && \
apt-get install -y dumb-init && \
rm -rf /var/apt/lists/*

WORKDIR /app

COPY . .
COPY --from=bundle /app/vendor /app/vendor
COPY --from=bundle /app/.bundle /app/.bundle
COPY --from=webpack /app/node_modules /app/node_modules

ENTRYPOINT ["/app/docker/entrypoint.sh"]
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ gem 'haml', '~> 5.0'

gem 'net-ldap'

group :production do
gem 'unicorn'
end

group :test do
gem 'rspec'
end
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ GEM
haml (5.2.2)
temple (>= 0.8.0)
tilt
kgio (2.11.4)
multi_json (1.15.0)
mustermann (2.0.2)
ruby2_keywords (~> 0.0.1)
net-ldap (0.17.1)
rack (2.2.6.4)
rack-protection (2.2.4)
rack
raindrops (0.20.1)
rake (13.0.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
Expand Down Expand Up @@ -40,6 +42,9 @@ GEM
tilt (~> 2.0)
temple (0.10.0)
tilt (2.1.0)
unicorn (6.1.0)
kgio (~> 2.6)
raindrops (~> 0.7)
webrick (1.8.1)

PLATFORMS
Expand All @@ -53,6 +58,7 @@ DEPENDENCIES
rspec
sinatra (~> 2.2)
sinatra-contrib
unicorn
webrick

BUNDLED WITH
Expand Down
6 changes: 6 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/dumb-init bash

set -eo pipefail

cd /app
exec bundle exec unicorn "$@"
9 changes: 1 addition & 8 deletions webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs';
import glob from 'glob';
import path from 'path';
import yaml from 'js-yaml';

import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
Expand All @@ -22,14 +21,8 @@ declare module 'webpack' {
}


const loadSettings = (mode: string): Record<string, any> => {
const data: any = yaml.load(fs.readFileSync(path.join(__dirname, 'config/guildbook.yml'), 'utf-8'));
return (data.production || data.development) ? data[mode] : data;
}

const genConfig: webpack.ConfigurationFactory = (_env, argv) => {
const isProduction = argv.mode === 'production';
const settings = loadSettings(argv.mode || 'development');

const context = path.join(__dirname, 'javascript')
const entry = glob.sync(path.join(context, 'packs/*')).reduce((entry, pack) => {
Expand All @@ -40,7 +33,7 @@ const genConfig: webpack.ConfigurationFactory = (_env, argv) => {
};
}, {});

const publicPath = process.env.WEBPACK_DEV_SERVER_URL || settings.assets_uri || '/assets/';
const publicPath = process.env.WEBPACK_DEV_SERVER_URL || '/assets/';

const plugins: webpack.Configuration['plugins'] = [
new WebpackAssetsManifest({
Expand Down

0 comments on commit 35c42b8

Please sign in to comment.