Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build/001 #2

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Docker Image CI
name: Docker CI

on:
push:
Expand All @@ -21,38 +21,37 @@ jobs:
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-multi-buildx-${{ github.sha }}
path: ${{ runner.temp }}/.buildx-cache # Use GitHub Actions cache directory
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-multi-buildx
${{ runner.os }}-buildx-

- name: Build image
uses: docker/build-push-action@v5
with:
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
cache-from: type=local,src=${{ runner.temp }}/.buildx-cache
cache-to: type=local,mode=max,dest=${{ runner.temp }}/.buildx-cache-new
context: .
target: build
tags: ${{ env.TEST_IMAGE_TAG }}
outputs: type=docker,dest=/tmp/${{ env.TEMP_IMAGE_NAME }}.tar
outputs: type=docker,dest=${{ runner.temp }}/${{ env.TEMP_IMAGE_NAME }}.tar

- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
rm -rf ${{ runner.temp }}/.buildx-cache
mv ${{ runner.temp }}/.buildx-cache-new ${{ runner.temp }}/.buildx-cache

- name: Load built image
run: docker load -i /tmp/${{ env.TEMP_IMAGE_NAME }}.tar
run: docker load -i ${{ runner.temp }}/${{ env.TEMP_IMAGE_NAME }}.tar

- name: DB Test Prepare
- name: Run Tests
run: |
docker compose up -d database
docker compose run --rm -e RAILS_ENV=test web bin/rails db:test:prepare
docker compose run --rm -e RAILS_ENV=test web bin/rails spec

- name: Run Tests
run: docker compose run --rm -e RAILS_ENV=test web bin/rails spec

- name: Login to DockerHub
- name: Login to DockerHub (if main branch)
if: github.ref == 'refs/heads/main'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
Expand All @@ -62,11 +61,11 @@ jobs:
if: github.ref == 'refs/heads/main'
uses: docker/build-push-action@v5
with:
cache-from: type=local,src=${{ runner.temp }}/.buildx-cache
context: .
push: true
target: deployment
tags: xavius/wave-connect:${{ github.sha }}, xavius/wave-connect:latest
#repository: your-docker-hub-username/your-repository-name

deploy:
needs: build-and-test
Expand Down
25 changes: 14 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ WORKDIR /rails

# Install dependencies for both build and runtime
RUN apk add --no-cache \
libpq \
build-base \
libpq-dev \
libxml2 \
tzdata \
bash \
gcompat

# Set environment variables
ENV BUNDLE_PATH="/usr/local/bundle"
ENV NOKOGIRI_USE_SYSTEM_LIBRARIES=true
ENV BUNDLER_VERSION='2.4.10'
RUN gem install bundler -v ${BUNDLER_VERSION}


# Throw-away build stage to reduce size of final image
FROM base as build

# Install build dependencies and Node.js
RUN apk add --no-cache --virtual .build-deps \
build-base \
git \
libpq-dev \
libxml2-dev && \
Expand All @@ -29,12 +35,9 @@ RUN apk add --no-cache --virtual .build-deps \

# Copy Gemfile and Gemfile.lock before other files (leverage Docker cache)
COPY Gemfile Gemfile.lock ./

# Install application gems
RUN bundle install --jobs "$(nproc)" && \
rm -rf /usr/local/bundle/cache && \
find /usr/local/bundle/gems/ -name "*.c" -delete && \
find /usr/local/bundle/gems/ -name "*.o" -delete
RUN bundle install --jobs 2 --path="${BUNDLE_PATH}" && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile

# Copy application code
COPY . .
Expand All @@ -46,10 +49,10 @@ RUN bundle exec bootsnap precompile app/ lib/
RUN apk del .build-deps

# Final stage for app image
FROM base as deployment
FROM base

# Copy built artifacts: gems and application code
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails

# Ensure permissions are correct for non-root user
Expand Down
22 changes: 8 additions & 14 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
source "https://rubygems.org"

ruby "3.3.4"
ruby "3.3.5"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.1.3", ">= 7.1.3.3"
gem "rails", "~> 7.2.0"

gem "bootsnap", require: false
gem "cssbundling-rails"
Expand All @@ -17,20 +17,14 @@ gem "sprockets-rails"
gem "stimulus-rails"
gem "turbo-rails"


group :development, :test do
gem "debug", platforms: %i[ mri windows ]
gem "debug", platforms: %i[ mri windows ], require: "debug/prelude"
gem "brakeman", require: false
gem "rubocop-rails-omakase", require: false

# ADDED DEVELOPMENT GEMS
gem "rspec-rails"
gem "factory_bot_rails"
gem "faker"
end

group :development do
gem "web-console"

# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
# gem "rack-mini-profiler"

# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
end

Loading