Showing posts with label capistrano. Show all posts
Showing posts with label capistrano. Show all posts

December 23, 2014

OpenShiftのJenkins ServerにCapistrano 3をインストールする

OpenShiftのJenkins Serverはrubyのバージョンが1.8.7で、Capistrano 3をインストールしようとしたらエラーになりました。

簡単な方法は無いかなーと考えてみたんですが、とりあえず以下の方法でインストール出来ました。

適当な場所にbundleをコピーして、実行するrubyを変更します。
> mkdir ~/app-root/data/bin
> cp /usr/bin/bundle ~/app-root/data/bin/
vim等で ~/app-root/data/bin/bundle の先頭の行を以下に変更します。
#!/usr/bin/ruby193-ruby
適当な場所にGemfileを作ります。
source 'https://rubygems.org'

gem 'capistrano', '~> 3.3.0'
インストールと初期化がうまく行けばOKだと思います。
> ~/app-root/data/bin/bundle install
> ~/app-root/data/bin/bundle exec cap install

January 12, 2013

CapistranoのオレオレGitデプロイをJenkins(ブラウザ)上から実行してみた

前回書いたCapistrano用のスクリプトを、Jenkins(ブラウザ)上から実行してみます。

CapistranoでオレオレGitデプロイをしてみた
http://madroom-project.blogspot.jp/2013/01/capistranogit.html

この方法はCapistranoを用いたデプロイの一例です。環境により、上記スクリプトそのものや、以下の設定"以外の設定"の調整が、別途必要になるかもしれません。飽くまで参考としてお願いします。


(1)
https://github.com/mp-php/cap4git をDLします。

config/deploy.rbの設定を行います。最低限、以下の修正が必要です。
set :local_user, "xxxxx"
"local_app_dir"は一度削除されるので注意して下さい。

config/deploy以下の環境別設定ファイルの設定を行います。最低限、以下の修正が必要です。
set :git_uri, ""
set :remote_user, "xxxxx"
#set :password, "xxxxx"
role :remote, "#{remote_user}@example.com"
"remote_app_dir"は一度削除されるので注意して下さい。

設定が終わったら、"capistrano"というディレクトリ名で、ZIPにします。このZIPは、後で使います。


(2)
Jenkinsに"Build Secret Plugin"をインストールします。


(3)
Jenkinsで新規ジョブを作成します。このジョブは、以後に作成するジョブの雛形とします。このジョブからはデプロイは行いません。

This build is parameterizedにチェックを入れて、以下の設定をします。
▼Choice
Name: ENV
Choices: staging[改行]production
▼Choice
Name: MODE
Choices: update[改行]rollback
▼String Parameter
Name: CHECKOUT
Default Value: master

Disable Buildにチェックを入れます。

Build EnvironmentのBuild Secretにチェックを入れて、Environment variableに"CAPISTRANO"と入力します。
* この時点でZIPはアップロードしません。

BuildのExecute shellのCommandに以下を入力します。
cd $CAPISTRANO/capistrano
cap $ENV deploy_ -s _mode=$MODE -s _checkout=$CHECKOUT
Saveします。

以下、設定のキャプチャです。
1585x2196(311KB)



(4)
(3)のジョブをコピーして新規ジョブを作成します。実際にデプロイを行うジョブです。
Disable Buildのチェックを外します。
Build EnvironmentのBuild Secretに(1)のZIPをUPしてSaveします。


(5)
ビルドします。


(6)
新たにジョブを作成する場合、(1)と(4)のみでOKです。


以下、ビルド時のログです。一部、Gitコマンドの出力とか、特に問題ないものが何故か"err"と出ていますが。。。尚、ローカルからローカルに対して実行したログになります。(パス等、一部、書き換えています。)

1回目。remote_deploy_toに".git"ディレクトリが無いのでgit initが行われます。
Started by user admin
Building in workspace /var/lib/jenkins/jobs/TEST/workspace
[workspace] $ /bin/sh -xe /tmp/hudson4292648394511032628.sh
+ cd /var/lib/jenkins/secrets/7e022c8d-762c-4c1c-9981-b64b37504d19/capistrano
+ cap staging deploy_ -s _mode=update -s _checkout=master
    triggering load callbacks
  * executing `staging'
    triggering start callbacks for `deploy_'
  * executing `multistage:ensure'
  * executing `deploy_'
  * executing `cleanup_'
  * executing `cleanup_local_'
  * executing "rm -rf /home/xxxxx/capistrano/cap4git/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 231ms
  * executing "mkdir -p /home/xxxxx/capistrano/cap4git/repo/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 3ms
  * executing `cleanup_reomote_'
  * executing "rm -rf /home/xxxxx/capistrano/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 5ms
  * executing "mkdir -p /home/xxxxx/capistrano/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 3ms
  * executing `tar_'
  * executing "git clone --bare https://github.com/mp-php/cap4git /home/xxxxx/capistrano/cap4git/repo/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
 ** [out :: xxxxx@localhost] Cloning into bare repository '/home/xxxxx/capistrano/cap4git/repo'...
    command finished in 4293ms
  * executing "tar -cvzf /home/xxxxx/capistrano/cap4git/repo.tar.gz -C /home/xxxxx/capistrano/cap4git/ repo >& /dev/null"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 9ms
  * executing `upload_'
    servers: ["localhost"]
 ** scp upload /home/xxxxx/capistrano/cap4git/repo.tar.gz -> /home/xxxxx/capistrano/repo.tar.gz
    [localhost] /home/xxxxx/capistrano/cap4git/repo.tar.gz
  * scp upload complete
  * executing "tar xvzf /home/xxxxx/capistrano/repo.tar.gz -C /home/xxxxx/capistrano/ >& /dev/null"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 9ms
  * executing "if [ -e /home/xxxxx/repo/.git ]; then echo 'true'; fi"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 3ms
  * executing `git_init_'
  * executing "if [ -e /home/xxxxx/repo/ ]; then echo 'true'; fi"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 3ms
  * executing "mkdir -p /home/xxxxx/repo/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 3ms
  * executing "cd /home/xxxxx/repo/ && git init ."
    servers: ["localhost"]
    [xxxxx@localhost] executing command
 ** [out :: xxxxx@localhost] Initialized empty Git repository in /home/xxxxx/repo/.git/
    command finished in 6ms
  * executing `git_remote_'
  * executing "cd /home/xxxxx/repo/ && git remote rm capistrano"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
*** [err :: xxxxx@localhost] error: Could not remove config section 'remote.capistrano'
    command finished in 5ms
  * executing "cd /home/xxxxx/repo/ && git remote add capistrano /home/xxxxx/capistrano/repo/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 5ms
  * executing `git_pull_'
  * executing "cd /home/xxxxx/repo/ && git pull capistrano master"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
*** [err :: xxxxx@localhost] From /home/xxxxx/capistrano/repo
*** [err :: xxxxx@localhost] * branch            master     -> FETCH_HEAD
    command finished in 47ms
Finished: SUCCESS
2回目。remote_deploy_toに".git"ディレクトリが有るのでgit pullが行われます。
Started by user admin
Building in workspace /var/lib/jenkins/jobs/TEST/workspace
[workspace] $ /bin/sh -xe /tmp/hudson5519699631114845069.sh
+ cd /var/lib/jenkins/secrets/fc7237c2-57b0-41b9-a91c-6dc3ec9b8d57/capistrano
+ cap staging deploy_ -s _mode=update -s _checkout=master
    triggering load callbacks
  * executing `staging'
    triggering start callbacks for `deploy_'
  * executing `multistage:ensure'
  * executing `deploy_'
  * executing `cleanup_'
  * executing `cleanup_local_'
  * executing "rm -rf /home/xxxxx/capistrano/cap4git/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 233ms
  * executing "mkdir -p /home/xxxxx/capistrano/cap4git/repo/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 3ms
  * executing `cleanup_reomote_'
  * executing "rm -rf /home/xxxxx/capistrano/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 5ms
  * executing "mkdir -p /home/xxxxx/capistrano/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 8ms
  * executing `tar_'
  * executing "git clone --bare https://github.com/mp-php/cap4git /home/xxxxx/capistrano/cap4git/repo/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
 ** [out :: xxxxx@localhost] Cloning into bare repository '/home/xxxxx/capistrano/cap4git/repo'...
    command finished in 3222ms
  * executing "tar -cvzf /home/xxxxx/capistrano/cap4git/repo.tar.gz -C /home/xxxxx/capistrano/cap4git/ repo >& /dev/null"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 9ms
  * executing `upload_'
    servers: ["localhost"]
 ** scp upload /home/xxxxx/capistrano/cap4git/repo.tar.gz -> /home/xxxxx/capistrano/repo.tar.gz
    [localhost] /home/xxxxx/capistrano/cap4git/repo.tar.gz
  * scp upload complete
  * executing "tar xvzf /home/xxxxx/capistrano/repo.tar.gz -C /home/xxxxx/capistrano/ >& /dev/null"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 8ms
  * executing "if [ -e /home/xxxxx/repo/.git ]; then echo 'true'; fi"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 3ms
  * executing `git_remote_'
  * executing "cd /home/xxxxx/repo/ && git remote rm capistrano"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 4ms
  * executing "cd /home/xxxxx/repo/ && git remote add capistrano /home/xxxxx/capistrano/repo/"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
    command finished in 4ms
  * executing `git_checkout_'
  * executing "cd /home/xxxxx/repo/ && git checkout master"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
*** [err :: xxxxx@localhost] Already on 'master'
    command finished in 5ms
  * executing `git_pull_'
  * executing "cd /home/xxxxx/repo/ && git pull capistrano master"
    servers: ["localhost"]
    [xxxxx@localhost] executing command
*** [err :: xxxxx@localhost] From /home/xxxxx/capistrano/repo
*** [err :: xxxxx@localhost] * branch            master     -> FETCH_HEAD
 ** [out :: xxxxx@localhost] Already up-to-date.
    command finished in 29ms
Finished: SUCCESS

January 10, 2013

CapistranoでオレオレGitデプロイをしてみた

ここ最近、Jenkinsで色々と自動化しているので、ついでにデプロイについても考えてみました。Capistranoが便利そうだったので、使ってみました。


最新のソースは以下になります。ちまちまと改良していければと思います。(Rubyは初めてだったので、おかしな書き方があるかもしれませんが。。。)
https://github.com/mp-php/cap4git
コマンドは、上記のconfig/deploy.rbの"Usage:"を御覧ください。READMEは後日書く予定です。

アプリケーション毎に必ず設定すべき箇所は、config/deployディレクトリ以下の環境別設定ファイルと、config/deploy.rbの"Change me."です。尚、local_app_dirとremote_app_dirは一度rmするので、注意して下さい。


想定する環境は
(1) レポジトリは非公開サーバに配置
(2) 非公開サーバから公開サーバはsshでアクセス可、逆は不可
です。

やりたいことは
(3) 公開サーバからpullして更新
です。


* 以下、Capistranoの知識が浅いので、間違っていたらすみません。

Capistranoは、scmをgit、deploy_viaをremote_cacheにすると、(3)のようにレポジトリから直接pullして更新を行うようです。ただし、(2)の理由からNGでした。

deploy_viaをcopyにすると、レポジトリを公開サーバに転送して、そのレポジトリをcloneして更新を行うようです。具体的には、毎回、公開サーバ側で"releases"というディレクトリ下に新規ディレクトリを作成、そのディレクトリにclone。そして、そのディレクトリに対して、"current"というシンボリックリンクを貼るようです。また、currentを過去のリリースディレクトリに貼り替えることで、ロールバックを実現しているようです。

この仕組みは、例えば、ログファイルやキャッシュファイルが出力されるディレクトリ以下をgitignoreしている場合、それらのファイルがrelease毎に分散してしまいます。更に、公開サーバ側でcurrentとreleasesというディレクトリ構成を意識しなくてはいけません。ですので、この方法もNGでした。


上記を踏まえて、以下の方法を考えてみました。
(1) 非公開サーバのテンポラリな場所にgit clone --bareでレポジトリを複製
(2) 複製したレポジトリをtar.gzにして公開サーバにscpで転送
(3) 公開サーバのテンポラリな場所で解凍、デプロイ先からpull
* デプロイ先に".git"ディレクトリが存在しない場合はgit initを行います。

この方法なら、公開サーバ側から非公開サーバ側へのアクセスは発生しません。

既にGitでの更新を行なっているシステムでも使えるようにしたかったので、毎回
git remote rm capistrano
して
git remote add capistrano [(3)で解凍した場所]
しています。つまり、これまでoriginからpullしていたのがcapistranoからpullするようになるだけです。originとcapistranoの歴史が同一であれば、スムーズに移行出来るはずです。(はずです。というのは、実際には、まだ未確認です。。。)スムーズに移行出来ます。ついでに、pullで更新するのみでなく、指定したブランチ / タグ / ハッシュ値等をgit checkoutするrollback機能も書いておきました。

結局、sshやscp周りをCapistranoに任せて、その他は独自で実装した感じです。


以下、主な必要環境です。

▼非公開サーバ側
* Capistranoが実行できる環境
* Gitが実行できる環境
* localhost(非公開サーバ自身)に対して鍵認証(パスワード不要)によるssh接続ができる環境

▼公開サーバ
* Gitが実行できる環境

尚、処理は/bin/bashで進む想定です。


メール送信などの環境依存する問題を調査する際、公開(試験)サーバ上で直接編集、あるいはSFTP等でのアップロードから、一時的な差分が発生する場合が有ります。この差分を正としたい場合、公開サーバ上でremote capistranoにpush、ローカルからpull、非公開サーバのレポジトリにpushというフローを取らなければいけません。


次回は、このスクリプトをJenkinsからキックしてみます。

2013/01/12 Jenkinsから実行してみました。
http://madroom-project.blogspot.jp/2013/01/capistranogitjenkins.html