diff --git a/.controlplane/Dockerfile b/.controlplane/Dockerfile index eca69f44f..223b1a8c3 100644 --- a/.controlplane/Dockerfile +++ b/.controlplane/Dockerfile @@ -1,39 +1,69 @@ -FROM ruby:3.1.2 +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile +ARG RUBY_VERSION=3.1.2 +FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base -RUN apt-get update +# Install packages needed to build gems and node modules +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y build-essential curl git libpq-dev libvips node-gyp pkg-config python-is-python3 -# install node and yarn -RUN curl -sL https://deb.nodesource.com/setup_18.x | bash -RUN apt-get install -y nodejs -RUN npm install -g yarn +# Install JavaScript dependencies +# Make sure NODE_VERSION matches the node version in .nvmrc and package.json +ARG NODE_VERSION=18.13.0 +ARG YARN_VERSION=1.22.19 +ENV PATH=/usr/local/node/bin:$PATH +RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \ + /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \ + npm install -g yarn@$YARN_VERSION && \ + rm -rf /tmp/node-build-master +# Rails app lives here WORKDIR /app -# install ruby gems -COPY Gemfile* ./ +# Set production environment +ENV RAILS_ENV="production" \ + BUNDLE_DEPLOYMENT="1" \ + BUNDLE_PATH="/usr/local/bundle" \ + BUNDLE_WITHOUT="development test" -RUN bundle config set without 'development test' && \ - bundle config set with 'staging production' && \ - bundle install --jobs=3 --retry=3 -# install node packages +# Throw-away build stage to reduce size of final image +FROM base as build + +# Install application gems +COPY Gemfile Gemfile.lock ./ +RUN bundle install && \ + rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git + +# Install node modules COPY package.json yarn.lock ./ -RUN yarn install +RUN yarn install --frozen-lockfile + +# Copy application code +COPY . . + +# Final stage for app image +FROM base -COPY . ./ +# Install packages needed for deployment +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y curl libvips postgresql-client && \ + rm -rf /var/lib/apt/lists /var/cache/apt/archives -ENV RAILS_ENV=production -ENV NODE_ENV=production +# Copy built artifacts: gems, application +COPY --from=build /usr/local/bundle /usr/local/bundle +COPY --from=build /app /app +ENV RAILS_ENV=production \ + NODE_ENV=production \ + SECRET_KEY_BASE=NOT_USED_NON_BLANK # compiling assets requires any value for ENV of SECRET_KEY_BASE -ENV SECRET_KEY_BASE=NOT_USED_NON_BLANK RUN yarn res:build -RUN rails react_on_rails:locale -RUN rails assets:precompile +RUN bin/rails react_on_rails:locale +RUN bin/rails assets:precompile # add entrypoint COPY .controlplane/entrypoint.sh ./ ENTRYPOINT ["/app/entrypoint.sh"] -CMD ["rails", "s"] +CMD ["rails", "server"] diff --git a/.controlplane/controlplane.yml b/.controlplane/controlplane.yml index f78265a62..960e61346 100644 --- a/.controlplane/controlplane.yml +++ b/.controlplane/controlplane.yml @@ -22,6 +22,9 @@ aliases: - redis - postgres + # To use the base image for a faster build process, uncomment this line: + # dockerfile: Dockerfile_from_base + apps: react-webpack-rails-tutorial: <<: *common diff --git a/.controlplane/entrypoint.sh b/.controlplane/entrypoint.sh index 6f07cc89b..0c6d050e7 100755 --- a/.controlplane/entrypoint.sh +++ b/.controlplane/entrypoint.sh @@ -1,5 +1,7 @@ -#!/bin/sh +#!/bin/bash -e # Runs before the main command +# This script is unique to this demo project as it ensures the database and redis are ready before running the rails server + wait_for_service() { @@ -18,8 +20,13 @@ echo " -- Waiting for services" wait_for_service $(echo $DATABASE_URL | sed -e 's|^.*@||' -e 's|/.*$||') wait_for_service $(echo $REDIS_URL | sed -e 's|redis://||' -e 's|/.*$||') -echo " -- Preparing database" -rails db:prepare +# If running the rails server then create or migrate existing database +if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then + echo " -- Preparing database" + ./bin/rails db:prepare +fi echo " -- Finishing entrypoint.sh, executing '$@'" + +# Run the main command exec "$@" diff --git a/.github/workflows/deploy-to-control-plane.yml b/.github/workflows/deploy-to-control-plane.yml new file mode 100644 index 000000000..175481d7e --- /dev/null +++ b/.github/workflows/deploy-to-control-plane.yml @@ -0,0 +1,53 @@ +# Control Plane GitHub Action + +name: Deploy-To-Control-Plane + +# Controls when the workflow will run +on: + # Uncomment the lines you want actions that will cause the workflow to Triggers the workflow on push or pull request events but only for the main branch + + # push: + # branches: [main] + # pull_request: + # branches: [main] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# Convert the GitHub secret variables to environment variables for use by the Control Plane CLI +env: + CPLN_ORG: ${{secrets.CPLN_ORG}} + CPLN_TOKEN: ${{secrets.CPLN_TOKEN}} + +jobs: + build-image-for-control-plane: + runs-on: ubuntu-latest + + steps: + - name: Check out the repo + uses: actions/checkout@v2 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.2' # Specify your Ruby version here + + - name: Install Control Plane CLI + shell: bash + run: | + sudo npm install -g @controlplane/cli + cpln --version + gem install cpl -v 1.1.2 + + - name: Set Short SHA + id: vars + run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)" + + - name: cpl profile + run: | + cpln profile update default --token ${CPLN_TOKEN} + + - name: cpl build-image + run: | + cpln image docker-login + cpl build-image -a ${{ secrets.APP_NAME_STAGING }} --commit ${{steps.vars.outputs.sha_short}} diff --git a/.overcommit.yml b/.overcommit.yml new file mode 100644 index 000000000..c2bc68656 --- /dev/null +++ b/.overcommit.yml @@ -0,0 +1,33 @@ +# Use this file to configure the Overcommit hooks you wish to use. This will +# extend the default configuration defined in: +# https://github.com/sds/overcommit/blob/master/config/default.yml +# +# At the topmost level of this YAML file is a key representing type of hook +# being run (e.g. pre-commit, commit-msg, etc.). Within each type you can +# customize each hook, such as whether to only run it on certain files (via +# `include`), whether to only display output if it fails (via `quiet`), etc. +# +# For a complete list of hooks, see: +# https://github.com/sds/overcommit/tree/master/lib/overcommit/hook +# +# For a complete list of options that you can use to customize hooks, see: +# https://github.com/sds/overcommit#configuration +# +# Uncomment the following lines to make the configuration take effect. + +#PreCommit: +# RuboCop: +# enabled: true +# on_warn: fail # Treat all warnings as failures +# +# TrailingWhitespace: +# enabled: true +# exclude: +# - '**/db/structure.sql' # Ignore trailing whitespace in generated files +# +#PostCheckout: +# ALL: # Special hook name that customizes all hooks of this type +# quiet: true # Change all post-checkout hooks to only display output on failure +# +# IndexTags: +# enabled: true # Generate a tags file with `ctags` each time HEAD changes diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 47853530f..000000000 --- a/Dockerfile +++ /dev/null @@ -1,27 +0,0 @@ -# TODO: Document where used -# Maybe outdated. -# Control Plane setup is in the .controlplane directory -FROM ruby:3.1.2 - -RUN apt-get update - -RUN curl -sL https://deb.nodesource.com/setup_18.x | bash -RUN apt-get install -y nodejs -RUN npm install -g yarn - -WORKDIR /app -COPY Gemfile* ./ - -RUN bundle install - -COPY package.json yarn.lock ./ -RUN yarn install - -COPY . ./ - -ENV RAILS_ENV=production -ENV NODE_ENV=production - -RUN rails react_on_rails:locale && rails assets:precompile - -CMD ["rails", "s"] diff --git a/Gemfile b/Gemfile index 2d997964a..ca55d2497 100644 --- a/Gemfile +++ b/Gemfile @@ -5,8 +5,6 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby "3.1.2" -gem "cpl", "~>1.1.2" - gem "react_on_rails", "13.4.0" gem "shakapacker", "7.1.0" diff --git a/Gemfile.lock b/Gemfile.lock index 088aec14e..3c2beb597 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -116,11 +116,6 @@ GEM term-ansicolor (~> 1.6) thor (>= 0.20.3, < 2.0) tins (~> 1.16) - cpl (1.1.2) - debug (~> 1.7.1) - dotenv (~> 2.8.1) - psych (~> 5.1.0) - thor (~> 1.2.1) crass (1.0.6) database_cleaner (2.0.2) database_cleaner-active_record (>= 2, < 3) @@ -135,7 +130,6 @@ GEM debug_inspector (1.1.0) diff-lcs (1.5.0) docile (1.4.0) - dotenv (2.8.1) drb (2.1.1) ruby2_keywords erubi (1.12.0) @@ -429,7 +423,6 @@ DEPENDENCIES capybara-screenshot coffee-rails coveralls_reborn (~> 0.25.0) - cpl (~> 1.1.2) database_cleaner debug (>= 1.0.0) factory_bot_rails diff --git a/bin/docker-entrypoint b/bin/docker-entrypoint new file mode 100755 index 000000000..67ef49314 --- /dev/null +++ b/bin/docker-entrypoint @@ -0,0 +1,8 @@ +#!/bin/bash -e + +# If running the rails server then create or migrate existing database +if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then + ./bin/rails db:prepare +fi + +exec "${@}" diff --git a/docs/maintainers.md b/docs/maintainers.md new file mode 100644 index 000000000..6396b65d4 --- /dev/null +++ b/docs/maintainers.md @@ -0,0 +1,22 @@ +# Guidelines for maintainers + +## Deployment on Control Plane + +The simple yet longer process of building and deploying this project on Control Plane is to use the default files in the `.controlplane` directory. +It is especially easier and more flexible for deployment during project development. + +To get a faster Docker image build process, +after each change on the project, +the maintainer should push a new image to the docker hub using the `Dockerfile_base` to update the base image. + +``` +docker build -f ./Dockerfile_base -t rwrt-base . +docker image tag rwrt-base:latest shakacode/rwrt-base:latest +docker image push shakacode/rwrt-base:latest +``` + +After successfully building the base image and pushing it to the Dockerhub, +build the project from `Dockerfile_from_base`. +This Dockerfile uses the base image from the Docker hub. +For this, uncomment the line for the custom Dockerfile in the `controlplane.yml` file. +Then run `cpln build-image ...` command. diff --git a/package.json b/package.json index cf2fdaff1..352f9b4eb 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "test:client": "cd client && yarn test", "build:test": "rm -rf public/packs-test && RAILS_ENV=test NODE_ENV=test bin/shakapacker", "build:dev": "rm -rf public/packs && RAILS_ENV=development NODE_ENV=development bin/shakapacker", - "build:clean": "rm -rf public/packs || true" + "build:clean": "rm -rf public/packs || true", + "build:prod": "rm -rf public/packs && RAILS_ENV=production NODE_ENV=production bin/shakapacker --profile --json" }, "dependencies": { "@babel/cli": "^7.21.0",

AltStyle によって変換されたページ (->オリジナル) /