facebook twitter hatena line email

Jenkins/git連携

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

jenkinsにgitを入れる

$ wget -O jenkins-cli.jar http://localhost:8080/jnlpJars/jenkins-cli.jar
$ java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin git
$ java -jar jenkins-cli.jar -s http://localhost:8080 safe-restart
$ rm jenkins-cli.jar
$ /etc/rc.d/init.d/jenkins restart
$ cd /var/lib/jenkins/.ssh
$ sudo -u jenkins -H ssh-keygen -t rsa -C jenkins@example.jp
$ chmod 700 ../.ssh

githubのときはこちらも

$ sudo -u jenkins ssh -T git@github.com
$ sudo -u jenkins git config --global user.email "jenkins@example.jp"
$ sudo -u jenkins git config --global user.name "jenkins"

以下エラーが出るとき

ERROR: Error cloning remote repo 'origin'

jenkins側サーバの

vi /var/lib/jenkins/.ssh/id_rsa.pub

をgit側サーバの

vi /home/[user1]/.ssh/authorized_keysに追加
  1. 一度jenkinsユーザでssh接続
sudo -u jenkins ssh user1@server1

管理画面設定(git

  • リポジトリURLにssh://[uname]@[localhost]/var/git/[repo1].git
  • Branches to buildをmasterに

管理画面設定(github

  • リポジトリURLにssh://git@github.com/[uname]/[repo1].git
  • Branches to buildをmasterに

テンプレ配信

  1. 管理画面でビルド/シェル実行を追加し
  2. 以下を追加
sh /var/lib/jenkins/deploy/deploy.sh
  1. rsyncのsshのnoパス設定追加
  2. 一度jenkinsユーザでssh接続
sudo -u jenkins ssh user1@server1

例:fuelphp

  • /var/lib/jenkins/deploy/deploy.sh
#!/bin/bash
DIR=$(cd $(dirname $0);pwd)
SERVERS="user1@127.0.0.2 user1@127.0.0.2"
SERVER_DIR=/var/www/fuelphp/project1
EXCLUDE=$DIR/conf/exclude.conf
GIT_REPO_DIR=/var/lib/jenkins/jobs/project1_deploy/workspace/
if [ `whoami` != "jenkins" ]; then
echo "It is not jenkins user. Please run with jenkins user."
    exit
fi
echo "*****************************************************************"
echo 'timestamp set'
for FILE in `git ls-files`; do
  if fuel/app/views|fuel/app/themes ; then
    TIME=`git log --pretty=format:%ci -n1 $FILE`
    echo $TIME'\t'$FILE
    STAMP=`date -d "$TIME" +"%y%m%d%H%M.%S"`
    touch -t $STAMP $FILE
  fi
done
#echo "*****************************************************************"
#echo 'dry-run exec'
#for SERVER in $SERVERS; do
#  echo "-${SERVER}---------------------------------------"
#  echo "rsync --dry-run -avuz --exclude-from=${EXCLUDE} ${GIT_REPO_DIR} ${SERVER}::${SERVER_DIR} --links --delete-after"
#  rsync --dry-run -avuz --exclude-from=${EXCLUDE} ${GIT_REPO_DIR} ${SERVER}::${SERVER_DIR} --links --delete-after| grep -v /$
#done
echo "*****************************************************************"
echo 'deploy exec'
for SERVER in $SERVERS; do
  echo "-${SERVER}---------------------------------------"
  echo `date` >> $LOG_FILE_RSYNC
  echo "rsync -avuz --exclude-from=${EXCLUDE} ${GIT_REPO_DIR} ${SERVER}::${SERVER_DIR} --links --delete-after"
  rsync -avuz --exclude-from=${EXCLUDE} ${GIT_REPO_DIR} ${SERVER}::${SERVER_DIR} --links --delete-after| grep -v /$
done
echo 'deploy completed!!'
  • /var/lib/jenkins/deploy/conf/exclude.conf
*.tmp
logs
.gitmodules
CHANGELOG.md
CONTRIBUTING.md
README.md
TESTING.md
composer.json
composer.lock
composer.phar
public/index.php
fuel/vendor
fuel/packages
fuel/core
fuel/app/vendor
fuel/app/migrations
fuel/app/config
fuel/app/classes
fuel/LICENSE
fuel/.htaccess
oil
fuel/app/tasks
fuel/app/bootstrap.php
public/.htaccess