Last active
November 17, 2019 21:14
-
-
Save iandunn/21f5e7cc20fed6bf2a237dfec4d8cbdd to your computer and use it in GitHub Desktop.
Relative symlinks for Deployer -- https://github.com/deployphp/deployer/issues/503
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Create symlinks for shared directories and files. | |
* | |
* This is a copy of the default `deploy:shared` task, but modified to create relative symlinks. See | |
* `deploy:symlink` for details. | |
*/ | |
task( 'deploy:shared', function() { | |
$sharedPath = "{{deploy_path}}/shared"; | |
foreach ( get( 'shared_dirs' ) as $dir ) { | |
// Remove from source. | |
run( "if [ -d $(echo {{release_path}}/$dir) ]; then rm -rf {{release_path}}/$dir; fi" ); | |
// Create shared dir if it does not exist. | |
run( "mkdir -p $sharedPath/$dir" ); | |
// Create path to shared dir in release dir if it does not exist. | |
// (symlink will not create the path and will fail otherwise) | |
run( "mkdir -p `dirname {{release_path}}/$dir`" ); | |
// Symlink shared dir to release dir | |
$change_directory = 'cd {{release_path}}/' . dirname( $dir ); | |
$parent_levels = str_repeat( '../', substr_count( $dir, '/' ) + 2 ); | |
$create_symlink = "ln -nfs {$parent_levels}shared/$dir " . basename( $dir ); | |
run( "$change_directory && $create_symlink" ); | |
} | |
foreach ( get( 'shared_files' ) as $file ) { | |
$dirname = dirname( $file ); | |
// Remove from source. | |
run( "if [ -f $(echo {{release_path}}/$file) ]; then rm -rf {{release_path}}/$file; fi" ); | |
// Ensure dir is available in release | |
run( "if [ ! -d $(echo {{release_path}}/$dirname) ]; then mkdir -p {{release_path}}/$dirname;fi" ); | |
// Create dir of shared file | |
run( "mkdir -p $sharedPath/" . $dirname ); | |
// Touch shared | |
run( "touch $sharedPath/$file" ); | |
// Symlink shared dir to release dir | |
$change_directory = 'cd {{release_path}}/' . dirname( $file ); | |
$parent_levels = str_repeat( '../', substr_count( $file, '/' ) + 2 ); | |
$create_symlink = "ln -sfn {$parent_levels}shared/$file " . basename( $file ); | |
run( "$change_directory && $create_symlink" ); | |
} | |
} )->desc( 'Creating symlinks for shared files' ); | |
/** | |
* Create symlink to last release. | |
* | |
* This is a copy of the default `deploy:symlink` task, but modified to create relative symlinks. | |
* | |
* See https://github.com/deployphp/deployer/issues/503 | |
* | |
* On many shared hosting platforms, the user is chroot'd into a child directory, so absolute symlinks | |
* fail because they require permission to execute on each folder in the path, all the way up to / | |
* - https://unix.stackexchange.com/a/21339/18886 | |
* - https://gist.github.com/mkrisher/74721/revisions#diff-01cf30e04b9c4489ed9e927b99e3b66dR45 | |
* | |
* Also, the symlinks need to be set relative to the symlink's target directory. | |
* - https://unix.stackexchange.com/a/15285/18886 | |
* - https://askubuntu.com/a/524635 | |
*/ | |
task( 'deploy:symlink', function() { | |
$relative_release_path = '.' . substr( | |
env( 'release_path' ), | |
strlen( env( 'deploy_path' ) ) | |
); | |
env( 'relative_release_path', $relative_release_path ); | |
run( "cd {{deploy_path}} && ln -sfn {{relative_release_path}} current" ); // Atomic override symlink. | |
run( "cd {{deploy_path}} && rm release" ); // Remove release link. | |
} )->desc( 'Creating symlink to release' ); | |
/** | |
* Rollback to previous release. | |
* | |
* This is a copy of the default `rollback` task, but modified to create relative symlinks. See | |
* `deploy:symlink` for details. | |
*/ | |
task( 'rollback', function() { | |
$releases = env( 'releases_list' ); | |
if ( isset( $releases[1] ) ) { | |
$releaseDir = "./releases/{$releases[1]}"; | |
// Symlink to old release. | |
run( "cd {{deploy_path}} && ln -nfs $releaseDir current" ); | |
// Remove release | |
run( "rm -rf ./releases/{$releases[0]}" ); | |
writeln( "Rollback to `{$releases[1]}` release was successful." ); | |
} else { | |
writeln( "<comment>No more releases you can revert to.</comment>" ); | |
} | |
} )->desc( 'Rollback to previous release' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment