From 24ff719f2a5ffa802369e5821e37e05dbf609245 Mon Sep 17 00:00:00 2001 From: Vladimir Bazhanov Date: Tue, 21 Jul 2020 14:43:41 +0300 Subject: [PATCH 1/5] Docker configs from graphql-api --- .dockerignore | 15 ++++++++++ Dockerfile | 61 ++++++++++++++++++++++++++++++++++++++++ bin/docker-entrypoint | 9 ++++++ bin/docker-sync | 7 +++++ docker-compose.linux.yml | 6 ++++ docker-compose.osx.yml | 10 +++++++ docker-compose.yml | 50 ++++++++++++++++++++++++++++++++ docker-sync.yml | 6 ++++ 8 files changed, 164 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100755 bin/docker-entrypoint create mode 100755 bin/docker-sync create mode 100644 docker-compose.linux.yml create mode 100644 docker-compose.osx.yml create mode 100644 docker-compose.yml create mode 100644 docker-sync.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..fa489420 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +.env + +/tmp/* +/log/*.log + +/vendor/bundle +.bundle + +.bash_history +.byebug_hist +.pry_history + +docker-compose.yml + +.semaphore-cache diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..43e862d8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,61 @@ +###################### +# Stage: Builder +FROM ruby:2.7.1-alpine as Builder + +ARG BUNDLER_VERSION + +RUN apk add --update --no-cache \ + build-base \ + postgresql-dev \ + git \ + nodejs \ + npm \ + imagemagick \ + tzdata + +# Remove bundle config if exist +RUN rm -f .bundle/config + +RUN gem install bundler:$BUNDLER_VERSION + +WORKDIR /app + +# Install gems +ARG BUNDLE_WITHOUT +ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT} + +RUN bundle config set without ${BUNDLE_WITHOUT} + +COPY Gemfile* /app/ +RUN bundle install -j4 --retry 3 \ + # Remove unneeded files (cached *.gem, *.o, *.c) + && rm -rf /usr/local/bundle/cache/*.gem \ + && find /usr/local/bundle/gems/ -name "*.c" -delete \ + && find /usr/local/bundle/gems/ -name "*.o" -delete + +RUN npm install -g yarn && yarn install + +# Add the Rails app +COPY . /app/ + +# Remove folders not needed in resulting image +ARG FOLDERS_TO_REMOVE +RUN rm -rf $FOLDERS_TO_REMOVE + +############################### +# Stage Final +FROM ruby:2.6.6-alpine as Final + +# Add Alpine packages +RUN apk add --update --no-cache \ + postgresql-client \ + imagemagick \ + tzdata \ + file \ + git + +# Copy app with gems from former build stage +COPY --from=Builder /usr/local/bundle/ /usr/local/bundle/ +COPY --from=Builder /app/ /app/ + +WORKDIR /app diff --git a/bin/docker-entrypoint b/bin/docker-entrypoint new file mode 100755 index 00000000..ef132805 --- /dev/null +++ b/bin/docker-entrypoint @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e + +if [ -f tmp/pids/server.pid ]; then + rm tmp/pids/server.pid +fi + +bundle exec rails server -b 0.0.0.0 diff --git a/bin/docker-sync b/bin/docker-sync new file mode 100755 index 00000000..192ce520 --- /dev/null +++ b/bin/docker-sync @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +set -e + +gem install docker-sync +docker-sync start +cp docker-compose.osx.yml docker-compose.override.yml diff --git a/docker-compose.linux.yml b/docker-compose.linux.yml new file mode 100644 index 00000000..05da2d62 --- /dev/null +++ b/docker-compose.linux.yml @@ -0,0 +1,6 @@ +version: "3.4" + +services: + app: + volumes: + - .:/app diff --git a/docker-compose.osx.yml b/docker-compose.osx.yml new file mode 100644 index 00000000..a66f3b55 --- /dev/null +++ b/docker-compose.osx.yml @@ -0,0 +1,10 @@ +version: "3.4" + +services: + app: + volumes: + - app-files:/app:nocopy + +volumes: + app-files: + external: true diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..00a31bd9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,50 @@ +version: "3.4" + +x-app: &app_base + depends_on: + - db + image: ${IMAGE_NAME} + environment: + - DATABASE_URL=postgres://postgres:password@db + - RAILS_ENV + - RACK_ENV + - DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL + - AUTH_SECRET_TOKEN + - MAILER_SENDER_ADDRESS + - PASSWORD_RECOVERY_LINK_TEMPLATE + build: + context: . + args: + - BUNDLE_WITHOUT="${BUNDLE_WITHOUT}" + - BUNDLER_VERSION=2.1.4 + - FOLDERS_TO_REMOVE="" + links: + - db + volumes: + - ruby-bundle:/usr/local/bundle + +services: + db: + image: postgres:12-alpine + environment: + - POSTGRES_PASSWORD=password + ports: + - "5432:5432" + volumes: + - db-data:/var/lib/postgresql/data + mailcatcher: + image: yappabe/mailcatcher + ports: + - "1025:1025" + - "1080:1080" + app: + <<: *app_base + depends_on: + - mailcatcher + ports: + - "3000:3000" + command: bin/docker-entrypoint + +volumes: + ruby-bundle: + db-data: diff --git a/docker-sync.yml b/docker-sync.yml new file mode 100644 index 00000000..31baf7fa --- /dev/null +++ b/docker-sync.yml @@ -0,0 +1,6 @@ +version: "2" +syncs: + app-files: + notify_terminal: true + src: './' + sync_excludes: ['.git', '.bundle'] From da2a9129650d0baf791d604bc4ace8d909857bcd Mon Sep 17 00:00:00 2001 From: Vladimir Bazhanov Date: Tue, 21 Jul 2020 14:52:51 +0300 Subject: [PATCH 2/5] Semaphore config --- .semaphore/semaphore.yml | 84 +++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/.semaphore/semaphore.yml b/.semaphore/semaphore.yml index 6edad1a3..8c93dcfa 100644 --- a/.semaphore/semaphore.yml +++ b/.semaphore/semaphore.yml @@ -11,74 +11,68 @@ execution_time_limit: auto_cancel: queued: - when: 'true' + when: "true" fail_fast: stop: - when: 'true' + when: "true" global_job_config: - env_vars: - - name: RAILS_ENV - value: 'test' - - name: DATABASE_URL - value: 'postgresql://postgres@localhost/test?encoding=utf8' - - name: BUNDLE_ARGS - value: '--deployment --without development staging production --jobs 4' - prologue: commands: + # Setup dynamic environment variables b/c they do not support via env_vars yet + - export DOCKER_REPO="docker.pkg.github.com/fs/rails-base-graphql-api" + - export BUILDER_NAME="${DOCKER_REPO}/builder:${SEMAPHORE_GIT_BRANCH}" + - export IMAGE_NAME="${DOCKER_REPO}/final:${SEMAPHORE_GIT_BRANCH}" + - export RAILS_ENV="test" + - export RACK_ENV="test" + - export BUNDLE_WITHOUT="development staging production" + - export DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL="true" + - export MAILER_SENDER_ADDRESS="noreply@example.com" + + # Authenticate with DockerHub + - echo "${DOCKER_PASSWORD}" | docker login https://docker.pkg.github.com -u "${DOCKER_USERNAME}" --password-stdin + - checkout - - cache restore - - cp .env.example .env blocks: - - name: Setup + - name: Build task: + secrets: + - name: github-docker-secrets jobs: - - name: Bundle - commands: - - bundle install ${BUNDLE_ARGS} - - cache store + - name: Docker build + commands: + - docker pull "${BUILDER_NAME}" || true + - docker pull "${IMAGE_NAME}" || true + - docker build -t "${BUILDER_NAME}" --target Builder --cache-from="${BUILDER_NAME}" --build-arg BUNDLE_WITHOUT="${BUNDLE_WITHOUT}" --build-arg BUNDLER_VERSION=2.1.4 . + - docker build -t "${IMAGE_NAME}" --target Final --cache-from="${BUILDER_NAME}" --cache-from="${IMAGE_NAME}" --build-arg BUNDLE_WITHOUT="${BUNDLE_WITHOUT}" --build-arg BUNDLER_VERSION=2.1.4 . + - docker push "${BUILDER_NAME}" + - docker push "${IMAGE_NAME}" - - name: Quality + - name: Run task: - jobs: - - name: Quality - commands: - - bundle install ${BUNDLE_ARGS} --local - - bin/quality + secrets: + - name: github-docker-secrets - - name: Test - task: prologue: commands: - - nvm use - - sem-version ruby 2.5.7 - - bundle install ${BUNDLE_ARGS} --local - - sem-service start postgres - - bin/rails db:setup - - bin/rails assets:precompile + - docker pull "${IMAGE_NAME}" + - cp docker-compose.linux.yml docker-compose.override.yml + - docker-compose up --detach jobs: - - name: Unit + - name: Run RSpec commands: - - bin/rspec --tag ~type:feature + - docker-compose exec app bin/rails db:create db:schema:load + - bin/tests - - name: Features + - name: Run Quality commands: - - bin/rspec --tag type:feature - - # - name: Jasmine - # commands: - # - bundle exec rails jasmine:ci + - bin/quality promotions: - name: Deploy to Heroku pipeline_file: heroku.yml - - # Continuous deployment from master branch - auto_promote_on: - - result: passed - branch: - - master + auto_promote: + when: "result = 'passed' and branch = 'master'" From b52b1c925ca034c03c9d4c99f2a78a0a9eef9fc0 Mon Sep 17 00:00:00 2001 From: Vladimir Bazhanov Date: Tue, 21 Jul 2020 14:56:46 +0300 Subject: [PATCH 3/5] name fix --- .semaphore/semaphore.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.semaphore/semaphore.yml b/.semaphore/semaphore.yml index 8c93dcfa..9b9f28d7 100644 --- a/.semaphore/semaphore.yml +++ b/.semaphore/semaphore.yml @@ -21,7 +21,7 @@ global_job_config: prologue: commands: # Setup dynamic environment variables b/c they do not support via env_vars yet - - export DOCKER_REPO="docker.pkg.github.com/fs/rails-base-graphql-api" + - export DOCKER_REPO="docker.pkg.github.com/fs/rails-base" - export BUILDER_NAME="${DOCKER_REPO}/builder:${SEMAPHORE_GIT_BRANCH}" - export IMAGE_NAME="${DOCKER_REPO}/final:${SEMAPHORE_GIT_BRANCH}" - export RAILS_ENV="test" From 906171ba7852e4f03f0389746870b4a7ced43ab1 Mon Sep 17 00:00:00 2001 From: Vladimir Bazhanov Date: Tue, 21 Jul 2020 15:20:12 +0300 Subject: [PATCH 4/5] Fixes for version number --- Dockerfile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 43e862d8..6b370567 100644 --- a/Dockerfile +++ b/Dockerfile @@ -44,7 +44,7 @@ RUN rm -rf $FOLDERS_TO_REMOVE ############################### # Stage Final -FROM ruby:2.6.6-alpine as Final +FROM ruby:2.7.1-alpine as Final # Add Alpine packages RUN apk add --update --no-cache \ @@ -52,7 +52,9 @@ RUN apk add --update --no-cache \ imagemagick \ tzdata \ file \ - git + git \ + nodejs \ + npm # Copy app with gems from former build stage COPY --from=Builder /usr/local/bundle/ /usr/local/bundle/ From e1cecdd4226a74bc8c506d3a336d2d1e69deb742 Mon Sep 17 00:00:00 2001 From: Vladimir Bazhanov Date: Wed, 22 Jul 2020 14:02:29 +0300 Subject: [PATCH 5/5] Secrets added --- .semaphore/semaphore.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.semaphore/semaphore.yml b/.semaphore/semaphore.yml index 9b9f28d7..fd5d97ee 100644 --- a/.semaphore/semaphore.yml +++ b/.semaphore/semaphore.yml @@ -31,7 +31,7 @@ global_job_config: - export MAILER_SENDER_ADDRESS="noreply@example.com" # Authenticate with DockerHub - - echo "${DOCKER_PASSWORD}" | docker login https://docker.pkg.github.com -u "${DOCKER_USERNAME}" --password-stdin + - echo "${GITHUB_TOKEN}" | docker login https://docker.pkg.github.com -u "${GITHUB_USERNAME}" --password-stdin - checkout @@ -39,7 +39,7 @@ blocks: - name: Build task: secrets: - - name: github-docker-secrets + - name: semaphore-github-packages-secrets jobs: - name: Docker build commands: