前回書いた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