|  | 
| 43 | 43 | 
 | 
| 44 | 44 | // Sets deploy:update_code strategy. | 
| 45 | 45 | // Can be one of: | 
| 46 |  | -// - archive | 
| 47 |  | -// - clone (if you need the origin repository `.git` dir in your {{release_path}}) | 
|  | 46 | +// - local_archive (copies the repository from local machine) | 
|  | 47 | +// - archive (default, fetches the code from the remote repository) | 
|  | 48 | +// - clone (if you need the origin repository `.git` dir in your {{release_path}}, clones from remote repository) | 
| 48 | 49 | set('update_code_strategy', 'archive'); | 
| 49 | 50 | 
 | 
| 50 | 51 | // Sets environment variable _GIT_SSH_COMMAND_ for `git clone` command. | 
|  | 
| 55 | 56 | 
 | 
| 56 | 57 | /** | 
| 57 | 58 |  * Specifies a sub directory within the repository to deploy. | 
| 58 |  | - * Works only when [`update_code_strategy`](#update_code_strategy) is set to `archive` (default). | 
|  | 59 | + * Works only when [`update_code_strategy`](#update_code_strategy) is set to `archive` (default) or `local_archive`. | 
| 59 | 60 |  * | 
| 60 | 61 |  * Example: | 
| 61 | 62 |  *  - set value to `src` if you want to deploy the folder that lives at `/src`. | 
|  | 
| 70 | 71 |  */ | 
| 71 | 72 | desc('Updates code'); | 
| 72 | 73 | task('deploy:update_code', function () { | 
| 73 |  | -    $git = get('bin/git'); | 
| 74 |  | -    $repository = get('repository'); | 
|  | 74 | +    $strategy = get('update_code_strategy'); | 
| 75 | 75 |     $target = get('target'); | 
| 76 | 76 | 
 | 
| 77 |  | -    if (empty($repository)) { | 
| 78 |  | -        throw new ConfigurationException("Missing 'repository' configuration."); | 
| 79 |  | -    } | 
| 80 |  | - | 
| 81 | 77 |     $targetWithDir = $target; | 
| 82 | 78 |     if (!empty(get('sub_directory'))) { | 
| 83 | 79 |         $targetWithDir .= ':{{sub_directory}}'; | 
| 84 | 80 |     } | 
| 85 | 81 | 
 | 
| 86 |  | -    $bare = parse('{{deploy_path}}/.dep/repo'); | 
| 87 |  | -    $env = [ | 
| 88 |  | -        'GIT_TERMINAL_PROMPT' => '0', | 
| 89 |  | -        'GIT_SSH_COMMAND' => get('git_ssh_command'), | 
| 90 |  | -    ]; | 
| 91 |  | - | 
| 92 |  | -    start: | 
| 93 |  | -    // Clone the repository to a bare repo. | 
| 94 |  | -    run("[ -d $bare ] || mkdir -p $bare"); | 
| 95 |  | -    run("[ -f $bare/HEAD ] || $git clone --mirror $repository $bare 2>&1", env: $env); | 
| 96 |  | - | 
| 97 |  | -    cd($bare); | 
| 98 |  | - | 
| 99 |  | -    // If remote url changed, drop `.dep/repo` and reinstall. | 
| 100 |  | -    if (run("$git config --get remote.origin.url") !== $repository) { | 
| 101 |  | -        cd('{{deploy_path}}'); | 
| 102 |  | -        run("rm -rf $bare"); | 
| 103 |  | -        goto start; | 
| 104 |  | -    } | 
| 105 |  | - | 
| 106 |  | -    run("$git remote update 2>&1", env: $env); | 
|  | 82 | +    if ($strategy === 'local_archive') { | 
|  | 83 | +        $host = currentHost()->connectionString(); | 
| 107 | 84 | 
 | 
|  | 85 | +        // Copy to release_path. | 
|  | 86 | +        runLocally(<<<BASH | 
|  | 87 | +            git archive {$targetWithDir} | ssh {$host} "tar -x -f - -C {{release_path}} 2>&1" | 
|  | 88 | +            BASH); | 
| 108 | 89 | 
 | 
| 109 |  | -    // Copy to release_path. | 
| 110 |  | -    if (get('update_code_strategy') === 'archive') { | 
| 111 |  | -        run("$git archive $targetWithDir | tar -x -f - -C {{release_path}} 2>&1"); | 
| 112 |  | -    } elseif (get('update_code_strategy') === 'clone') { | 
| 113 |  | -        cd('{{release_path}}'); | 
| 114 |  | -        run("$git clone -l $bare ."); | 
| 115 |  | -        run("$git remote set-url origin $repository", env: $env); | 
| 116 |  | -        run("$git checkout --force $target"); | 
|  | 90 | +        $rev = escapeshellarg(runLocally("git rev-list $target -1")); | 
| 117 | 91 |     } else { | 
| 118 |  | -        throw new ConfigurationException(parse("Unknown `update_code_strategy` option: {{update_code_strategy}}.")); | 
|  | 92 | +        $git = get('bin/git'); | 
|  | 93 | +        $repository = get('repository'); | 
|  | 94 | + | 
|  | 95 | +        if (empty($repository)) { | 
|  | 96 | +            throw new ConfigurationException("Missing 'repository' configuration."); | 
|  | 97 | +        } | 
|  | 98 | + | 
|  | 99 | +        $bare = parse('{{deploy_path}}/.dep/repo'); | 
|  | 100 | +        $env = [ | 
|  | 101 | +            'GIT_TERMINAL_PROMPT' => '0', | 
|  | 102 | +            'GIT_SSH_COMMAND' => get('git_ssh_command'), | 
|  | 103 | +        ]; | 
|  | 104 | + | 
|  | 105 | +        start: | 
|  | 106 | +        // Clone the repository to a bare repo. | 
|  | 107 | +        run("[ -d $bare ] || mkdir -p $bare"); | 
|  | 108 | +        run("[ -f $bare/HEAD ] || $git clone --mirror $repository $bare 2>&1", env: $env); | 
|  | 109 | + | 
|  | 110 | +        cd($bare); | 
|  | 111 | + | 
|  | 112 | +        // If remote url changed, drop `.dep/repo` and reinstall. | 
|  | 113 | +        if (run("$git config --get remote.origin.url") !== $repository) { | 
|  | 114 | +            cd('{{deploy_path}}'); | 
|  | 115 | +            run("rm -rf $bare"); | 
|  | 116 | +            goto start; | 
|  | 117 | +        } | 
|  | 118 | + | 
|  | 119 | +        run("$git remote update 2>&1", env: $env); | 
|  | 120 | + | 
|  | 121 | +        // Copy to release_path. | 
|  | 122 | +        if ($strategy === 'archive') { | 
|  | 123 | +            run("$git archive $targetWithDir | tar -x -f - -C {{release_path}} 2>&1"); | 
|  | 124 | +        } else if ($strategy === 'clone') { | 
|  | 125 | +            cd('{{release_path}}'); | 
|  | 126 | +            run("$git clone -l $bare ."); | 
|  | 127 | +            run("$git remote set-url origin $repository", env: $env); | 
|  | 128 | +            run("$git checkout --force $target"); | 
|  | 129 | +        } else { | 
|  | 130 | +            throw new ConfigurationException(parse("Unknown `update_code_strategy` option: {{update_code_strategy}}.")); | 
|  | 131 | +        } | 
|  | 132 | + | 
|  | 133 | +        $rev = escapeshellarg(run("$git rev-list $target -1")); | 
| 119 | 134 |     } | 
| 120 | 135 | 
 | 
| 121 | 136 |     // Save git revision in REVISION file. | 
| 122 |  | -    $rev = escapeshellarg(run("$git rev-list $target -1")); | 
| 123 | 137 |     run("echo $rev > {{release_path}}/REVISION"); | 
| 124 | 138 | }); | 
0 commit comments