Deployer: Task git:verify to assert nothing has changed

Created on 21 Apr 2016  路  8Comments  路  Source: deployphp/deployer

| Q | A |
| --- | --- |
| Issue Type | Feature Request |
| Deployer Version | master |
| Local Machine OS | N/A |
| Remote Machine OS | N/A |

Description

Verify the remote site before applying changes.

Steps to reproduce

  • Remote site has local changes
  • Deployer ignores them
  • Customer changes are gone

    Solution

<?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.

feature

Most helpful comment

Done in 6.5.0

All 8 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jolipixel picture jolipixel  路  4Comments

sweebee picture sweebee  路  3Comments

chouex picture chouex  路  4Comments

ovaiskhan11 picture ovaiskhan11  路  4Comments

dima-stefantsov picture dima-stefantsov  路  5Comments