Capistrano部署上线项目

07-26 22:12 字数 6634 阅读 226 已编辑

Capistrano 官网 https://capistranorb.com/documentation/getting-started/installation/

复制代码

Capistrano 是由 ruby 写的因此需要安装 ruby

sudo yum install ruby

因为使用yum安装的ruby过低导致capistrano安装失败,所以通过源码安装最新版本,首先卸载老的ruby

sudo yum remove ruby

ruby源码下载地址 http://www.ruby-lang.org/en/downloads/

以ruby2.6.3为例

wget https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.3.tar.gz

ruby安装步骤官方文档地址 http://www.ruby-lang.org/en/documentation/installation/#building-from-source

复制代码
Building from Source
Of course, you can install Ruby from source. Download and unpack a tarball, then just do this:

$ ./configure
$ make
$ sudo make install

By default, this will install Ruby into /usr/local. To change, pass the --prefix=DIR option to the ./configure script.

You can find more information about building from source in the Ruby README file.

Using the third-party tools or package managers might be a better idea, though, because the installed Ruby won’t be managed by any tools.

如果没有在/usr/bin下生成可执行文件的话可以做软连接

$ sudo ln -s /usr/local/ruby/bin/ruby /usr/bin/ruby
$ sudo ln -s /usr/local/ruby/bin/gem /usr/bin/gem

正确的删除方式(删除软链接,但不删除实际数据)

rm -rf  ./test_chk_ln

错误的删除方式

rm -rf ./test_chk_ln/ (这样就会把原来test_chk下的内容删除)

capistranorb安装

官方安装文档:https://capistranorb.com/documentation/getting-started/installation/

复制代码

General Usage The following command will install the latest released capistrano v3 revision:

$ gem install capistrano

Or grab the bleeding edge head from:

$ git clone https://github.com/capistrano/capistrano.git
$ cd capistrano
$ gem build *.gemspec
$ gem install *.gem

复制代码
SSH
Capistrano deploys using SSH. Thus, you must be able to SSH (ideally with keys and ssh-agent) from the deployment system to the destination system for Capistrano to work.
You can test this using a ssh client, e.g. ssh myuser@destinationserver. If you cannot connect at all, you may need to set up the SSH server or resolve firewall/network issues. Look for a tutorial (here are suggestions for Ubuntu and RedHat/CentOS).

If a password is requested when you log in, you may need to set up SSH keys. GitHub has a good tutorial on creating these (follow steps 1 through 3). You will need to add your public key to ~/.ssh/authorized_keys on the destination server as the deployment user (append on a new line).

More information on SSH and login is available via the Authentication and Authorisationsection of the guide.

使用和配置

在公共用户下如aspire下新建项目的部署目录如deploy_shop,执行

复制代码

$ cap install

生成如下目录

├── Capfile
├── config
│ ├── deploy
│ │ ├── production.rb
│ │ └── staging.rb
│ └── deploy.rb
└── lib
└── capistrano
└── tasks

复制代码

config/deploy中可以配置版本测试版本,预发版本,生成版本;如下是deploy.rb的配置,可以指定仓库类型svn见官网默认git,指定分支默认master

复制代码

config valid for current version and patch releases of Capistrano lock "~> 3.11.0"

set :application 设置应用的名称随便
set :application, "shop" 
set :repo_url 设置仓库地址默认git
set :repo_url, "http://php-deployer@git.xxxxx.com/scm/abc.git"
git config --global credential.helper store  git记住密码

Default branch is :master
ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

Default deploy_to directory is /var/www/my_app_name;设置代码要部署的目录
set :deploy_to, "/var/www/my_app_name"
set :deploy_to, "/var/www/test_shop_web"

Default value for :format is :airbrussh.
set :format, :airbrussh

You can configure the Airbrussh format using :format_options.
These are the defaults.
set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto

Default value for :pty is false
set :pty, true

Default value for :linked_files is [];设置具体共享文件
append :linked_files, "config/database.yml"
append :linked_files, "config.inc.php", "define.inc.php"

Default value for linked_dirs is [];设置共享目录
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"
append :linked_dirs, "data"

Default value for default_env is {}
set :default_env, { path: "/opt/ruby/bin:$PATH" }

Default value for local_user is ENV['USER']
set :local_user, -> { `git config user.name`.chomp }

Default value for keep_releases is 5
set :keep_releases, 5   设置保留最近的版本个数以便回滚操作

复制代码

分布式配置deploy/目录如预发版本plus.rb

复制代码

server-based syntax
======================
Defines a single server with a list of roles and multiple properties.
You can define all roles on a single server, or split them:

server "example.com", user: "deploy", roles: %w{app db web}, my_property: :my_value
server "example.com", user: "deploy", roles: %w{app web}, other_property: :other_value
server "db.example.com", user: "deploy", roles: %w{db}

role-based syntax
==================

Defines a role with one or multiple servers. The primary server in each
group is considered to be the first unless any hosts have the primary
property set. Specify the username and a domain or IP for the server.
Don't use `:all`, it's a meta role.

role :app, %w{deploy@example.com}, my_property: :my_value
role :web, %w{user1@primary.com user2@additional.com}, other_property: :other_value
role :web, %w{aspire@127.0.2.1}
role :db,  %w{deploy@example.com}

Configuration
=============
You can set any configuration variable like in config/deploy.rb
These variables are then only loaded and set in this stage.
For available Capistrano configuration variables see the documentation page.
http://capistranorb.com/documentation/getting-started/configuration/
Feel free to add new variables to customise your setup.



Custom SSH Options
==================
You may pass any option but keep in mind that net/ssh understands a
limited set of options, consult the Net::SSH documentation.
http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start

Global options
--------------

set :ssh_options, {
   keys: %w(/home/rlisowski/.ssh/id_rsa),
   forward_agent: false,
   auth_methods: %w(password)
}
set :ssh_options, {
  keys: %w(/home/aspire/.ssh/authorized_keys),
  forward_agent: false,
  auth_methods: %w(publickey)
 }

set :deploy_to, "/var/www/test_shop_web"

设置部署目录从deploy.rb中拷贝而来方便,预发和正式目录分开

set :ssh_options, {
  user: "uname", # overrides user setting above
  forward_agent: false,
  auth_methods: %w(password),#授权方式
  password: "1234qwer"#指定ssh用户登录密码
}

The server-based syntax can be used to override options:
------------------------------------
server "example.com",
   user: "user_name",
   roles: %w{web app},
   ssh_options: {
     user: "user_name", # overrides user setting above
     keys: %w(/home/user_name/.ssh/id_rsa),
     forward_agent: false,
     auth_methods: %w(publickey password)
     # password: "please use keys"
}

复制代码

通常情况下需要执行cap plus deploy会因为用户没有设置密码而抛出异常,为了免密码登录和不在配置文件中显式指定用户密码,通常用ssh建立信任关系

文档 https://capistranorb.com/documentation/getting-started/authentication-and-authorisation/

复制代码

创建部署用户

$ adduser deploy
$ passwd -l deploy

创建key

$ ssh-keygen -t rsa -C 'me@my_email_address.com'

To avoid having to type this passphrase every time you need to use a key, most operating systems have a concept of a key agent.

This key agent stores SSH keys securely between uses, typically the first time a key is needed in a given time period, the SSH agent will load the key, prompt you for your passphrase and then the key agent will remember the key for a certain amount of time (on OSX it tends to be indefinite, on linux this can vary from 15 minutes upwards.)
We can see which keys are loaded in the SSH agent by running ssh-add -l

$ ssh-add -l

If you don’t see any keys listed, you can simply run ssh-add:me@localhost $ ssh-add
Identity added: /Users/me/.ssh/id_rsa (/Users/me/.ssh/id_rsa)

如果系统提示:could not open a connection to your authentication agent 则需要执行以下命令:ssh-agent bash 进入到~/.ssh中

$ cat id_rsa.pub >> ~/.ssh/authorized_keys
$ chmod 700 .ssh
$ chmod 600 .ssh/authorized_keys

复制代码

至此,不需要密码输入的上线系统部署完成,具体的需要按照官方文档修改配置

0人点赞>
关注 收藏 改进 举报
1 条评论
排序方式 时间 投票
Up骚年

帮们更新了排版。Markdown 使用指南 https://www.shiqidu.com/d/164

请登录后发表评论