diff --git a/.dockerignore b/.dockerignore index a955123..8a3ef0c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,7 @@ .git -test/** -spec/** tmp/** +coverage/** +docker/** .gitignore .hound.yml .rspec diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6360e8d..a4b4188 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,26 +1,34 @@ stages: - - test - build + - test - deploy -rails spec: - stage: test - image: ruby:2.6.2 - script: - - bundle install --path /cache - - bundle exec rails db:migrate RAILS_ENV=test - - bundle exec rails spec - -kaniko: +kaniko: + tags: + - docker stage: build image: name: gcr.io/kaniko-project/executor:debug entrypoint: [""] script: - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json - - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:latest --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG + - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/docker/production/Dockerfile --destination $CI_REGISTRY_IMAGE/production --destination $CI_REGISTRY_IMAGE/production/commits:$CI_COMMIT_SHA --destination $CI_REGISTRY_IMAGE/production/branches:$CI_COMMIT_REF_SLUG + - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/docker/development/Dockerfile --destination $CI_REGISTRY_IMAGE/development --destination $CI_REGISTRY_IMAGE/development/commits:$CI_COMMIT_SHA --destination $CI_REGISTRY_IMAGE/development/branches:$CI_COMMIT_REF_SLUG + - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/docker/test/Dockerfile --destination $CI_REGISTRY_IMAGE/test --destination $CI_REGISTRY_IMAGE/test/commits:$CI_COMMIT_SHA --destination $CI_REGISTRY_IMAGE/test/branches:$CI_COMMIT_REF_SLUG + +rails spec: + tags: + - docker + stage: test + image: $CI_REGISTRY_IMAGE/test/commits:$CI_COMMIT_SHA + script: + - cd /app + - bundle exec rails db:migrate + - bundle exec rails spec kubernetes: + tags: + - turniere-deploy stage: deploy only: - master diff --git a/README.md b/README.md index accf4b1..313266b 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,28 @@ Development (without mail confirmation and separate database): $ RAILS_ENV=development rails server ``` +## Docker +[Registry](https://gitlab.com/turniere/turniere-backend/container_registry) + +You can find all our Dockerfiles in the docker directory. +They depend on each other in the following order: `production` → `development` → `test` +This means, to build the `development` image, you have to build the `production` image first and tag it with the corresponding tag that is mentioned in the `FROM` line in the `development` Dockerfile. +To build all images do: + +```bash +cd turniere-backend +docker build -t registry.gitlab.com/turniere/turniere-backend/production -f docker/production/Dockerfile . +docker build -t registry.gitlab.com/turniere/turniere-backend/development -f docker/development/Dockerfile . +docker build -t registry.gitlab.com/turniere/turniere-backend/test -f docker/test/Dockerfile . +``` + +This is done to leave test and development dependencies out of the production container. +Also we have a dedicated test container which runs the tests reproducible when you start it, but can also run the normal rails server to somewhat debug problems occuring in the test suite if needed. + +While developing, if you want to use the development docker container, it should™ be sufficient to mount the root of this repository into the /app folder within the docker container to avoid building it over and over again. +**Only rebuilding the `development` container is not sufficient, as the `development` Dockerfile does not have a `COPY` Statement** + + ## Generate diagrams ```bash $ rails diagram:all_with_engines diff --git a/docker/development/Dockerfile b/docker/development/Dockerfile new file mode 100644 index 0000000..e78c64e --- /dev/null +++ b/docker/development/Dockerfile @@ -0,0 +1,6 @@ +# Build production container locally first with following tag +FROM registry.gitlab.com/turniere/turniere-backend/production +WORKDIR /app +RUN bundle install --deployment --with development +ENV RAILS_ENV development +CMD bundle exec rails db:migrate && bundle exec rails s -p 3000 -b 0.0.0.0 \ No newline at end of file diff --git a/Dockerfile b/docker/production/Dockerfile similarity index 57% rename from Dockerfile rename to docker/production/Dockerfile index d999999..4305b36 100644 --- a/Dockerfile +++ b/docker/production/Dockerfile @@ -1,8 +1,7 @@ FROM ruby:2.6.2-alpine -RUN apk add build-base tzdata sqlite sqlite-dev postgresql-dev && gem install tzinfo-data +RUN apk add --no-cache build-base tzdata sqlite sqlite-dev postgresql-dev && gem install tzinfo-data WORKDIR /app -COPY Gemfile /app/Gemfile -COPY Gemfile.lock /app/Gemfile.lock +COPY Gemfile* /app/ RUN bundle install --deployment --without development test COPY . /app ENV RAILS_ENV production diff --git a/docker/test/Dockerfile b/docker/test/Dockerfile new file mode 100644 index 0000000..e3a2b0f --- /dev/null +++ b/docker/test/Dockerfile @@ -0,0 +1,6 @@ +# Build development container locally first with following tag +FROM registry.gitlab.com/turniere/turniere-backend/development +WORKDIR /app +RUN bundle install --deployment --with test +ENV RAILS_ENV test +CMD bundle exec rails db:migrate && bundle exec rails spec \ No newline at end of file diff --git a/kubernetes/deployment.yml.erb b/kubernetes/deployment.yml.erb index f475c53..2e30c60 100644 --- a/kubernetes/deployment.yml.erb +++ b/kubernetes/deployment.yml.erb @@ -17,7 +17,7 @@ spec: spec: containers: - name: backend-container - image: registry.gitlab.com/turniere/turniere-backend:<%= current_sha %> + image: registry.gitlab.com/turniere/turniere-backend/production/commits:<%= current_sha %> ports: - containerPort: 3000 env: