Building a CICD pipeline using Docker (personal memorandum)

Git settings

1.Create a repository with any name
2.public settings
3.Click "create repository"
4.User Setting
-#Do it in the app directory
% git config user.name "user name"
% git config user.email “github registration email”
-#Confirm that the set user name and email are registered.
% git config user.name
% git config user.email
5.Linking remote repositories
%git remote add origin repository URL.git
-#Confirmation of linking
% git remote -v

Push to repository

% git add .
% git commit -m ‘first commit’
% git push origin master

Check if the app can be pushed to the repository.

Register with Travis CI

1.Click Sign in in the upper right
2.Click "SIGN IN WITH GIT HUB"
3.「Authorize travis-Click "ci"
4.Enter information on Github
5.left"+Click
6.Click on any repository to sync
7.Click "Dashbord" at the top and make sure you have the selected repository in "Active repositories".

Create .travis.yml

-#Create in the app's home directory
% vim .travis.yaml

yaml:.travis.yaml


#Run with sudo privileges
sudo: required
#Declaration of use of docker
services: docker

#Start container
before_install:
  - docker-compose up --build -d

script:
  #DB preparation
  - docker-compose exec —env 'RAILS_ENV=test' web rails db:create
  - docker-compose exec —env 'RAILS_ENV=test' web rails db:migrate
  #Run the test
  - docker-compose exec —env 'RAILS_ENV=test' web rails test

You need to push the code to github to get Travi CI working.

docker-compose.description of yml
version: '3'
#docker volume Data is saved here
volumes: 
  db-data:

services:
  web:
    build: .
    ports:
      - '3000:3000'
    volumes:
      - '.:/product-register'
#Environment variable settings of the container, originally the password should not be written directly.
    environment:
      - 'DATABASE_PASSWORD=postgres'
    tty: true
    stdin_open: true
#Created once the db service is created
    depends_on:
      - db
#You can access db from the web
    links:
      - db
#postgres container
  db:
    image: postgres
#Host db-Store data in data
    volumes:
      - 'db-data:/var/lib/postgresql/data'
    environment:
      - 'POSTGRES_USER=postgres'
      - 'POSTGRES_PASSWORD=postgres'
#Required if you want to run postgres on other than localhost
      - 'POSTGRES_HOST_AUTH_METHOD=trust'

push to git

% git add .
% git commit -m ‘update travis and compose’
% git push origin master

Here you can see the actual execution situation on the travis page.

Preparing for Heroku

1.log in
2.Click "create new app"
3.Enter any name in "APP name".
4.Click "Create app"
5.Click "Resources" when it is created.
6.「Add-Search for postgres with "ons". Select "Heroku Postgres"
7.Click "Provision".
8.Click "Settings", then click "Reveal Config Vars".
9.config/master.Enter the value of key in VALUE.
10.Enter "SECRET" for KEY_KEY_Enter "BASE".
11.Click "Add".
12.Click the "Deploy" tab.
13.Click on GitHub for "Deployment method"
14.Enter the name of the repository in "Connect to GitHub" and click "Search"
15.Click "Connet" in any repository
16.Wait for CI to pass before deploy in "Automatic deploys"(Deploy after passing CI)Check to.
17.Click "Enable Automatic Deploy"

Set the bottom of database.yml like this

config/databese.yml


# You can use this database configuration with:
#
production:
  url: <%= ENV['DATABASE_URL'] %>
#
# production:
#   <<: *default
#   database: product-register_production
#   username: product-register
#   password: <%= ENV['PRODUCT-REGISTER_DATABASE_PASSWORD'] %>

.travis.yml fix

yml:.travis.yml


#Run with sudo privileges
sudo: required
#Declaration of use of docker
services: docker

#Start container
before_install:
  - docker-compose up --build -d
#Log in to Heroku's Docker registry
  - docker login -u "$HEROKU_USERNAME" -p "$HEROKU_API_KEY" registry.heroku.com

script:
  #DB preparation
  - docker-compose exec --env 'RAILS_ENV=test' web rails db:create
  - docker-compose exec --env 'RAILS_ENV=test' web rails db:migrate
  #Run the test
  - docker-compose exec --env 'RAILS_ENV=test' web rails test

deploy:
  provider: script
  script:
    docker build -t registry.heroku.com/$HEROKU_API_NAME/web -f Dockerfile.prod .;
    docker push registry.heroku.com/$HEROKU_API_NAME/web;
    heroku run --app $HEROKU_API_NAME rails db:migrate;
  on:
    branch: master

If you add a comment out, you will see problems, so it is better not to add it as much as possible.

Set environment variables in Travis CI

-# Heroku-install cli
% brew tap heroku/brew && brew install heroku
-#Get Token
% heroku authorizations:create
  1. Open Travis CI and open the repository.
  2. Click "Settings" under "More options"
  3. Enter "HEROKU_USERNAME" in the NAME of "Environment Variables" and "_" in the VALUE. Click "Add". In the same way, enter "HEROKU_API_KEY" in NAME, enter the Token in VALUE, and click "Add". Also, enter the heroku repository name in "HEROKU_API_NAME".

Set environment variables on Heroku

  1. Open Heroku and click on any app
  2. Click the Settings tab and click Reveal Config Vars.
  3. Set "DATABASE_PASSWORD = postgres" of the db service environment set in docker-compose.yml as DATABASE_PASSWORD as KEY and postgres as VALUE, and click "Add".

Create Dockerfile.prod (for production environment)

% vim Dockerfile.prod

Dockerfile.prod


FROM ruby:2.5
RUN apt-get update && apt-get install -y \
    build-essential \
    libpq-dev \
    nodejs \
    postgresql-client \
    yarn
WORKDIR /product-register
COPY Gemfile Gemfile.lock /product-register/
RUN bundle install
#Move all the code in the current directory to the container
COPY . .
#Start rails server
CMD [ "rails", "s"]
% git add .
% git commit -m ’add deploy code’
% git push origin master

Actual development flow

  1. Create a branch% git checkout -b hoge
  2. Do the work
  3. Add the modified file add% git add.
  4. Commit your changes% git commit -m ‘message’
  5. Push% git push origin huge on the branch (Travis CI works)
  6. Creating a pull request
  7. Merge pull request (after Travis CI is complete)
  8. Travis CI works
  9. Deployed to Heroku.

Recommended Posts

Building a CICD pipeline using Docker (personal memorandum)
Creating a docker host on AWS using Docker Machine (personal memorandum)
CI/CD pipeline and Docker
Docker Machine (personal memorandum)
Data management using volume in Docker (personal memorandum)
Docker network (personal memorandum)
Data management using Docker bindmount and tmpfs (personal memorandum)
A memorandum when installing Docker and building a Linux container
Construction of data analysis environment using Docker (personal memorandum)
A memorandum of personal phpenv install
Launch docker container on EC2 (personal memorandum)
Building a Kotlin development environment using SDKMAN
[Personal memo] Writing a file using BufferedWriter
Docker memorandum
Docker memorandum
[Personal memo] Reading a file using BufferedReader
I made a bulletin board using Docker 1
A memorandum when building an environment with Ruby3.0 x Rails6.1 x Docker x CentOS Stream
Building an ML pipeline using Google Cloud Dataflow (1)
Create a Privoxy + Tor environment instantly using Docker
Build a Kotlin app using OpenJDK's Docker container
Django development environment construction using Docker-compose (personal memorandum)
Technical memorandum (Docker)
Memorandum docker command
A memo when building a Rails 5.2 development environment using Docker Desktop + WSL2 on Windows 10 Home
Docker command memorandum
A memorandum when trying to create a GUI using JavaFX
Try to build a Java development environment using Docker
[2021] Build a Docker + Vagrant environment for using React / TypeScript
A memorandum for creating an extended logger using org.slf4j.Logger