Skip to content

Commit

Permalink
[#357] Fix: docker compose command is not working as expected (#504)
Browse files Browse the repository at this point in the history
* [#357] Use docker compose v2

debug

debug

Add log

* Update APP_NAME

* Update container name

Update container name

Update container name

* Update container name

* Replace app name with hyphen

* Use CONTAINERIZED_APP_NAME

* Update docker image name

* Update docker image

* Update APP NAME and using CONTAINERIZED_APP_NAME for testing

* Use `eslint.config.mjs` instead of oudated `.eslintignore`

* Use `ignores` key instead

* Use `APP_NAME` inside docker image
  • Loading branch information
sanG-github authored Apr 11, 2024
1 parent a63ef25 commit e1afdc2
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 52 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test_generated_app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: push

env:
COMPOSE_FILE: docker-compose.test.yml
APP_NAME: rails_templates
APP_NAME: rails-templates
DOCKER_IMAGE: ${{ github.repository }}
DOCKER_REGISTRY_HOST: ${{ secrets.DOCKER_REGISTRY_HOST }}
DOCKER_REGISTRY_USERNAME: ${{ github.repository_owner }}
Expand Down Expand Up @@ -86,7 +86,7 @@ jobs:
run: |
export BRANCH_TAG=${{ env.BRANCH_TAG }}-${{ matrix.variant }}
cd $APP_NAME
docker-compose pull test || true
docker compose pull test || true
- name: Build docker image
run: |
Expand All @@ -97,7 +97,7 @@ jobs:
run: |
export BRANCH_TAG=${{ env.BRANCH_TAG }}-${{ matrix.variant }}
cd $APP_NAME
docker-compose push test
docker compose push test
- name: Test the structure that is generated by the template
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_production_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Test Production Docker Build
on: push

env:
APP_NAME: rails_templates
APP_NAME: rails-templates
DOCKER_IMAGE: ${{ github.repository }}
DOCKER_REGISTRY_HOST: ${{ secrets.DOCKER_REGISTRY_HOST }}
RUBY_VERSION: 3.2.2
Expand Down
4 changes: 2 additions & 2 deletions .template/addons/docker/docker-compose.dev.yml.tt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3.2'
services:
db:
image: postgres:<%= POSTGRES_VERSION %>
container_name: <%= APP_NAME %>_db
container_name: <%= CONTAINERIZED_APP_NAME %>-db
environment:
- POSTGRES_DB=<%= APP_NAME %>_development
- POSTGRES_PASSWORD=postgres
Expand All @@ -12,6 +12,6 @@ services:

redis:
image: redis:<%= REDIS_VERSION %>
container_name: <%= APP_NAME %>_redis
container_name: <%= CONTAINERIZED_APP_NAME %>-redis
ports:
- "6379:6379"
6 changes: 3 additions & 3 deletions .template/addons/docker/docker-compose.test.yml.tt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3.2'
services:
db:
image: postgres:<%= POSTGRES_VERSION %>
container_name: <%= APP_NAME %>_db
container_name: <%= CONTAINERIZED_APP_NAME %>-db
environment:
- POSTGRES_DB=<%= APP_NAME %>_test
- POSTGRES_PASSWORD=postgres
Expand All @@ -12,7 +12,7 @@ services:

redis:
image: redis:<%= REDIS_VERSION %>
container_name: <%= APP_NAME %>_redis
container_name: <%= CONTAINERIZED_APP_NAME %>-redis
ports:
- "6379"

Expand All @@ -28,7 +28,7 @@ services:
- NODE_ENV=test
<%- end -%>
image: ${DOCKER_REGISTRY_HOST}/${DOCKER_IMAGE}:${BRANCH_TAG}-test
container_name: <%= APP_NAME %>_test
container_name: <%= CONTAINERIZED_APP_NAME %>-test
command: bin/test.sh
stdin_open: true
tty: true
Expand Down
6 changes: 3 additions & 3 deletions .template/addons/docker/docker-compose.yml.tt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ version: '3.2'
services:
db:
image: postgres:<%= POSTGRES_VERSION %>
container_name: <%= APP_NAME %>_db
container_name: <%= CONTAINERIZED_APP_NAME %>-db
environment:
- POSTGRES_DB=<%= APP_NAME %>_production
ports:
- "5432:5432"

redis:
image: redis:<%= REDIS_VERSION %>
container_name: <%= APP_NAME %>_redis
container_name: <%= CONTAINERIZED_APP_NAME %>-redis
ports:
- "6379:6379"

Expand All @@ -28,7 +28,7 @@ services:
- ASSET_HOST
<%- end -%>
image: ${DOCKER_REGISTRY_HOST}/${DOCKER_IMAGE}:${BRANCH_TAG}
container_name: <%= APP_NAME %>_web
container_name: <%= CONTAINERIZED_APP_NAME %>-web
command: bin/start.sh
ports:
- "${PORT}:${PORT}"
Expand Down
20 changes: 10 additions & 10 deletions .template/addons/github/.github/workflows/test.yml.tt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ env:
NODE_VERSION: 18
<%- end -%>

# Set the default docker-compose file
# Set the default docker compose file
COMPOSE_FILE: docker-compose.test.yml

# Set the default CI value despite the Github Action default value
Expand Down Expand Up @@ -46,13 +46,13 @@ jobs:

- name: Pull Docker image
if: ${{ env.BRANCH_TAG != 'latest' && env.BRANCH_TAG != 'develop' }}
run: docker-compose pull test || true
run: docker compose pull test || true

- name: Build Docker image
run: bin/docker-prepare && docker-compose build
run: bin/docker-prepare && docker compose build

- name: Push Docker image
run: docker-compose push test
run: docker compose push test

linting:
name: Linting
Expand All @@ -73,10 +73,10 @@ jobs:
password: ${{ env.DOCKER_REGISTRY_TOKEN }}

- name: Pull Docker image
run: docker-compose pull test || true
run: docker compose pull test || true

- name: Run codebase test
run: docker-compose run test -e DISABLE_SPRING=1 bundle exec rspec spec/codebase/codebase_spec.rb --format progress
run: docker compose run test -e DISABLE_SPRING=1 bundle exec rspec spec/codebase/codebase_spec.rb --format progress

unit_tests:
name: Unit tests
Expand All @@ -97,10 +97,10 @@ jobs:
password: ${{ env.DOCKER_REGISTRY_TOKEN }}

- name: Pull Docker image
run: docker-compose pull test || true
run: docker compose pull test || true

- name: Run unit tests
run: docker-compose run test -e DISABLE_SPRING=1 bundle exec rspec --exclude-pattern "spec/systems/**/*_spec.rb, spec/codebase/codebase_spec.rb" --profile --format progress
run: docker compose run test -e DISABLE_SPRING=1 bundle exec rspec --exclude-pattern "spec/systems/**/*_spec.rb, spec/codebase/codebase_spec.rb" --profile --format progress

- name: Upload tests coverage artifact
uses: actions/upload-artifact@v3
Expand Down Expand Up @@ -130,10 +130,10 @@ jobs:
password: ${{ env.DOCKER_REGISTRY_TOKEN }}

- name: Pull Docker image
run: docker-compose pull test || true
run: docker compose pull test || true

- name: Run system tests
run: docker-compose run test bundle exec rspec spec/systems --profile --format progress
run: docker compose run test bundle exec rspec spec/systems --profile --format progress

- name: Upload system tests screenshots artifact
uses: actions/upload-artifact@v2
Expand Down
2 changes: 1 addition & 1 deletion .template/addons/heroku/Dockerfile.web.tt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ARG DOCKER_REGISTRY_HOST=<%= DOCKER_REGISTRY_HOST %>
ARG DOCKER_IMAGE=<%= DOCKER_IMAGE %>
ARG DOCKER_IMAGE=<%= CONTAINERIZED_APP_NAME %>
ARG BRANCH_TAG=latest
FROM ${DOCKER_REGISTRY_HOST}/${DOCKER_IMAGE}:${BRANCH_TAG}

Expand Down
2 changes: 1 addition & 1 deletion .template/addons/heroku/Dockerfile.worker.tt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ARG DOCKER_REGISTRY_HOST=<%= DOCKER_REGISTRY_HOST %>
ARG DOCKER_IMAGE=<%= DOCKER_IMAGE %>
ARG DOCKER_IMAGE=<%= CONTAINERIZED_APP_NAME %>
ARG BRANCH_TAG=latest
FROM ${DOCKER_REGISTRY_HOST}/${DOCKER_IMAGE}:${BRANCH_TAG}

Expand Down
3 changes: 2 additions & 1 deletion .template/spec/support/serverspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module ServerSpecHelpers
# Prebuild and run docker image before running the test
# Because the docker api does not support docker compose
def self.test_container
container_id = `docker ps -qf "name=#{ENV.fetch('APP_NAME')}_test"`
container_name = "#{ENV.fetch('APP_NAME')}-test"
container_id = `docker ps -qf "name=#{container_name}"`

Docker::Container.get(container_id.strip)
end
Expand Down
2 changes: 1 addition & 1 deletion .template/spec/variants/web/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe 'Web variant - template' do
it 'creates the eslint configuration files' do
expect(file('.eslintrc')).to exist
expect(file('.eslintignore')).to exist
expect(file('eslint.config.mjs')).to exist
end

it 'creates the stylelint configuration files' do
Expand Down
18 changes: 0 additions & 18 deletions .template/variants/web/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
/node_modules/**
/config/**
/coverage/**
/public/**
/tmp/**
/vendor/**

/app/assets/builds/**

/app/javascript/channels/**
/app/javascript/translations/translations.js

/engines/*/node_modules/**
/engines/*/vendor/**
/engines/*/app/javascript/*/translations/translations.js

/docker-compose.yml
/docker-compose.test.yml
18 changes: 18 additions & 0 deletions .template/variants/web/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default {
ignores: [
"/node_modules/",
"/config/",
"/coverage/",
"/public/",
"/tmp/",
"/vendor/",
"/app/assets/builds/",
"/app/javascript/channels/",
"/app/javascript/translations/translations.js",
"/engines/*/node_modules/",
"/engines/*/vendor/",
"/engines/*/app/javascript/*/translations/translations.js",
"/docker-compose.yml",
"/docker-compose.test.yml"
]
}
2 changes: 1 addition & 1 deletion .template/variants/web/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def apply_web_variant!
use_source_path __dir__

template '.eslintrc.tt'
copy_file '.eslintignore'
copy_file 'eslint.config.mjs'
copy_file '.stylelintrc'
copy_file '.stylelintignore'

Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ create_api:
build:
cd $(APP_NAME) && \
bin/docker-prepare && \
docker-compose -f docker-compose.test.yml build
docker compose -f docker-compose.test.yml build

build_production:
cd $(APP_NAME) && \
bin/docker-prepare && \
docker-compose build
docker compose build

test_variant_app:
cd $(APP_NAME) && \
docker-compose -f docker-compose.test.yml run test
docker compose -f docker-compose.test.yml run test

base_addon_spec = spec/addons/base/**/*_spec.rb
web_addon_spec = spec/addons/variants/web/**/*_spec.rb
Expand All @@ -48,9 +48,9 @@ api_spec = spec/variants/api/**/*_spec.rb

test_template:
cd $(APP_NAME) && \
docker-compose -f docker-compose.test.yml up --detach db redis && \
docker-compose -f docker-compose.test.yml run test bash -c "./bin/inject_port_into_nginx.sh && nginx -c /etc/nginx/conf.d/default.conf -t" && \
docker-compose -f docker-compose.test.yml run --detach test bin/start.sh && \
docker compose -f docker-compose.test.yml up --detach db redis && \
docker compose -f docker-compose.test.yml run test bash -c "./bin/inject_port_into_nginx.sh && nginx -c /etc/nginx/conf.d/default.conf -t" && \
docker compose -f docker-compose.test.yml run --detach test bin/start.sh && \
cd ../.template && \
bundle install; \
if [ $(VARIANT) = web ]; then \
Expand Down
3 changes: 2 additions & 1 deletion template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

# Variables
APP_NAME = app_name
CONTAINERIZED_APP_NAME = app_name.tr('_', '-').downcase
# Transform the app name from slug to human-readable name e.g. nimble-web -> Nimble
APP_NAME_HUMANIZED = app_name.split(/[-_]/).map(&:capitalize).join(' ').gsub(/ Web$/, '')
DOCKER_REGISTRY_HOST = 'docker.io'
DOCKER_IMAGE = "nimblehq/#{APP_NAME}".freeze
DOCKER_IMAGE = "nimblehq/#{CONTAINERIZED_APP_NAME}".freeze
RUBY_VERSION = '3.2.2'
POSTGRES_VERSION = '15.2'
REDIS_VERSION = '6.2.7'
Expand Down

0 comments on commit e1afdc2

Please sign in to comment.