Vagrant + Jenkins ã® CI ã AWS ã§ãåã
æ¨æ© Jenkins 㨠Vagrant 㧠CI ã ãã¨æ¸ããã
ã¨ããåå¿ããã£ãã確ãã«ãå¯è½ãªãç©çãµã¼ãã«ä¾åããªãå½¢ã§ãã¹ãã§ããã¨ããå¬ããå ´é¢ãããããã§ããã
ããããã㯠VagrantãVagrant ã¯ãã¼ã¸ã§ã³ 1.1 ãããããã¯ã¨ã³ãã VirtualBox ã ãã§ãªã AWS (EC2) ãªã©ã® IaaS ãæå®ãã¦ä»®æ³ãµã¼ãã¼ãä½ã£ããå£ãããã§ããããã«ãªã£ã¦ãã¾ãã詳ãã㯠http://d.hatena.ne.jp/naoya/20130315/1363340698 ãã®è¾ºãããã®æ©è½ãå©ç¨ããã°æ¨æ¥ã® Jenkins + Vagrant ã®ããã¼ãã»ã¨ãã©å¤ããã«ãEC2 ã®ã¤ã³ã¹ã¿ã³ã¹ã§ã®ã¤ã³ãã°ã¬ã¼ã·ã§ã³ãã¹ããã§ãããã§ããã
éè¦ããã¿ã¡ãã§ã¯ãæ©éãã£ã¦ããã¾ããããã
Multi VM ã§ãã¼ã«ã«/ãªã¢ã¼ã両対å¿ã«
ãã£ãããªã®ã§ VirtualBox ã使ã£ããã¼ã«ã«ã§ã®ãã¹ãã¨ãEC2 ã使ã£ããªã¢ã¼ãã§ã®ãã¹ãã両æ¹ãæµããããã«ããããVagrant ã® Multi VM ã使ãã°ä¸ã¤ã® Vagrantfile ã§è¤æ°ã®ä»®æ³ãµã¼ãã¼ã管çããã®ãç°¡åã§ãã
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.define :local do |local| local.vm.box = "centos" end config.vm.define :remote do |remote| remote.vm.box = "dummy" remote.vm.provider :aws do |aws, override| aws.access_key_id = ENV['AWS_ACCESS_KEY_ID'] aws.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY'] aws.keypair_name = "naoya's keys" aws.instance_type = "t1.micro" aws.region = "ap-northeast-1" aws.ami = "ami-a3af2ba2" aws.security_groups = [ 'webserver' ] aws.tags = { 'Name' => 'jenkins-test' } override.ssh.username = "ec2-user" override.ssh.private_key_path = "~/.ssh/aws-naoyaskeys.pem" end end end
ãããªæããããã§
% vagrant up local
ã¨ããã°ãã¤ãéã VirtualBox ã®ãµã¼ãã¼ãç«ã¡ä¸ãã
% vagrant up remote --provider=aws
㧠EC2 ã®ã¤ã³ã¹ã¿ã³ã¹ãç«ã¡ä¸ããã¾ããvagrant up
ã§ä¸¡æ¹åæã«ç«ã¡ä¸ãããã¨ãå¯è½ã§ãã
Jenkins ã®ãã©ã¡ã¼ã¿ä»ããã«ãã§æ¯ãåãã
Jenkins ã«ã¯ããã«ãã®ãã©ã¡ã¼ã¿åããªãè¨å®é ç®ããã£ã¦ãããã使ãã¨ã¦ã¼ã¶ã¼ããã®å ¥åå¤ã«å¿ãã¦ãã«ãã®å 容ãåçã«å¤æ´ããããããã¨ãã§ãããä¾ãã°ãGit ããåã£ã¦ãããã©ã³ãåããã«ãã®æ¯ã«æå®ãããã¨ãã«ã使ããããããã使ã£ã¦å ã® local / remote ã® VM åãé¸æã§ããããã«ãã¾ãã
ããã§è¨å®ããå¤ã¯ãã«ãã®ã·ã§ã«ã¹ã¯ãªããããç°å¢å¤æ°ã¨ãã¦åç §ã§ããã®ã§ããããã¾ã使ã£ã¦
if [ $VM_NAME = 'remote' ]; then vagrant up remote --provider=aws else vagrant up local fi vagrant ssh-config $VM_NAME --host=jenkingrant > vagrant-ssh.conf bundle bundle exec knife solo bootstrap jenkingrant -F vagrant-ssh.conf bundle exec rake ci:setup:rspec spec rm -f vagrant-ssh.conf vagrant destroy $VM_NAME -f
ã¨ããå ·åã«ãå¤æ°ãè¦ã¦ãããã® VM ãèµ·åããããé¸æããããã«ããã
ããã§ãã«ãã®éã«ãã©ã¡ã¼ã¿ã®å ¥åãè¦æ±ãããããã«ãªãã
ãã¹ããã¦ã¿ã
ã¡ããã¨ãã¾ããã£ã¦ããã§ãããããå®éã« Jenkins ã®ã³ã³ã½ã¼ã«ãè¦ã¦ã¿ã¾ããä¸é¨æç²ã
[workspace] $ /bin/sh -xe /var/folders/nl/vdz7nrv12zj44kcjg_vmpr1r0000gn/T/hudson5435348702666934788.sh + '[' remote = remote ']' + vagrant up remote --provider=aws Bringing machine 'remote' up with 'aws' provider... [remote] Warning! The AWS provider doesn't support any of the Vagrant high-level network configurations (`config.vm.network`). They will be silently ignored. [remote] Launching an instance with the following settings... [remote] -- Type: t1.micro [remote] -- AMI: ami-a3af2ba2 [remote] -- Region: ap-northeast-1 [remote] -- SSH Port: 22 [remote] -- Keypair: naoya's keys [remote] -- Security Groups: ["webserver"] [remote] Waiting for instance to become "ready"... [remote] Waiting for SSH to become available... [remote] Machine is booted and ready for use! [remote] Rsyncing folder: /Users/naoya/.jenkins/jobs/jenkingrant/workspace/ => /vagrant + vagrant ssh-config remote --host=jenkingrant + bundle Resolving dependencies... Using rake (10.0.4) Using builder (3.2.0)
EC2 ã¤ã³ã¹ã¿ã³ã¹ãç«ã¡ä¸ãã£ã¦ãã® VM ã使ããã¦ããã®ãåããã¾ããä¸æã«ç¼ãã¾ãããããæå¾ã«ãªãªã¼ããªã¤ã«ããããã®ããå¿ããªãã追ããªãªã¼ãã
ãªãããã©ã¡ã¼ã¿ä»ããã«ãã¯ãå½ç¶ã®ãã¨ãªããæ¯åæåã§ãã©ã¡ã¼ã¿ãå ¥åããã®ã§ã¯ãªããã«ãå®è¡ã®URLã kick ããéã«ãã©ã¡ã¼ã¿ãæå®ãããã¨ãã£ããã¨ãå¯è½ã§ãããã®ããã㯠Jenkinsで外部パラメータで与えたブランチを対象にビルドできるようにしておくと凄惨性あがって墓ドル - ( ꒪⌓꒪) ゆるよろ日記 ãªã©ãåç §ã
è足ã§ããããã®ãã¹ãã¯è½ã¡ãéã«ãªããã¹ããè½ã¡ããã対象ãµã¼ããè¦ã¦æ¤è¨¼ã§ããããã«ããµã¼ãã¼ã®ç ´æ£ã¯ããªãããã«ãã¦ãã¾ããVirtualBox ãªãåé¡ãªãã§ãããEC2 ã ã¨ãã¹ãã失æããã¨ããã®ã¾ã¾ã¤ã³ã¹ã¿ã³ã¹ãç«ã¡ä¸ããã£ã±ãªãã ã¨èª²éãçºçãã¦ãã¾ãã®ã§ããã«ãå¾ã«å¿ ã vagrant destroy ãèµ°ãããã«ããæ¹ãããããããã¾ããã
ãã®è¾ºãèªåã¯ä»ã®ã¨ãã Jenkins Notifier for Mac OS X ãå ¥ãã¦éç¥ãåºãã¦æ¾ç½®ããªãããã«æ°ãã¤ãããã¨ã§åé¿ãã¦ããã¾ãã
ããã¿ã¡ãä»æ¥ãããããã§ãã¾ãããï¼ã