forked from markjaquith/WordPress-Skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitDeploy.class.php
180 lines (151 loc) · 5.29 KB
/
GitDeploy.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
if ( isset( $_GET['out'] ) ) {
header( "Content-Type: text/plain" );
}
class Deploy {
/**
* A callback function to call after the deploy has finished.
*
* @var callback
*/
public $post_deploy;
/**
* The name of the file that will be used for logging deployments. Set to
* FALSE to disable logging.
*
* @var string
*/
private $_log = 'deployments.log';
/**
* The timestamp format used for logging.
*
* @link http://www.php.net/manual/en/function.date.php
* @var string
*/
private $_date_format = 'Y-m-d H:i:sP';
/**
* The name of the branch to pull from.
*
* @var string
*/
private $_branch = 'master';
/**
* The name of the remote to pull from.
*
* @var string
*/
private $_remote = 'origin';
/**
* The directory where your website and git repository are located, can be
* a relative or absolute path
*
* @var string
*/
private $_directory;
/**
* Sets up defaults.
*
* @param string $directory Directory where your website is located
* @param array $data Information about the deployment
*/
public function __construct( $directory, $options = array() ) {
// Determine the directory path
$this->_directory = realpath( $directory ) . DIRECTORY_SEPARATOR;
$available_options = array(
'log',
'date_format',
'branch',
'remote'
);
foreach ( $options as $option => $value ) {
if ( in_array( $option, $available_options ) ) {
$this->{'_' . $option} = $value;
}
}
$this->log( 'Attempting deployment...' );
}
/**
* Writes a message to the log file.
*
* @param string $message The message to write
* @param string $type The type of log message (e.g. INFO, DEBUG, ERROR, etc.)
*/
public function log( $message, $type = 'INFO' ) {
if ( $this->_log ) {
// Set the name of the log file
$filename = $this->_log;
if ( !file_exists( $filename ) ) {
// Create the log file
file_put_contents( $filename, '' );
// Allow anyone to write to log files
chmod( $filename, 0666 );
}
// Write the message into the log file
// Format: time --- type: message
if ( !is_array( $message ) ) {
$messages = array( $message );
} else {
$messages = $message;
}
if ( isset( $_GET['out'] ) ) {
foreach ( $messages as $key => $message ) {
echo $message . "\n\r";
}
} else {
foreach ( $messages as $key => $message ) {
file_put_contents( $filename, date( $this->_date_format ) . ' --- ' . $type . ': ' . $message . PHP_EOL, FILE_APPEND );
}
}
}
}
/**
* Executes the necessary commands to deploy the website.
*/
public function execute( ) {
try {
// Make sure we're in the right directory
// exec( 'cd ' . $this->_directory, $output );
chdir( $this->_directory );
$this->log( "Changing working directory $this->_directory ... " );
// Discard any changes to tracked files since our last deploy
exec( 'git reset --hard HEAD ', $output );
// exec( 'git reset --hard HEAD && git clean -f -d', $output );
$this->log( 'Reseting repository... ' );
$this->log( $output, 'OUT' );
unset( $output );
// Update the local repository
exec( 'git pull ' . $this->_remote . ' ' . $this->_branch . ' 2>&1', $output );
$this->log( 'Pulling in changes... ' );
$this->log( $output, 'OUT' );
unset( $output );
// Update submodules
exec( 'git submodule update --recursive --init 2>&1', $output );
$this->log( 'Updating submodules... ' );
$this->log( $output, 'OUT' );
unset( $output );
// Update submodules
exec( 'git submodule foreach git fetch 2>&1', $output );
$this->log( 'Updating submodules... ' );
$this->log( $output, 'OUT' );
unset( $output );
exec( 'git submodule foreach git fetch --tags 2>&1', $output );
$this->log( 'Updating submodules... ' );
$this->log( $output, 'OUT' );
unset( $output );
// Update submodules
exec( 'git submodule update --recursive --init 2>&1', $output );
$this->log( 'Updating submodules... ' );
$this->log( $output, 'OUT' );
unset( $output );
// Secure the .git directory
exec( 'chmod -R og-rx .git' );
$this->log( 'Securing .git directory... ' );
if ( is_callable( $this->post_deploy ) ) {
call_user_func( $this->post_deploy, $this->_data );
}
$this->log( 'Deployment successful.' );
} catch (Exception $e) {
$this->log( $e, 'ERROR' );
}
}
}