| Q | A |
| --- | --- |
| Issue Type | Feature Request |
| Deployer Version | master |
| Local Machine OS | N/A |
| Remote Machine OS | N/A |
Verify the remote site before applying changes.
<?php
\Deployer\Server\Environment::setDefault( 'branch', 'master' );
task(
'git:validate',
function () {
// check if directory exists and is under GIT control
$result = run( 'if [ -d {{current}} ] && [ -d {{current}}/.git ]; then echo true; fi' );
if ( ! $result->toBool() ) {
if ( isVerbose() ) {
writeln( ' <comment>No GIT repository found - skipping.</comment>' );
}
return;
}
$cmd = 'git -C ' . escapeshellarg( env( 'current' ) ) . ' ';
try {
// update index
run( $cmd . 'fetch' );
run( $cmd . 'update-index -q --ignore-submodules --refresh' );
} catch ( \RuntimeException $e ) {
writeln( '<error>Could not update index.</error>' );
die( 1 );
}
try {
// check unstaged
run( $cmd . 'diff-files --quiet --ignore-submodules --' );
// Disallow uncommitted changes in the index
run( $cmd . 'diff-index --cached --quiet HEAD --ignore-submodules --' );
} catch ( \RuntimeException $e ) {
writeln( '<error>Remote site has changed files.</error>' );
die( 1 );
}
try {
// check for unpushed commits
$branch = env( 'branch' );
$result = run( $cmd . 'log --pretty="format:%h: %s" origin/' . $branch . '..' . $branch );
$output = array_filter( $result->toArray() );
if ( $output ) {
writeln( '<error>You have ' . count( $output ) . ' unpushed commit(s).</error>' );
write( implode( ' ' . PHP_EOL, $output ) . PHP_EOL );
die( 1 );
}
} catch ( \RuntimeException $e ) {
writeln( '<error>Could not compare branch with origin branch.</error>' );
die( 1 );
}
}
);
Something like that.
I think this can go to new Hot-To.
I keep updating the code in the description from time to time.
This is something that I use which does some related (but not equal) tasks. Probably also better as a HowTo article.
/**
* Start by checking that the current git branch is:
* - master
* - fully pushed
*/
task('deploy:checkgit', function() {
if (trim($branch = `git rev-parse --abbrev-ref HEAD`) !== 'master') {
writeln("<error>You should release from master, not $branch.</error>");
exit(1);
}
// check that there are no modified files
exec("git diff-index --quiet HEAD", $output, $exitCode);
if ($exitCode) {
writeln("<error>Branch is not clean, not starting a deploy process.</error>");
exit(1);
}
// update remotes and check git status
`git remote update`;
$local = trim(`git rev-parse @`);
$remote = trim(`git rev-parse @{u}`);
if ($local !== $remote) {
writeln("<error>Your `master` is not equal to `origin/master`, you should push first.</error>");
exit(1);
}
})->once()->desc('Check that git is clean.');
I think it is not a deployment issue, it's an issue of your corporate processes and workflows. Changes on servers without commiting in VCS is a very bad practice. So it's a very specific task. But hopefully Deployer allows us to define custom tasks in deploy.php. Use this force ;)
I think, it will be cool to put this in some sort on public knowledge database to be able send requests and pr there.
This is a "Git is missing" issue with a first example.
And with missing I mean: https://github.com/deployphp/deployer/tree/master/recipe
very specific task
Nah. Very common. Like yii-app-advanced.
Done in 6.5.0
@antonmedv Was there a specific commit where we can see this new recipe?
Most helpful comment
Done in 6.5.0