{"id":458,"date":"2024-10-22T19:27:12","date_gmt":"2024-10-22T19:27:12","guid":{"rendered":"https:\/\/ecodebook.wordpress.com\/?p=458"},"modified":"2024-10-22T19:27:12","modified_gmt":"2024-10-22T19:27:12","slug":"setup-code-deployment-using-github","status":"publish","type":"post","link":"https:\/\/blog.mycarts.shop\/?p=458","title":{"rendered":"Setup code deployment using Github."},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># ssh-keygen -t ed25519 -C \"youremail@email.com\"\n# cat \/home\/username\/.ssh\/id_ed25519.pub\n# git init\n# git branch -m master\nor\n# git config --global init.defaultBranch master\n# git remote add \"origin\" git@github.com:usename\/yourgit.git\n# git pull --set-upstream origin master\n# git add public_html\/hook.php\n# chmod 600 public_html\/hook.php\n# git pull\nor\n# git pull \"origin\" master\n\n<\/code><\/pre>\n\n\n\n<p><strong>webhook.php<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$secret = 'yoursecretcode';\nlist($hashAlgo, $signature) = explode('=', $_SERVER&#091;'HTTP_X_HUB_SIGNATURE']);\n$payload = $HTTP_RAW_POST_DATA ?: file_get_contents('php:\/\/input');\n\n$compute = hash_hmac($hashAlgo, $payload, $secret);\n$valid = hash_equals($compute, $signature);\n\nif ($valid === false) {\n\texit('Incorrect data');\n}else{\n    \/\/ array of commands\n    $commands = array(\n        'echo $PWD',\n        'whoami',\n        \/\/'git pull',\n      \t'git --work-tree=$PWD pull',\n        'git status',\n        'git submodule sync',\n        'git submodule update',\n        'git submodule status',\n    );\n\n    \/\/ exec commands\n    $output = '';\n    foreach($commands AS $command){\n        $tmp = shell_exec($command);\n        \n        $output .= \"&lt;span style=\\\"color: #6BE234;\\\"&gt;\\$&lt;\/span&gt;&lt;span style=\\\"color: #729FCF;\\\"&gt;{$command}\\n&lt;\/span&gt;&lt;br \/&gt;\";\n        $output .= htmlentities(trim($tmp)) . \"\\n&lt;br \/&gt;&lt;br \/&gt;\";\n    }\n}\n?&gt;\n\n&lt;!DOCTYPE HTML&gt;\n&lt;html lang=\"en-US\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;title&gt;GIT DEPLOYMENT SCRIPT&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body style=\"background-color: #000000; color: #FFFFFF; font-weight: bold; padding: 0 10px;\"&gt;\n&lt;div style=\"width:700px\"&gt;\n    &lt;div style=\"float:left;width:350px;\"&gt;\n    &lt;p style=\"color:white;\"&gt;Git Deployment Script&lt;\/p&gt;\n    &lt;?php echo $output; ?&gt;\n    &lt;\/div&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p><strong>webhook.php<\/strong> <strong>with sha256 validation<\/strong> <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ndefine(\"SECRET\", \"yoursecretcode\");\n\n$body = file_get_contents(\"php:\/\/input\");\n\/\/ $decodedBody = json_decode(urldecode($body));\n\nfunction verifySignature($body){\n  $headers = getallheaders();\n  return hash_equals('sha256='.hash_hmac('sha256', $body, SECRET), isset($headers&#091;'X-Hub-Signature-256']) ? $headers&#091;'X-Hub-Signature-256'] : ''); \n}\n\nif (verifySignature($body) !== false) {\n\n    \/\/ array of commands\n    $commands = array(\n        'echo $PWD',\n        'whoami',\n        \/\/'git pull',\n        \/\/'git --work-tree=$PWD pull',\n        'git --work-tree=\/home\/username\/public_html pull',\n        'git status',\n        \/\/'git submodule sync',\n        \/\/'git submodule update',\n        'git submodule status',\n    );\n\n    \/\/ exec commands\n    $output = '';\n    foreach($commands AS $command){\n        $tmp = shell_exec($command);\n        \n        $output .= \"&lt;span style=\\\"color: #6BE234;\\\"&gt;\\$&lt;\/span&gt;&lt;span style=\\\"color: #729FCF;\\\"&gt;{$command}\\n&lt;\/span&gt;&lt;br \/&gt;\";\n        $output .= htmlentities(trim($tmp)) . \"\\n&lt;br \/&gt;&lt;br \/&gt;\";\n    }\n} else {\n  \/\/ unverified\n  http_response_code(404);\n  echo '&lt;!DOCTYPE HTML PUBLIC \"-\/\/IETF\/\/DTD HTML 2.0\/\/EN\"&gt;\n&lt;html&gt;&lt;head&gt;\n&lt;title&gt;404 Not Found&lt;\/title&gt;\n&lt;\/head&gt;&lt;body&gt;\n&lt;h1&gt;Not Found&lt;\/h1&gt;\n&lt;p&gt;The requested URL was not found on this server.&lt;\/p&gt;\n&lt;\/body&gt;&lt;\/html&gt;';\n}\n\n?&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>webhook.php webhook.php with sha256 validation<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[15,19],"class_list":["post-458","post","type-post","status-publish","format-standard","hentry","category-ssh","tag-git","tag-ssh"],"_links":{"self":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts\/458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=458"}],"version-history":[{"count":0,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts\/458\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}