Codeship Using Docker and Codeship For Ruby Development
Codeship Using Docker and Codeship For Ruby Development
-2-
Share this
Codeship Guid
e
The first part of this eBook will cover developing and testing
a Ruby app with Docker compose. The second part will
focus on how to further create a CI/CD pipeline for your app
using Codeship.
-3-
Share this
Codeship Guid
e
-4-
Share this
Codeship Guid
e
1 FROM ruby:2.4.0-alpine
-5-
Share this
Codeship Guid
e
1 FROM ruby:2.4.0-alpine
2
3 RUN apk update && apk add nodejs build-base libxml2-dev libxslt-dev postgresql
postgresql-dev
4 RUN mkdir /app
5 WORKDIR /app
CODE
6
7 COPY Gemfile ./Gemfile
8 COPY Gemfile.lock ./Gemfile.lock
9
10 RUN bundle install -j 20
11 COPY . .
-6-
Share this
Codeship Guid
e
You can now run docker build . again and see the
results:
-7-
Share this
Codeship Guid
e
34 ## Gems Installed ##
35
36 Bundle complete! 16 Gemfile dependencies, 70 gems now installed.
37 Bundled gems are installed into /usr/local/bundle.
38 ---> e78a78f6e485
39 Removing intermediate container f15edb779ff4
40 Step 8/8 : COPY . .
41 ---> f4178bf3a235
42 Removing intermediate container 4cfe9df1be1f
43 Successfully built f4178bf3a235
-8-
Share this
Codeship Guid
e
1 version: '2'
2 services:
3 web:
CODE
4 build: .
5 command: bundle exec rails s -b 0.0.0.0
6 volumes:
-9-
Share this
Codeship Guid
e
7 - .:/app
8 ports:
9 - "3000:3000"
10 links:
11 - postgres
12 environment:
CODE
13 DATABASE_URL: postgres://todoapp@postgres/todos
14 postgres:
15 image: postgres:9.6.2-alpine
16 environment:
17 POSTGRES_USER: todoapp
18 POSTGRES_DB: todos
- 10 -
Share this
Codeship Guid
e
- 11 -
Share this
Codeship Guid
e
- 12 -
Share this
Codeship Guid
e
This will tell you the name of the services, the command
used to start it, its current state, and the ports. Notice
rubyrailstodoapp_web_1 has listed the port as
0.0.0.0:3000->3000/tcp . This tells us that you can
access the application using localhost:3000/todos on
the host machine.
- 13 -
Share this
Codeship Guid
e
4 -- create_table(:todos)
5 -> 0.0220s
6 == 20170321180544 CreateTodos: migrated (0.0221s) =============================
2
3 []
The schema and all of the data in the container will persist
as long as the postgres:9.6.2-alpine image is not
removed. Eventually, however, it would be good to check
how your app will build with a clean setup. You can run
docker-compose down , which will clear things that are
built and let you see what is happening with a fresh start.
Feel free to check out the source code, play around a bit,
and see how things go for you.
- 14 -
Share this
Codeship Guid
e
1 /> docker-compose -p tests run -p 3000 --rm web bundle exec rspec
2 ...............
CODE
3
4 Finished in 1.9 seconds (files took 5.29 seconds to load)
5 15 examples, 0 failures
- 15 -
Share this
Codeship Guid
e
- 16 -
Share this
Codeship Guid
e
Codeship account
Codeship Jet CLI
Heroku account
GitHub, Bitbucket, or GitLab account
- 17 -
Share this
Codeship Guid
e
- 18 -
Share this
Codeship Guid
e
1 version: '2'
2 services:
3 web:
4 build: ./
5 links:
6 - postgres
7 environment:
CODE
8 DATABASE_URL: postgres://todoapp@postgres/todos
9 cached: true
10 postgres:
11 image: postgres:9.6.2-alpine
12 environment:
13 POSTGRES_USER: todoapp
14 POSTGRES_DB: todos
15 cached: true
- 19 -
Share this
Codeship Guid
e
1 - type: parallel
2 steps:
3 - name: rspec
4 service: web
CODE
5 command: rspec
6 - name: rubocop
7 service: web
8 command: rubocop
Bin scripts
Now that things are more automated, we end up
introducing a small race case that we can easily get
around with some simple scripts. As the project is built,
the integration tests require that Postgres is running
to perform the migrations. We can't guarantee that will
happen with the codeship-services.yml file alone. One
approach is to check that the database is available before
starting the migration.
- 20 -
Share this
Codeship Guid
e
wait-for postgres
The contents of the wait-for-postgres script are the
following:
1 #!/bin/sh
2 # wait-for-postgres
3 set -e
4
5 TIMEOUT=60
6 COUNT=0
7
CODE
- 21 -
Share this
Codeship Guid
e
1 RUN apk update && apk add nodejs build-base libxml2-dev libxslt-dev postgresql
CODE
postgresql-dev
ci
The ci script is as follows:
1 #!/bin/sh
2 # Usage: bin/ci [setup]
3 set -e
4
5 bin/wait-for-postgres
CODE
6
7 time bundle exec rake db:create
8 time bundle exec rake db:migrate
9 time bundle exec rake db:seed
10
11 time bundle exec rspec $2
Not too much heavy lifting here. This script runs the
wait-for-postgres script to finish up, then it will
perform the migration, and the last line takes the first
parameter and will run that. This allows me to change a
line in codeship-steps.yml to run this script first.
1 - name: rspec
CODE
2 service: web
3 command: bin/ci rspec # you only need to change the command here
- 22 -
Share this
Codeship Guid
e
If you see the final result above, you have set everything
up correctly. If you receive instead type:STEP_FINISHED_
TYPE_ERROR , something didn't go right and you should
check some things. Codeship Jet CLI will produce
a log that you can review to try to locate the problem.
- 23 -
Share this
Codeship Guid
e
Create project
When your repo is ready to go, you can now set up the
Codeship project.
- 24 -
Share this
Codeship Guid
e
- 25 -
Share this
Codeship Guid
e
- 26 -
Share this
Codeship Guid
e
- 27 -
Share this
Codeship Guid
e
Make sure you have added all of your files to your repo.
Review your codeship-services.yml and
codeship-steps.yml files.
Run Codeship Jet CLI locally to double-check it works
locally.
- 28 -
Share this
Codeship Guid
e
1. Click Resources.
2. Under Add-ons, search for postgres .
3. In the results dropdown, click Heroku Postgres.
4. Leave the selection as Hobby Dev Free, then click
Provision.
- 29 -
Share this
Codeship Guid
e
1. Click Projects, then the project you are currently working in.
2. Click Settings, then click General.
3. Locate and copy the AES Key .
- 30 -
Share this
Codeship Guid
e
NOTE: Make sure that both codeship.aes and deployment.env are ignored in
your .gitignore file. Since they contain sensitive data, you don't want these to be
pushed into your repository.
1 version: '2'
2 services:
CODE
3 web:
4 build: ./
5 links:
- 31 -
Share this
Codeship Guid
e
6 - postgres
7 environment:
8 DATABASE_URL: postgres://todoapp@postgres/todos
9 cached: true
10 postgres:
11 image: postgres:9.6.2-alpine
12 environment:
CODE
13 POSTGRES_USER: todoapp
14 POSTGRES_DB: todos
15 cached: true
16 deploy:
17 image: codeship/heroku-deployment
18 encrypted_env_file: deployment.env.encrypted
19 volumes:
20 - ./:/deploy
1 - type: parallel
2 steps:
3 - name: rspec
4 service: web
5 command: bin/ci rspec
CODE
6 - name: rubocop
7 service: web
8 command: rubocop
9 - name: deploy #step added
10 tag: master
- 32 -
Share this
Codeship Guid
e
11 service: deploy
12 command: codeship_heroku deploy /deploy ruby-rails-todoapp
13 - name: migrate #step added
CODE
14 tag: master
15 service: deploy
16 command: heroku run --app ruby-rails-todoapp -- bundle exec rake db:migrate
- 33 -
Share this
Codeship Guid
e
1 git add .
CODE
- 34 -
Share this
Codeship Guid
e
Conclusion
Docker
Docker Community Edition
Dockerfile Reference
Docker Compose Overview
Docker Compose CLI
- 35 -
Share this
Codeship Guid
e
Codeship Pro
Codeship Pro Documentation
Codeship Jet CLI Docs
Encrypting Environment Variables
Codeship Steps Docs
Codeship Services Docs
Article Resources
Using Docker Compose for Ruby Development
Ruby on Rails Todo App Repo
Alpine Based Docker Images Make a Difference in Real
World Apps
- 36 -
Share this
Codeship Guid
e
- 37 -
Share this
Codeship Guid
e
About Codeship.
Codeship is a hosted Continuous Integration service that fits all your needs.
Codeship Basic provides pre-installed dependencies and a simple setup UI
that let you incorporate CI and CD in only minutes. Codeship Pro has native
Docker support and gives you full control of your CI and CD setup while
providing the convenience of a hosted solution.