Linking to multiple subdirectories using :repo_tree

江枫思渺然 提交于 2019-12-11 15:52:15

问题


My repository is set up similar to the following:

repo_base
  - artwork
  - app
  - designsystem
  - api

Since each of the other folders in the repo (e.g. app, api, designsystem) depend on artwork, I have symlinks in place when running locally. This is working fine, as the path for images in the designsystem subdirectory is something like ../../artwork. When you check out the repository, the entire tree is checked out, so the symlinks are pointing to the correct directory.

However, when I deploy with capistrano, I use :repo_tree to only deploy a portion of the overall monorepo. For example, the deploy.rb script for the designsystem folder looks like:

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

set :application, "designsystem"
set :repo_url, "git@gitlab.com:myuser/mymonorepo"
set :deploy_to, "/var/www/someplace.net/designsystem.someplace.net"
set :deploy_via, "remote_cache_with_project_root"
set :repo_tree, 'designsystem'
set :log_level, :error

before 'deploy:set_current_revision', 'deploy:buildMonolith'

The problem, of course, is that this only ends up deploying the designsystem subdirectory. Thus, the symlinks aren't valid, and are actually skipped in the building (buildMonolith step).

I'm wondering how I might go about having capistrano check out another subdirectory, artwork, and placing it somewhere in the repository source tree.


回答1:


I was able to solve this by adding a capistrano task called assets.rb:

require 'pathname'

##
# Import assets from a top level monorepo directory into the current working
# directory.
#
# When you use :repo_tree to deploy a specific directory of a monorepo, but your
# asset repository is in a different directory, you need to check out this
# top-level directory and add it to the deployment path. For example, if your
# monorepo directory structure looks something like:
#
# - /app
#   - src/
#     - assets -> symlink to ../../assets
# - /assets
# - /api
#
# And you want to deploy /app, the symlink to the upper directory won't exist if
# capistrano is configured to use :repo_tree "app". In order to overcome this,
# this task checks out a specified piece of the larger monorepo (in this case,
# the assets directory), and places it in the deployment directory at a
# specified location.
#
# Configuration:
# In your deploy/<stage>.rb file, you will need to specify two variables:
#   - :asset_path - The location within the deployment directory where the
#                   assets should be placed. Relative to the deployment working
#                   directory.
#   - :asset_source - The location of the top-level asset folder in the
#                     monorepo. Relative to the top level of the monorepo (i.e.
#                     the directory that would be used as a deployment if
#                     :repo_tree was not specified).
#
# In the above example, you would specify:
#
# set :asset_path, "src/assets"
# set :asset_source, "assets"
#
namespace :deploy do
  desc "Import assets from a top-level monorepo directory"
  task :import_assets do
   on roles(:all) do |host|
      within repo_path do
        final_asset_location = "#{release_path}/#{fetch(:asset_path)}"
        asset_stat_result = capture "stat", "-t", "#{final_asset_location}"
        asset_stat_result = asset_stat_result.split(" ")
        if asset_stat_result[0] == "#{final_asset_location}"
          info "Removing existing asset directory #{final_asset_location}..."
          execute "rm", "-rf", "#{final_asset_location}"
        end

        source_dir = Pathname.new(final_asset_location).parent.to_s
        info "Importing assets to #{source_dir}/#{fetch(:asset_source)}"
        execute "GIT_WORK_TREE=#{source_dir}", :git, "checkout", "#{fetch(:branch)}", "--", "#{fetch(:asset_source)}"

        info "Moving asset directory #{source_dir}/#{fetch(:asset_source)} to #{final_asset_location}..."
        execute :mv, "#{source_dir}/#{fetch(:asset_source)}", "#{final_asset_location}"
      end
    end
  end
end

It would be nice if I could somehow link into the git scm plugin, rather than calling git from the command line directly.



来源:https://stackoverflow.com/questions/57360952/linking-to-multiple-subdirectories-using-repo-tree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!