SlideShare a Scribd company logo
1 of 26
Download to read offline
GitLab + Dokku で作る CI/
CD 環境
Kazuhiro NISHIYAMA
第78回 Ruby関西 勉強会
2017/07/29
Powered by Rabbit 2.2.0
自己紹介
西山和広
id:znz (github, twitter など)
Ruby コミッター
1/25
GitLab + Dokku
GitLab
GitLab CI
Dokku
(+ Heroku)
2/25
GitLab とは?
簡単にいえば OSS の GitHub クローンのよ
うなもの
Git ホスティング
Merge Request (GitHub の Pull Request)
Issue 管理など色々
GitHub にない機能もある
https://about.gitlab.com/features/
3/25
GitLab CI とは?
Continuous Integration/Continuous Delivery
(CI/CD)
Jenkins のジョブを実行しないマスターのよ
うなものが GitLab に組み込み
GitLab Runner (Jenkins の slave のようなも
の) を動かすマシンが別途必要
repository の .gitlab-ci.yml で設定
(.travis.yml などと同様)
https://about.gitlab.com/features/gitlab-ci-cd/
4/25
GitLab Runner とは?
マシン上で直接実行する (Shell executor)
Docker の中で実行する (Docker executor)
これを使用
その他
https://docs.gitlab.com/runner/
5/25
Dokku とは?
Docker を使った OSS のミニ Heroku (PaaS)
ssh 経由の git で deploy できる
http://dokku.viewdocs.io/dokku/
6/25
組み合わせた状態
ブランチに push → Dokku に Review App
を deploy
Merge Request をマージ → Review App を
停止
master に push → Staging に deploy
確認後、クリックで Production に deploy
https://about.gitlab.com/features/review-
apps/
7/25
deploy 例
https://docs.gitlab.com/ce/ci/examples/
deployment/README.html では dpl gem で
heroku に deploy する例がある
Dokku との組み合わせは独自研究
8/25
組み合わせ方
.gitlab-ci.yml で設定する
deploy 用の ssh 秘密鍵などは secret
variables に設定
9/25
.gitlab-ci.yml の設定例
使用する docker image を指定
image: ruby:2.3.3
Rails アプリなので https://hub.docker.com/r/_/
ruby/ を使用
10/25
cache
per-branch caching:
cache:
key: "$CI_COMMIT_REF_NAME"
untracked: true
https://docs.gitlab.com/ce/ci/yaml/#cache-key
11/25
テスト用 variables
services で指定する postgres image で使用
(DATABASE_URL は Rails で使用)
variables:
# for test
POSTGRES_DB: dbname
POSTGRES_USER: dbuser
POSTGRES_PASSWORD: dbpass
DATABASE_URL: "postgres://dbuser:dbpass@postgres:5432/dbname"
https://hub.docker.com/r/_/postgres/
12/25
deploy 用 variables
# for deploy
DOKKU: ssh dokku@$DOKKU_HOST
APP_NAME: $CI_ENVIRONMENT_SLUG
DB_NAME: $CI_ENVIRONMENT_SLUG-database
DOKKU はあとで短く表記するため
APP_NAME は Dokku でのアプリ名 (サブド
メイン名)
DB_NAME はデータベースコンテナ名 (内部
用なので識別できれば何でも良い)
13/25
stages
stages:
- test
- review
- staging
- production
ブランチに push → test → review
master に push → test → staging →
production (後述の例 (1))
master に push → test → staging / タグを
push → test → production (後述の例 (2))
14/25
before_script
before_script:
- 'apt-get update -qq && apt-get -o dir::cache::archives="/cache/apt"
install -y -qq sqlite3 libsqlite3-dev nodejs'
- gem install bundler --no-ri --no-rdoc
- bundle install --jobs $(nproc) --path=/cache/bundler
- ln -nfs .test.env .env
開発環境に合わせてテスト環境でも sqlite3
が必要
js runtime も必要なので nodejs
インストール時に /cache を使用
dotenv (dotenv-rails) でテスト用環境変数設
定 15/25
.before_ssh
.before_ssh: &before_ssh
# https://docs.gitlab.com/ce/ci/ssh_keys/README.html
- 'which ssh-agent || ( apt-get update -y && apt-get -o
dir::cache::archives="/cache/apt" install -y openssh-client )'
- eval $(ssh-agent -s)
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
# Set `ssh-keyscan $DOKKU_HOST` to SSH_SERVER_HOSTKEYS
- '[[ -f /.dockerenv ]] && echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts'
- '[[ -f /.dockerenv ]] && echo "$SSH_CONFIG" > ~/.ssh/config'
.で始まるキーは後で参照する用途に使える
Dokku や Heroku に ssh で git push するとき
の前処理
secret variables から ssh-agent に秘密鍵と
known_hosts と ssh config を設定 16/25
.deploy_script
.deploy_script: &deploy_script
- $DOKKU apps:create $APP_NAME || echo $?
# require `sudo dokku plugin:install https://github.com/dokku/dokku-postgres`
- $DOKKU postgres:create $DB_NAME || echo $?
- $DOKKU postgres:link $DB_NAME $APP_NAME || echo $?
- $DOKKU config:set --no-restart $APP_NAME TZ=Asia/Tokyo
RAILS_SERVE_STATIC_FILES=1 NO_FORCE_SSL=1 RACK_DEV_MARK_ENV=review
- git push dokku@$DOKKU_HOST:$APP_NAME HEAD:refs/heads/master
- $DOKKU -tt run $APP_NAME bundle exec rake db:seed
app と db がなければ作成
TZ などの環境変数設定
HEAD:refs/heads/master という指定で push
-tt で強制的に tty を確保して rake db:seed
17/25
rake test
rake:
stage: test
services:
- postgres:latest
script:
- bundle exec rake db:setup RAILS_ENV=test
- bundle exec rake
services に指定した postgres image とリン
クした状態で実行
データベースの初期設定をしてテスト実行
18/25
staging deploy (1)
staging:
stage: staging
variables:
APP_NAME: hello-app-staging.example.jp
before_script: *before_ssh
script:
- git push dokku@$PRODUCTION_DOKKU_HOST:$APP_NAME HEAD:refs/heads/master
environment:
name: staging
url: https://hello-app-staging.example.jp/
only:
- master
before_script は sqlite3 のインストールなど
の代わりに ssh 設定
Pipelines の Environments からリンク
master に push したときのみ 19/25
production deploy (1)
production:
stage: production
variables:
APP_NAME: hello-app.example.jp
before_script: *before_ssh
script:
- git push dokku@$PRODUCTION_DOKKU_HOST:$APP_NAME HEAD:refs/heads/master
environment:
name: production
url: https://hello-app.example.jp/
when: manual
only:
- master
staging の後に手動実行
master に push したときのみ
20/25
staging deploy (2)
staging:
stage: staging
variables:
APP_NAME: hello-app-staging
script:
- gem install dpl
- dpl --provider=heroku --app=$APP_NAME --api-key=$HEROKU_STAGING_API_KEY
environment:
name: staging
url: https://$APP_NAME.herokuapp.com/
only:
- master
master に push したときのみ
dpl で heroku に deploy
21/25
production deploy (2)
production:
stage: production
variables:
APP_NAME: hello-app
script:
- gem install dpl
- dpl --provider=heroku --app=$APP_NAME --api-key=$HEROKU_PRODUCTION_API_KEY
environment:
name: production
url: https://$APP_NAME.herokuapp.com/
only:
- tags
タグを push したときのみ
dpl で heroku に deploy
22/25
review deploy
review:
stage: review
before_script: *before_ssh
script: *deploy_script
environment:
name: review/$CI_COMMIT_REF_NAME
url: http://$CI_ENVIRONMENT_SLUG.$DOKKU_DOMAIN
on_stop: stop_review
only:
- branches
except:
- master
review 用の Dokku アプリを deploy
master 以外のブランチに push したときのみ23/25
stop review
stop_review:
stage: review
variables:
GIT_STRATEGY: none
before_script: *before_ssh
script:
- $DOKKU apps:destroy $CI_ENVIRONMENT_SLUG --force || echo $?
- $DOKKU postgres:destroy $CI_ENVIRONMENT_SLUG-database --force || echo $?
environment:
name: review/$CI_COMMIT_REF_NAME
action: stop
when: manual
only:
- branches
except:
- master
postgres は使用中だとエラーになるので
apps から停止
リンクも消える 24/25
まとめ
GitLab と Dokku を組み合わせて CI/CD 環境
を作成する例を紹介
環境構築に使っている Ansible Playbook は
https://github.com/znz/ansible-playbook-
gitlab-dokku
ブログ記事は http://blog.n-z.jp/blog/
categories/gitlab/
25/25Powered by Rabbit 2.2.0

More Related Content

What's hot

大規模DCのネットワークデザイン
大規模DCのネットワークデザイン大規模DCのネットワークデザイン
大規模DCのネットワークデザインMasayuki Kobayashi
 
AWS LambdaとDynamoDBがこんなにツライはずがない #ssmjp
AWS LambdaとDynamoDBがこんなにツライはずがない #ssmjpAWS LambdaとDynamoDBがこんなにツライはずがない #ssmjp
AWS LambdaとDynamoDBがこんなにツライはずがない #ssmjpMasahiro NAKAYAMA
 
Machine configoperatorのちょっとイイかもしれない話
Machine configoperatorのちょっとイイかもしれない話 Machine configoperatorのちょっとイイかもしれない話
Machine configoperatorのちょっとイイかもしれない話 Toshihiro Araki
 
大規模サービスを支えるネットワークインフラの全貌
大規模サービスを支えるネットワークインフラの全貌大規模サービスを支えるネットワークインフラの全貌
大規模サービスを支えるネットワークインフラの全貌LINE Corporation
 
忙しい人の5分で分かるDocker 2017年春Ver
忙しい人の5分で分かるDocker 2017年春Ver忙しい人の5分で分かるDocker 2017年春Ver
忙しい人の5分で分かるDocker 2017年春VerMasahito Zembutsu
 
Kubernetesを使う上で抑えておくべきAWSの基礎概念
Kubernetesを使う上で抑えておくべきAWSの基礎概念Kubernetesを使う上で抑えておくべきAWSの基礎概念
Kubernetesを使う上で抑えておくべきAWSの基礎概念Shinya Mori (@mosuke5)
 
クラウドネイティブ時代の分散トレーシング - Distributed Tracing in a Cloud Native Age
クラウドネイティブ時代の分散トレーシング - Distributed Tracing in a Cloud Native Ageクラウドネイティブ時代の分散トレーシング - Distributed Tracing in a Cloud Native Age
クラウドネイティブ時代の分散トレーシング - Distributed Tracing in a Cloud Native AgeYoichi Kawasaki
 
乗っ取れコンテナ!!開発者から見たコンテナセキュリティの考え方(CloudNative Days Tokyo 2021 発表資料)
乗っ取れコンテナ!!開発者から見たコンテナセキュリティの考え方(CloudNative Days Tokyo 2021 発表資料)乗っ取れコンテナ!!開発者から見たコンテナセキュリティの考え方(CloudNative Days Tokyo 2021 発表資料)
乗っ取れコンテナ!!開発者から見たコンテナセキュリティの考え方(CloudNative Days Tokyo 2021 発表資料)NTT DATA Technology & Innovation
 
オススメの標準・準標準パッケージ20選
オススメの標準・準標準パッケージ20選オススメの標準・準標準パッケージ20選
オススメの標準・準標準パッケージ20選Takuya Ueda
 
20210216 AWS Black Belt Online Seminar AWS Database Migration Service
20210216 AWS Black Belt Online Seminar AWS Database Migration Service20210216 AWS Black Belt Online Seminar AWS Database Migration Service
20210216 AWS Black Belt Online Seminar AWS Database Migration ServiceAmazon Web Services Japan
 
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -onozaty
 
Kinesis Firehoseを使ってみた
Kinesis Firehoseを使ってみたKinesis Firehoseを使ってみた
Kinesis Firehoseを使ってみたdcubeio
 
Node.js Native ESM への道 〜最終章: Babel / TypeScript Modules との闘い〜
Node.js Native ESM への道  〜最終章: Babel / TypeScript Modules との闘い〜Node.js Native ESM への道  〜最終章: Babel / TypeScript Modules との闘い〜
Node.js Native ESM への道 〜最終章: Babel / TypeScript Modules との闘い〜Teppei Sato
 
【Zabbix2.0】snmpttによるトラップメッセージの編集 #Zabbix #自宅ラック勉強会
【Zabbix2.0】snmpttによるトラップメッセージの編集 #Zabbix #自宅ラック勉強会【Zabbix2.0】snmpttによるトラップメッセージの編集 #Zabbix #自宅ラック勉強会
【Zabbix2.0】snmpttによるトラップメッセージの編集 #Zabbix #自宅ラック勉強会真乙 九龍
 
CircleCI vs. CodePipeline
CircleCI vs. CodePipelineCircleCI vs. CodePipeline
CircleCI vs. CodePipelineHonMarkHunt
 
AWS re:Inforce2019 re:Cap LT
AWS re:Inforce2019 re:Cap LTAWS re:Inforce2019 re:Cap LT
AWS re:Inforce2019 re:Cap LTHibino Hisashi
 
Recap: Windows Server 2019 Failover Clustering
Recap: Windows Server 2019 Failover ClusteringRecap: Windows Server 2019 Failover Clustering
Recap: Windows Server 2019 Failover ClusteringKazuki Takai
 
[AWSマイスターシリーズ]Amazon Elastic Load Balancing (ELB)
[AWSマイスターシリーズ]Amazon Elastic Load Balancing (ELB)[AWSマイスターシリーズ]Amazon Elastic Load Balancing (ELB)
[AWSマイスターシリーズ]Amazon Elastic Load Balancing (ELB)Amazon Web Services Japan
 

What's hot (20)

大規模DCのネットワークデザイン
大規模DCのネットワークデザイン大規模DCのネットワークデザイン
大規模DCのネットワークデザイン
 
AWS LambdaとDynamoDBがこんなにツライはずがない #ssmjp
AWS LambdaとDynamoDBがこんなにツライはずがない #ssmjpAWS LambdaとDynamoDBがこんなにツライはずがない #ssmjp
AWS LambdaとDynamoDBがこんなにツライはずがない #ssmjp
 
Machine configoperatorのちょっとイイかもしれない話
Machine configoperatorのちょっとイイかもしれない話 Machine configoperatorのちょっとイイかもしれない話
Machine configoperatorのちょっとイイかもしれない話
 
大規模サービスを支えるネットワークインフラの全貌
大規模サービスを支えるネットワークインフラの全貌大規模サービスを支えるネットワークインフラの全貌
大規模サービスを支えるネットワークインフラの全貌
 
忙しい人の5分で分かるDocker 2017年春Ver
忙しい人の5分で分かるDocker 2017年春Ver忙しい人の5分で分かるDocker 2017年春Ver
忙しい人の5分で分かるDocker 2017年春Ver
 
自宅k8s/vSphere入門
自宅k8s/vSphere入門自宅k8s/vSphere入門
自宅k8s/vSphere入門
 
Kubernetesを使う上で抑えておくべきAWSの基礎概念
Kubernetesを使う上で抑えておくべきAWSの基礎概念Kubernetesを使う上で抑えておくべきAWSの基礎概念
Kubernetesを使う上で抑えておくべきAWSの基礎概念
 
クラウドネイティブ時代の分散トレーシング - Distributed Tracing in a Cloud Native Age
クラウドネイティブ時代の分散トレーシング - Distributed Tracing in a Cloud Native Ageクラウドネイティブ時代の分散トレーシング - Distributed Tracing in a Cloud Native Age
クラウドネイティブ時代の分散トレーシング - Distributed Tracing in a Cloud Native Age
 
乗っ取れコンテナ!!開発者から見たコンテナセキュリティの考え方(CloudNative Days Tokyo 2021 発表資料)
乗っ取れコンテナ!!開発者から見たコンテナセキュリティの考え方(CloudNative Days Tokyo 2021 発表資料)乗っ取れコンテナ!!開発者から見たコンテナセキュリティの考え方(CloudNative Days Tokyo 2021 発表資料)
乗っ取れコンテナ!!開発者から見たコンテナセキュリティの考え方(CloudNative Days Tokyo 2021 発表資料)
 
オススメの標準・準標準パッケージ20選
オススメの標準・準標準パッケージ20選オススメの標準・準標準パッケージ20選
オススメの標準・準標準パッケージ20選
 
20210216 AWS Black Belt Online Seminar AWS Database Migration Service
20210216 AWS Black Belt Online Seminar AWS Database Migration Service20210216 AWS Black Belt Online Seminar AWS Database Migration Service
20210216 AWS Black Belt Online Seminar AWS Database Migration Service
 
クラウド上のシステム監視 入門編~システムを作ったその先に~
クラウド上のシステム監視 入門編~システムを作ったその先に~クラウド上のシステム監視 入門編~システムを作ったその先に~
クラウド上のシステム監視 入門編~システムを作ったその先に~
 
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
 
Kinesis Firehoseを使ってみた
Kinesis Firehoseを使ってみたKinesis Firehoseを使ってみた
Kinesis Firehoseを使ってみた
 
Node.js Native ESM への道 〜最終章: Babel / TypeScript Modules との闘い〜
Node.js Native ESM への道  〜最終章: Babel / TypeScript Modules との闘い〜Node.js Native ESM への道  〜最終章: Babel / TypeScript Modules との闘い〜
Node.js Native ESM への道 〜最終章: Babel / TypeScript Modules との闘い〜
 
【Zabbix2.0】snmpttによるトラップメッセージの編集 #Zabbix #自宅ラック勉強会
【Zabbix2.0】snmpttによるトラップメッセージの編集 #Zabbix #自宅ラック勉強会【Zabbix2.0】snmpttによるトラップメッセージの編集 #Zabbix #自宅ラック勉強会
【Zabbix2.0】snmpttによるトラップメッセージの編集 #Zabbix #自宅ラック勉強会
 
CircleCI vs. CodePipeline
CircleCI vs. CodePipelineCircleCI vs. CodePipeline
CircleCI vs. CodePipeline
 
AWS re:Inforce2019 re:Cap LT
AWS re:Inforce2019 re:Cap LTAWS re:Inforce2019 re:Cap LT
AWS re:Inforce2019 re:Cap LT
 
Recap: Windows Server 2019 Failover Clustering
Recap: Windows Server 2019 Failover ClusteringRecap: Windows Server 2019 Failover Clustering
Recap: Windows Server 2019 Failover Clustering
 
[AWSマイスターシリーズ]Amazon Elastic Load Balancing (ELB)
[AWSマイスターシリーズ]Amazon Elastic Load Balancing (ELB)[AWSマイスターシリーズ]Amazon Elastic Load Balancing (ELB)
[AWSマイスターシリーズ]Amazon Elastic Load Balancing (ELB)
 

Similar to GitLab + Dokku で作る CI/CD 環境

Google container builderと友だちになるまで
Google container builderと友だちになるまでGoogle container builderと友だちになるまで
Google container builderと友だちになるまでlestrrat
 
ラズパイ2で動く Docker PaaSを作ってみたよ
ラズパイ2で動く Docker PaaSを作ってみたよラズパイ2で動く Docker PaaSを作ってみたよ
ラズパイ2で動く Docker PaaSを作ってみたよnpsg
 
Docker実践入門
Docker実践入門Docker実践入門
Docker実践入門hiro nemu
 
Building production server on docker
Building production server on dockerBuilding production server on docker
Building production server on dockerHiroshi Miura
 
Building production server on docker
Building production server on dockerBuilding production server on docker
Building production server on dockerHiroshi Miura
 
WSL2+docker+JupyterとVS Codeリモート環境の構築
WSL2+docker+JupyterとVS Codeリモート環境の構築WSL2+docker+JupyterとVS Codeリモート環境の構築
WSL2+docker+JupyterとVS Codeリモート環境の構築Saito5656
 
ラズパイ2で動く Docker PaaS
ラズパイ2で動く Docker PaaSラズパイ2で動く Docker PaaS
ラズパイ2で動く Docker PaaSnpsg
 
Server side Swift & Photo Booth
Server side Swift & Photo Booth Server side Swift & Photo Booth
Server side Swift & Photo Booth LINE Corporation
 
20150101勉強会 dokku alt
20150101勉強会 dokku alt20150101勉強会 dokku alt
20150101勉強会 dokku altShugo Numano
 
Docker & Kubernetes基礎
Docker & Kubernetes基礎Docker & Kubernetes基礎
Docker & Kubernetes基礎Daisuke Hiraoka
 
Metahub for github
Metahub for githubMetahub for github
Metahub for githubSuguru Oho
 
オンプレでPrivate Registry使ったDockerイメージの運用について
オンプレでPrivate Registry使ったDockerイメージの運用についてオンプレでPrivate Registry使ったDockerイメージの運用について
オンプレでPrivate Registry使ったDockerイメージの運用についてYASUKAZU NAGATOMI
 

Similar to GitLab + Dokku で作る CI/CD 環境 (20)

Google container builderと友だちになるまで
Google container builderと友だちになるまでGoogle container builderと友だちになるまで
Google container builderと友だちになるまで
 
ラズパイ2で動く Docker PaaSを作ってみたよ
ラズパイ2で動く Docker PaaSを作ってみたよラズパイ2で動く Docker PaaSを作ってみたよ
ラズパイ2で動く Docker PaaSを作ってみたよ
 
Git (実践入門編)
Git (実践入門編)Git (実践入門編)
Git (実践入門編)
 
Docker実践入門
Docker実践入門Docker実践入門
Docker実践入門
 
Building production server on docker
Building production server on dockerBuilding production server on docker
Building production server on docker
 
Building production server on docker
Building production server on dockerBuilding production server on docker
Building production server on docker
 
Dockerと継続的インテグレーション
Dockerと継続的インテグレーションDockerと継続的インテグレーション
Dockerと継続的インテグレーション
 
WSL2+docker+JupyterとVS Codeリモート環境の構築
WSL2+docker+JupyterとVS Codeリモート環境の構築WSL2+docker+JupyterとVS Codeリモート環境の構築
WSL2+docker+JupyterとVS Codeリモート環境の構築
 
Github第8章
Github第8章Github第8章
Github第8章
 
ラズパイ2で動く Docker PaaS
ラズパイ2で動く Docker PaaSラズパイ2で動く Docker PaaS
ラズパイ2で動く Docker PaaS
 
Server side Swift & Photo Booth
Server side Swift & Photo Booth Server side Swift & Photo Booth
Server side Swift & Photo Booth
 
Gitの紹介
Gitの紹介Gitの紹介
Gitの紹介
 
20150101勉強会 dokku alt
20150101勉強会 dokku alt20150101勉強会 dokku alt
20150101勉強会 dokku alt
 
Capistrano
CapistranoCapistrano
Capistrano
 
Fig
FigFig
Fig
 
Docker & Kubernetes基礎
Docker & Kubernetes基礎Docker & Kubernetes基礎
Docker & Kubernetes基礎
 
Git (運用編)
Git (運用編)Git (運用編)
Git (運用編)
 
Docker社内勉強会
Docker社内勉強会Docker社内勉強会
Docker社内勉強会
 
Metahub for github
Metahub for githubMetahub for github
Metahub for github
 
オンプレでPrivate Registry使ったDockerイメージの運用について
オンプレでPrivate Registry使ったDockerイメージの運用についてオンプレでPrivate Registry使ったDockerイメージの運用について
オンプレでPrivate Registry使ったDockerイメージの運用について
 

More from Kazuhiro Nishiyama

lilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げたlilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げたKazuhiro Nishiyama
 
小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話Kazuhiro Nishiyama
 
Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告Kazuhiro Nishiyama
 
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdffukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdfKazuhiro Nishiyama
 
rubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdfrubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdfKazuhiro Nishiyama
 
qemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみたqemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみたKazuhiro Nishiyama
 
workflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考えるworkflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考えるKazuhiro Nishiyama
 
あまり知られていないRubyの便利機能
あまり知られていないRubyの便利機能あまり知られていないRubyの便利機能
あまり知られていないRubyの便利機能Kazuhiro Nishiyama
 
Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?Kazuhiro Nishiyama
 
チャットボットのススメ
チャットボットのススメチャットボットのススメ
チャットボットのススメKazuhiro Nishiyama
 
Action Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみたAction Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみたKazuhiro Nishiyama
 
最近のrubyのインストール方法
最近のrubyのインストール方法最近のrubyのインストール方法
最近のrubyのインストール方法Kazuhiro Nishiyama
 

More from Kazuhiro Nishiyama (20)

lilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げたlilo.linux.or.jp を buster から bullseye に上げた
lilo.linux.or.jp を buster から bullseye に上げた
 
小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話小規模個人アプリをRails 7.xにバージョンアップした話
小規模個人アプリをRails 7.xにバージョンアップした話
 
Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告Ruby リファレンスマニュアル改善計画 2022 進捗報告
Ruby リファレンスマニュアル改善計画 2022 進捗報告
 
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdffukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
fukuoka03-rubima-reboot-rubyist-magazine-reboot.pdf
 
rubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdfrubykaigi2022-rurema-history-and-future.pdf
rubykaigi2022-rurema-history-and-future.pdf
 
qemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみたqemuのriscv64にDebianを入れてみた
qemuのriscv64にDebianを入れてみた
 
systemd 再入門
systemd 再入門systemd 再入門
systemd 再入門
 
Ruby 3.0.0 コネタ集
Ruby 3.0.0 コネタ集Ruby 3.0.0 コネタ集
Ruby 3.0.0 コネタ集
 
livedoor天気API終了対応
livedoor天気API終了対応livedoor天気API終了対応
livedoor天気API終了対応
 
Wireguard 実践入門
Wireguard 実践入門Wireguard 実践入門
Wireguard 実践入門
 
workflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考えるworkflow,job,step の使い分けの基準を考える
workflow,job,step の使い分けの基準を考える
 
あまり知られていないRubyの便利機能
あまり知られていないRubyの便利機能あまり知られていないRubyの便利機能
あまり知られていないRubyの便利機能
 
Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?Dockerのオフィシャルrubyイメージとは?
Dockerのオフィシャルrubyイメージとは?
 
チャットボットのススメ
チャットボットのススメチャットボットのススメ
チャットボットのススメ
 
Dokku の紹介
Dokku の紹介Dokku の紹介
Dokku の紹介
 
Action Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみたAction Cableで簡易チャットを作ってみた
Action Cableで簡易チャットを作ってみた
 
Ruby svn to git
Ruby svn to gitRuby svn to git
Ruby svn to git
 
Ruby 2.6 Update
Ruby 2.6 UpdateRuby 2.6 Update
Ruby 2.6 Update
 
最近のrubyのインストール方法
最近のrubyのインストール方法最近のrubyのインストール方法
最近のrubyのインストール方法
 
Language update 2018 - ruby
Language update 2018 - rubyLanguage update 2018 - ruby
Language update 2018 - ruby
 

Recently uploaded

SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものですiPride Co., Ltd.
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Yuma Ohgami
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...Toru Tamaki
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdftaisei2219
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNetToru Tamaki
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムsugiuralab
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)Hiroki Ichikura
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A surveyToru Tamaki
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略Ryo Sasaki
 

Recently uploaded (9)

SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
 

GitLab + Dokku で作る CI/CD 環境

  • 1. GitLab + Dokku で作る CI/ CD 環境 Kazuhiro NISHIYAMA 第78回 Ruby関西 勉強会 2017/07/29 Powered by Rabbit 2.2.0
  • 2. 自己紹介 西山和広 id:znz (github, twitter など) Ruby コミッター 1/25
  • 3. GitLab + Dokku GitLab GitLab CI Dokku (+ Heroku) 2/25
  • 4. GitLab とは? 簡単にいえば OSS の GitHub クローンのよ うなもの Git ホスティング Merge Request (GitHub の Pull Request) Issue 管理など色々 GitHub にない機能もある https://about.gitlab.com/features/ 3/25
  • 5. GitLab CI とは? Continuous Integration/Continuous Delivery (CI/CD) Jenkins のジョブを実行しないマスターのよ うなものが GitLab に組み込み GitLab Runner (Jenkins の slave のようなも の) を動かすマシンが別途必要 repository の .gitlab-ci.yml で設定 (.travis.yml などと同様) https://about.gitlab.com/features/gitlab-ci-cd/ 4/25
  • 6. GitLab Runner とは? マシン上で直接実行する (Shell executor) Docker の中で実行する (Docker executor) これを使用 その他 https://docs.gitlab.com/runner/ 5/25
  • 7. Dokku とは? Docker を使った OSS のミニ Heroku (PaaS) ssh 経由の git で deploy できる http://dokku.viewdocs.io/dokku/ 6/25
  • 8. 組み合わせた状態 ブランチに push → Dokku に Review App を deploy Merge Request をマージ → Review App を 停止 master に push → Staging に deploy 確認後、クリックで Production に deploy https://about.gitlab.com/features/review- apps/ 7/25
  • 9. deploy 例 https://docs.gitlab.com/ce/ci/examples/ deployment/README.html では dpl gem で heroku に deploy する例がある Dokku との組み合わせは独自研究 8/25
  • 10. 組み合わせ方 .gitlab-ci.yml で設定する deploy 用の ssh 秘密鍵などは secret variables に設定 9/25
  • 11. .gitlab-ci.yml の設定例 使用する docker image を指定 image: ruby:2.3.3 Rails アプリなので https://hub.docker.com/r/_/ ruby/ を使用 10/25
  • 12. cache per-branch caching: cache: key: "$CI_COMMIT_REF_NAME" untracked: true https://docs.gitlab.com/ce/ci/yaml/#cache-key 11/25
  • 13. テスト用 variables services で指定する postgres image で使用 (DATABASE_URL は Rails で使用) variables: # for test POSTGRES_DB: dbname POSTGRES_USER: dbuser POSTGRES_PASSWORD: dbpass DATABASE_URL: "postgres://dbuser:dbpass@postgres:5432/dbname" https://hub.docker.com/r/_/postgres/ 12/25
  • 14. deploy 用 variables # for deploy DOKKU: ssh dokku@$DOKKU_HOST APP_NAME: $CI_ENVIRONMENT_SLUG DB_NAME: $CI_ENVIRONMENT_SLUG-database DOKKU はあとで短く表記するため APP_NAME は Dokku でのアプリ名 (サブド メイン名) DB_NAME はデータベースコンテナ名 (内部 用なので識別できれば何でも良い) 13/25
  • 15. stages stages: - test - review - staging - production ブランチに push → test → review master に push → test → staging → production (後述の例 (1)) master に push → test → staging / タグを push → test → production (後述の例 (2)) 14/25
  • 16. before_script before_script: - 'apt-get update -qq && apt-get -o dir::cache::archives="/cache/apt" install -y -qq sqlite3 libsqlite3-dev nodejs' - gem install bundler --no-ri --no-rdoc - bundle install --jobs $(nproc) --path=/cache/bundler - ln -nfs .test.env .env 開発環境に合わせてテスト環境でも sqlite3 が必要 js runtime も必要なので nodejs インストール時に /cache を使用 dotenv (dotenv-rails) でテスト用環境変数設 定 15/25
  • 17. .before_ssh .before_ssh: &before_ssh # https://docs.gitlab.com/ce/ci/ssh_keys/README.html - 'which ssh-agent || ( apt-get update -y && apt-get -o dir::cache::archives="/cache/apt" install -y openssh-client )' - eval $(ssh-agent -s) - ssh-add <(echo "$SSH_PRIVATE_KEY") - mkdir -p ~/.ssh # Set `ssh-keyscan $DOKKU_HOST` to SSH_SERVER_HOSTKEYS - '[[ -f /.dockerenv ]] && echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts' - '[[ -f /.dockerenv ]] && echo "$SSH_CONFIG" > ~/.ssh/config' .で始まるキーは後で参照する用途に使える Dokku や Heroku に ssh で git push するとき の前処理 secret variables から ssh-agent に秘密鍵と known_hosts と ssh config を設定 16/25
  • 18. .deploy_script .deploy_script: &deploy_script - $DOKKU apps:create $APP_NAME || echo $? # require `sudo dokku plugin:install https://github.com/dokku/dokku-postgres` - $DOKKU postgres:create $DB_NAME || echo $? - $DOKKU postgres:link $DB_NAME $APP_NAME || echo $? - $DOKKU config:set --no-restart $APP_NAME TZ=Asia/Tokyo RAILS_SERVE_STATIC_FILES=1 NO_FORCE_SSL=1 RACK_DEV_MARK_ENV=review - git push dokku@$DOKKU_HOST:$APP_NAME HEAD:refs/heads/master - $DOKKU -tt run $APP_NAME bundle exec rake db:seed app と db がなければ作成 TZ などの環境変数設定 HEAD:refs/heads/master という指定で push -tt で強制的に tty を確保して rake db:seed 17/25
  • 19. rake test rake: stage: test services: - postgres:latest script: - bundle exec rake db:setup RAILS_ENV=test - bundle exec rake services に指定した postgres image とリン クした状態で実行 データベースの初期設定をしてテスト実行 18/25
  • 20. staging deploy (1) staging: stage: staging variables: APP_NAME: hello-app-staging.example.jp before_script: *before_ssh script: - git push dokku@$PRODUCTION_DOKKU_HOST:$APP_NAME HEAD:refs/heads/master environment: name: staging url: https://hello-app-staging.example.jp/ only: - master before_script は sqlite3 のインストールなど の代わりに ssh 設定 Pipelines の Environments からリンク master に push したときのみ 19/25
  • 21. production deploy (1) production: stage: production variables: APP_NAME: hello-app.example.jp before_script: *before_ssh script: - git push dokku@$PRODUCTION_DOKKU_HOST:$APP_NAME HEAD:refs/heads/master environment: name: production url: https://hello-app.example.jp/ when: manual only: - master staging の後に手動実行 master に push したときのみ 20/25
  • 22. staging deploy (2) staging: stage: staging variables: APP_NAME: hello-app-staging script: - gem install dpl - dpl --provider=heroku --app=$APP_NAME --api-key=$HEROKU_STAGING_API_KEY environment: name: staging url: https://$APP_NAME.herokuapp.com/ only: - master master に push したときのみ dpl で heroku に deploy 21/25
  • 23. production deploy (2) production: stage: production variables: APP_NAME: hello-app script: - gem install dpl - dpl --provider=heroku --app=$APP_NAME --api-key=$HEROKU_PRODUCTION_API_KEY environment: name: production url: https://$APP_NAME.herokuapp.com/ only: - tags タグを push したときのみ dpl で heroku に deploy 22/25
  • 24. review deploy review: stage: review before_script: *before_ssh script: *deploy_script environment: name: review/$CI_COMMIT_REF_NAME url: http://$CI_ENVIRONMENT_SLUG.$DOKKU_DOMAIN on_stop: stop_review only: - branches except: - master review 用の Dokku アプリを deploy master 以外のブランチに push したときのみ23/25
  • 25. stop review stop_review: stage: review variables: GIT_STRATEGY: none before_script: *before_ssh script: - $DOKKU apps:destroy $CI_ENVIRONMENT_SLUG --force || echo $? - $DOKKU postgres:destroy $CI_ENVIRONMENT_SLUG-database --force || echo $? environment: name: review/$CI_COMMIT_REF_NAME action: stop when: manual only: - branches except: - master postgres は使用中だとエラーになるので apps から停止 リンクも消える 24/25
  • 26. まとめ GitLab と Dokku を組み合わせて CI/CD 環境 を作成する例を紹介 環境構築に使っている Ansible Playbook は https://github.com/znz/ansible-playbook- gitlab-dokku ブログ記事は http://blog.n-z.jp/blog/ categories/gitlab/ 25/25Powered by Rabbit 2.2.0