Using capistrano to deploy from different git branches

前端 未结 13 1023
小鲜肉
小鲜肉 2020-12-22 16:03

I am using capistrano to deploy a RoR application. The codebase is in a git repository, and branching is widely used in development. Capistrano uses deploy.rb f

相关标签:
13条回答
  • 2020-12-22 16:06
    git rev-parse --abbrev-ref HEAD
    

    will return the current branch you are exactly in.

    I always set the gpsh instead of git push -u origin branch_name

    $ which gpsh
    gpsh: aliased to git push -u origin `git rev-parse --abbrev-ref HEAD`
    
    0 讨论(0)
  • 2020-12-22 16:08

    This works with Capistrano >= 3.1:

    add this line to config/deploy.rb:

    set :branch, ENV['BRANCH'] if ENV['BRANCH']
    

    and then call capistrano with:

    cap production deploy BRANCH=master
    

    This solution works with Capistrano < 3.1:

    # call with cap -s env="<env>" branch="<branchname>" deploy
    
    set :branch, fetch(:branch, "master")
    set :env, fetch(:env, "production")
    
    0 讨论(0)
  • 2020-12-22 16:10

    I can confirm that the below still works in Cap 3.11.0 13/10/18 as well as Cap 2:

    In deploy.rb / stage.rb:

    set :branch, ENV['BRANCH'] || 'develop'
    

    On the command line:

    cap deploy BRANCH=featurex
    

    This gives you a default branch (which could be different for different environments), and the ability to change branches when you want.

    0 讨论(0)
  • 2020-12-22 16:11

    Method 1: Set stage specific branch (e.g. test, production) for deployment

    Put branch configuration in stage files instead of 'deploy.rb' and set the target branch for that stage to deploy from.

    For a two stage app with associated branch name test and production, the configuration will look like this,

    # app_root/config/deploy/test.rb
    ...
    set :branch, "test"
    ...
    
    # app_root/config/deploy/production.rb
    ...
    set :branch, "production"
    ...
    

    This method enables to deploy from stage specific branches. So, only additional step that'll be required is to merge or rebase latest code from base branch.

    Method 2: Deploy directly from any branch (using tag)

    Another approach is to deploy using tag. In order to deploy using tag, set the branch config. in 'deploy.rb' as follows,

    set :branch, `git describe --tags $(git rev-list --tags --max-count=1)`.chomp
    

    And, configure the CI to conditionally deploy to different stages if the associated tag pattern matches (e.g. /.*-test$/).

    Now, a deploy can be made from any branch,

    • First, create a tag from any branch,

      git tag -a v0.1.0-test -m "Version 0.1.0-test"

    • And, push

      git push origin v0.1.0-test

    Note: The above methods are based on Capistrano 3.

    0 讨论(0)
  • 2020-12-22 16:13

    If you're using capistrano-multistage, you only need to run

    cap -s branch=$MY_BRANCH deploy
    

    or

    cap -s branch=$MY_BRANCH production deploy
    

    without any further edit to your deploy.rb.

    0 讨论(0)
  • 2020-12-22 16:14

    This solution should work with all versions of Capistrano.

    def branch_name(default_branch)
      branch = ENV.fetch('BRANCH', default_branch)
    
      if branch == '.'
        # current branch
        `git rev-parse --abbrev-ref HEAD`.chomp
      else
        branch
      end
    end
    
    set :branch, branch_name('master')
    

    Usage:

    BRANCH=. cap [staging] deploy
    # => deploy current branch
    
    BRANCH=master cap [staging] deploy
    # => deploy master branch
    
    cap [staging] deploy
    # => deploy default branch
    
    0 讨论(0)
提交回复
热议问题