Skip to content

Commit

Permalink
Upgrade build to Shakapacker (#985)
Browse files Browse the repository at this point in the history
* Add shakapacker

* Update configuration to use Shakapacker

* Update Docker configuration so shakapacker compiles assets for production-ish environments

* Update compose.yaml to use prebuilt images

* Always configure terrastories.local for dev env

* Set protocol based on env

- https on production
- http on dev/offline/test

* Update CORS to always include HOST_HOSTNAME

* Update Dockerfile and GHA build to use root ctx

This changes the context of build from root/Rails to root. This requires
specifying a -f path/to/Dockerfile when building manually:

docker build -t terrastories-build -f rails/Dockerfile .

or specifying the `file` argument in the builder action.

* Remove unused common-app.yaml compose config file

* Update setup script

* Use pull cmd instead of up

---------

Co-authored-by: Laura Mosher <lauramosher@users.noreply.github.com>
  • Loading branch information
lauramosher and lauramosher committed Jan 9, 2024
1 parent 5cb6505 commit 9dbf454
Show file tree
Hide file tree
Showing 28 changed files with 3,599 additions and 4,178 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/on-merge-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v4
uses: docker/build-push-action@v5
with:
context: "{{defaultContext}}:rails"
context: .
file: "{context}/rails/Dockerfile"
target: devcore
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: true
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/on-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v4
uses: docker/build-push-action@v5
with:
context: "{{defaultContext}}:rails"
context: .
file: "{context}/rails/Dockerfile"
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: true
tags: |
Expand Down
45 changes: 26 additions & 19 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ if [ "$offline" = "y" ] || [ "$offline" = "Y" ]; then
echo "Choose one:"
echo " (1) Manual"
echo " (2) Automatic"
echo " (3) Skip configuration"
echo " (3) Skip configuration "
echo
read -p "Your choice: " choice
read -p "Your choice: [3] " -n 1 choice

if [ $choice -eq 2 ]; then
if [ "$choice" = "2" ]; then
echo
printf "Automatically configuring terrastories.local..."
sudo -- sh -c "echo '# Added by Terrastories\n127.0.0.1 terrastories.local\n# End of section' >> /etc/hosts"
echo -e "...${Green}done!${ColorOff}"
elif [ $choice -eq 1 ]; then
elif [ "$choice" = "1" ]; then
echo
echo "In a new terminal, open /etc/hosts with appropriate write permissions"
echo "and add the following line:"
Expand Down Expand Up @@ -109,11 +109,11 @@ echo " (1) Yes, local network access using my hostname: $hostname"
echo " (2) Yes, broader network access using a custom hostname"
echo " (3) No, I only plan on using my machine"
echo
read -p "Your choice: " addhosts
if [ $addhosts -eq 1 ]; then
read -p "Your choice: [3] " -n 1 addhosts
if [ "$addhosts" = "1" ]; then
grep -v "HOST_HOSTNAME" .env > .envnew; mv .envnew .env
echo "HOST_HOSTNAME=$hostname" >> .env
elif [ $addhosts -eq 2 ]; then
elif [ "$addhosts" = "2" ]; then
echo
read -p "Specify hostname: [$hostname] " hosts

Expand All @@ -127,30 +127,37 @@ fi
echo
echo "You may manage your hostname at any time in your .env file."

echo "Configuring and setting up your server…"

## pull images
docker compose pull --include-deps --quiet web

## init db
docker compose run --rm db postgres --version

## start db
docker compose up -d db

## build app
docker compose --profile $profile build

## setup database
docker compose --profile $profile run --rm $container bin/rails db:migrate

## Seed database with demo data
## Setup dev
if [[ "$profile" = "dev" ]]; then
docker compose --profile dev run --rm $container bin/rails db:seed
## Install dependencies
echo "Installing dependencies…"
docker compose run --rm $container yarn
docker compose run --rm $container bundle

## Setup database
echo "Setting up database…"
docker compose run --rm $container bin/rails db:migrate
docker compose run --rm $container bin/rails db:seed
fi

## Precompile assets for offline mode
## Setup offline
if [[ "$profile" = "offline" ]]; then
docker compose --profile offline run --rm -e RAILS_ENV=offline $container bin/rails assets:precompile
docker compose --profile $profile run --rm $container bin/rails db:migrate
fi

## run yarn
docker compose --profile $profile run --rm $container yarn
echo finishing up
docker compose --profile $container stop

echo
echo -e "${BoldGreen}Welcome to Terrastories!$ColorOff"
Expand Down
12 changes: 0 additions & 12 deletions common-app.yaml

This file was deleted.

47 changes: 26 additions & 21 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,26 @@ services:
web:
profiles:
- dev
extends:
file: common-app.yaml
service: rails
image: terrastories/devcore:latest
env_file:
- .env
command: scripts/server
restart: unless-stopped
stdin_open: true
tty: true
depends_on:
- db
ports:
- 3000:3000
- 80:3000 # Intended for use with HOST_HOSTNAME set
- 3000:3000 # Standard Rails development
- 3035:3035 # For Shakapacker/Webpack development server
expose:
- "80"
- "3000"
- "3035"
environment:
- CHECK_BUNDLER=yes
- RAILS_ENV=development
- RAILS_ENV=${RAILS_ENV-development}
volumes:
- bundler:/usr/local/bundle
- ./rails:/api
Expand All @@ -52,7 +60,6 @@ services:
- ./rails:/api
- ./data/media:/media
- ./data/import/media:/api/import/media
- ./map:/api/public/map:ro

nginx-proxy:
profiles:
Expand All @@ -64,6 +71,7 @@ services:
depends_on:
- localweb
- db
- tileserver
environment:
- DEFAULT_HOST=terrastories.local
volumes:
Expand All @@ -75,46 +83,43 @@ services:
localweb:
profiles:
- offline
extends:
file: common-app.yaml
service: rails
image: terrastories/terrastories:latest
env_file:
- .env
depends_on:
- db
ports:
- 3030:3030
expose:
- "3030"
volumes:
- bundler:/usr/local/bundle
- ./rails:/api
- ./data/media:/media
- ./data/import/media:/api/import/media
environment:
- RAILS_ENV=${RAILS_ENV-offline}
- RAILS_ENV=offline
- PORT=3030
- VIRTUAL_PORT=3030
- VIRTUAL_HOST=terrastories.local,${HOST_HOSTNAME-terrastories.local}
- VIRTUAL_PATH=/
- CHOKIDAR_USEPOLLING=true
- USE_LOCAL_MAP_SERVER=true
- OFFLINE_MAP_STYLE=/tileserver/styles/terrastories-map/style.json
- OFFLINE_MAP_STYLE=http://terrastories.local/tileserver/styles/terrastories-map/style.json
# DATABASE_URL is configured the same as config/database.yaml for backwards compatibility
# with existing servers that used to run using "development" mode, or offline with a full
# dev built image.
- DATABASE_URL=${DATABASE_URL-postgresql://postgres:postgres@db:5432/terrastories-dev}

tileserver:
profiles:
- offline
restart: unless-stopped
image: maptiler/tileserver-gl
command: "--port 8080 --public_url http://${HOST_HOSTNAME-terrastories.local}/tileserver/"
ports:
- 8080:8080
expose:
- "8080"
image: maptiler/tileserver-gl:v4.6.5
command: --public_url http://terrastories.local/tileserver/
env_file:
- .env
volumes:
- ./tileserver/data:/data
environment:
- VIRTUAL_HOST=terrastories.local,${HOST_HOSTNAME-terrastories.local}
- VIRTUAL_HOST=terrastories.local
- VIRTUAL_PATH=/tileserver/
- VIRTUAL_DEST=/
- VIRTUAL_PORT=8080
Expand Down
14 changes: 7 additions & 7 deletions rails/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ RUN apk --no-cache add --update \
ffmpeg \
poppler && \
gem update --system --no-document && \
gem install bundler --no-document --force
gem install bundler --no-document --force -v '~> 2.4.7'

WORKDIR /tmp

COPY Gemfile* /tmp/
COPY rails/Gemfile* /tmp/
RUN bundle config set --local path /usr/local/bundle && \
bundle config build.nokogiri --use-system-libraries && \
bundle install
Expand All @@ -43,10 +43,10 @@ WORKDIR /api
# == Transient step for building assets
FROM devcore as builder

COPY . /api
COPY ./rails /api

# So we can build assets for prod with dev dependencies
RUN bundle exec rake assets:precompile
RUN bin/rake assets:precompile

# == Final Production Image
#
Expand All @@ -71,19 +71,19 @@ RUN apk --no-cache add \
imagemagick \
ffmpeg \
poppler && \
gem update --system --no-document && \
gem install bundler --no-document --force

# Set deployment to true, bundled to vendor/bundle
RUN bundle config set deployment 'true'

# Copy sourcecode
COPY . /api
COPY ./rails /api

# Copy in bundled gems from devcore
COPY --from=devcore /usr/local/bundle /api/vendor/bundle
# Copy in precompiled assets from build
COPY --from=builder /api/public /api/public
COPY --from=builder /api/public/assets /api/public/assets
COPY --from=builder /api/public/packs /api/public/packs

WORKDIR /api

Expand Down
2 changes: 1 addition & 1 deletion rails/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ gem 'aws-sdk-s3', '~> 1.119.0'
gem 'active_storage_validations', '~> 1.0'

# Enable Webpack for javascript application code
gem 'webpacker', '~> 5.0'
gem 'shakapacker', '~> 7.1'
gem 'react-rails', '~> 2.6.2'

# Use ActiveStorage variant
Expand Down

0 comments on commit 9dbf454

Please sign in to comment.