diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a5bf9c5..a0117b5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,4 +14,5 @@ jobs: - uses: ruby/setup-ruby@v1 with: bundler-cache: true - - run: bin/test + - run: | + docker run --rm -it --env PORT=8080 $(docker build . -q) bin/test diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d4d73d3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,57 @@ +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version +ARG RUBY_VERSION=3.2.3 +FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base + +# The app lives here +WORKDIR /app + +# Install base packages +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y \ + curl + +# Set production environment +ENV BUNDLE_DEPLOYMENT="1" \ + BUNDLE_PATH="/usr/local/bundle" \ + BUNDLE_WITHOUT="development" + +# Throw-away build stage to reduce size of final image +FROM base as build + +# Install build packages +RUN apt-get install --no-install-recommends -y \ + build-essential \ + git \ + pkg-config + +# Install application gems +COPY Gemfile Gemfile.lock .ruby-version ./ +RUN bundle install + +# Copy application code +COPY . . + +# Final stage for app image +FROM base + +ARG APPUSER=app +ARG APPDIR=/app + +# Clean up installation packages to reduce image size +RUN rm -rf /var/lib/apt/lists /var/cache/apt/archives + +# Copy built artifacts: gems, application +COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" +COPY --from=build $APPDIR $APPDIR + +# Run and own only the runtime files as a non-root user for security +RUN mkdir -p log tmp +RUN groupadd --system --gid 1000 $APPUSER +RUN useradd $APPUSER --uid 1000 --gid 1000 --create-home --shell /bin/bash +RUN chown -R $APPUSER:$APPUSER log tmp +USER 1000:1000 + +RUN gem install overman + +# Start the server by default, this can be overwritten at runtime +CMD ["overman", "start"] diff --git a/README.md b/README.md index a00369b..be8134b 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,26 @@ Returns a list of URLs. Start - foreman start +```bash +overman start + +# in docker +bin/docker-run +``` Test - bin/test +```bash +bin/test +# in docker +docker run --rm -it -e PORT=8080 $(docker build . -q) bin/test +``` ## Deploy - git push # to github +```bash +git push # to github +``` ## Example usage diff --git a/bin/docker-run b/bin/docker-run new file mode 100755 index 0000000..fe707fd --- /dev/null +++ b/bin/docker-run @@ -0,0 +1,10 @@ +#!/bin/bash + +TAG=${TAG:-urls} +PORT=${PORT:-8080} + +# echo commands +set -x + +docker build . -t $TAG +docker run --rm -it --publish ${PORT}:${PORT} --env PORT=${PORT} $TAG diff --git a/bin/test b/bin/test index dca7859..9d40113 100755 --- a/bin/test +++ b/bin/test @@ -3,6 +3,7 @@ # echo commands set -x +PORT=${PORT:-8080} RUBYOPT=-W:deprecated # info @@ -12,22 +13,22 @@ gem -v # install app dependencies bundle install -which foreman || gem install --no-document foreman +which overman || gem install --no-document overman # start app and wait for it -foreman start & +overman start & sleep 3 # run tests and save the result # tests Urls.by_url # can't use "localhost" because that's a NullURL in twingly/url -curl --silent --fail "http://localhost:5000/?url=localhost.v4.dentarg.net:5000" | grep -q "3 URLs for" +curl --silent --fail "http://localhost:${PORT}/?url=localhost.v4.dentarg.net:${PORT}" | grep -q "3 URLs for" test1=$? # tests Urls.by_text curl --silent --fail --data \ - 'text=http://www.example.org https://google.com blog.trello.com' "http://localhost:5000/text" | grep -q "3 URLs" + 'text=http://www.example.org https://google.com blog.trello.com' "http://localhost:${PORT}/text" | grep -q "3 URLs" test2=$? # stop app diff --git a/fly.toml b/fly.toml index c1a2a63..266a008 100644 --- a/fly.toml +++ b/fly.toml @@ -1,8 +1,5 @@ app = "urls" -[build] - builder = "heroku/buildpacks:20" - [env] PORT = "8080" RACK_ENV = "production"