{
    "componentChunkName": "component---src-templates-post-index-js",
    "path": "/blog/setting-up-local-dev-and-deployment-pipelines",
    "result": {"data":{"post":{"featured":{"src":"feature/2e2caf5740aabb6fe6334d2aef4cab7acd28b408_rawpixel-com-579231-unsplash.jpg"},"title":"Setting up Local Dev and Deployment Pipelines","description":"Since I discovered git, everything I do gets recorded on GitHub (it’s one of the most amazing things I’ve learnt in web). I’ve also started using XAMPP so that I can develop on my local machine. But while copying files from my local development onto my server I thought surely there must be an easier way to do this.","slug":"setting-up-local-dev-and-deployment-pipelines","content":"<p>Since I discovered git, everything I do gets recorded on GitHub (it’s one of the most amazing things I’ve learnt in web). I’ve also started using XAMPP so that I can develop on my local machine. But while copying files from my local development onto my server I thought surely there must be an easier way to do this. Unfortunately there didn’t seem to be a lot of information about setting this up with FTP so I’m documenting it to make it easier for the next person.</p>\n<p>These instructions are specific to a WordPress build and using cPanel but can be adjusted to different situations.</p>\n<p>To set this up you’ll need a few things:</p>\n<ul>\n<li>Install <a href=\"https://www.apachefriends.org/index.html\" target=\"_blank\" rel=\"nofollow\">XAMPP</a> (or <a href=\"https://www.mamp.info/en/\" target=\"_blank\" rel=\"nofollow\">MAMP</a> on a Mac) for local development</li>\n<li><a href=\"https://github.com/\" target=\"_blank\" rel=\"nofollow\">Github</a> Account (can also use GitLab or Bitbucket)</li>\n<li>Latest <a href=\"https://en-au.wordpress.org/\" target=\"_blank\" rel=\"nofollow\">WordPress</a> version</li>\n<li><a href=\"https://git-for-windows.github.io/\" target=\"_blank\" rel=\"nofollow\">Git client</a> (for Windows computers, Mac and Linux can use Terminal). You could also use <a href=\"https://msdn.microsoft.com/en-au/commandline/wsl/install_guide\" target=\"_blank\" rel=\"nofollow\">Ubuntu Bash</a> if you have the lastest Windows 10</li>\n<li>Access to your hosting through cPanel (or similar)</li>\n</ul>\n<section><h2 id=\"install-wordpress\"><a href=\"#install-wordpress\" aria-hidden=\"true\" tabindex=\"-1\"><span class=\"icon icon-link\"></span></a>Install WordPress</h2><p>To start, you will need to have WordPress installed on both your <a href=\"https://premium.wpmudev.org/blog/setting-up-xampp/\" target=\"_blank\" rel=\"nofollow\">local machine</a> and on your <a href=\"https://codex.wordpress.org/Installing_WordPress\" target=\"_blank\" rel=\"nofollow\">hosting</a>. For my local installation, I include it in a subdirectory (eg. <a href=\"http://localhost/wordpress\" target=\"_blank\" rel=\"nofollow\">http://localhost/wordpress</a>) rather than the root path as that way I can have a bunch of different installations at once.</p></section>\n<section><h2 id=\"setup-git-repository\"><a href=\"#setup-git-repository\" aria-hidden=\"true\" tabindex=\"-1\"><span class=\"icon icon-link\"></span></a>Setup Git Repository</h2><p>Technically you could use the whole WordPress folder as your repository but I only include my theme folder and have no issues with that. You can get a copy of the Twenty Seventeen WordPress theme from my <a href=\"https://github.com/amykapernick/wordpress\" target=\"_blank\" rel=\"nofollow\">repository</a>. Clone this repository into the Themes folder of your local installation.</p></section>\n<section><h2 id=\"install-gulp-and-sass\"><a href=\"#install-gulp-and-sass\" aria-hidden=\"true\" tabindex=\"-1\"><span class=\"icon icon-link\"></span></a>Install Gulp and Sass</h2><p>I use Gulp to not only compile my Sass (definitely one of the most amazing things I’ve learnt) but also to create source maps and change the file paths when changing from local to online. If you’re starting from scratch, you’ll have to install the following:</p><ul>\n<li><a href=\"https://nodejs.org/en/\" target=\"_blank\" rel=\"nofollow\">Node.js</a></li>\n<li><a href=\"https://www.npmjs.com/package/gulp-install\" target=\"_blank\" rel=\"nofollow\">Gulp</a> (via Git Bash, Terminal or similar)</li>\n<li><a href=\"https://www.npmjs.com/package/gulp-sass\" target=\"_blank\" rel=\"nofollow\">Gulp Sass</a></li>\n<li><a href=\"https://www.npmjs.com/package/gulp-replace\" target=\"_blank\" rel=\"nofollow\">Gulp Replace</a></li>\n<li><a href=\"https://www.npmjs.com/package/gulp-string-replace\" target=\"_blank\" rel=\"nofollow\">Gulp String Replace</a></li>\n<li><a href=\"https://www.npmjs.com/package/gulp-sourcemaps\" target=\"_blank\" rel=\"nofollow\">Gulp Sourcemaps</a></li>\n</ul><p>These are all modules needed in my Gulpfile but feel free to use different ones or add your own, remember to adjust as necessary.</p><pre><code class=\"hljs language-javascript\"><span class=\"hljs-comment\">//Variables</span>\n<span class=\"hljs-keyword\">var</span> gulp = <span class=\"hljs-built_in\">require</span>(<span class=\"hljs-string\">\"gulp\"</span>);\n<span class=\"hljs-keyword\">var</span> sass = <span class=\"hljs-built_in\">require</span>(<span class=\"hljs-string\">\"gulp-sass\"</span>);\n<span class=\"hljs-keyword\">var</span> replace = <span class=\"hljs-built_in\">require</span>(<span class=\"hljs-string\">\"gulp-replace\"</span>);\n<span class=\"hljs-keyword\">var</span> replaceString = <span class=\"hljs-built_in\">require</span>(<span class=\"hljs-string\">\"gulp-string-replace\"</span>);\n<span class=\"hljs-keyword\">var</span> sourcemaps = <span class=\"hljs-built_in\">require</span>(<span class=\"hljs-string\">\"gulp-sourcemaps\"</span>);\n\n<span class=\"hljs-comment\">//File Paths</span>\n<span class=\"hljs-keyword\">var</span> sassFiles = <span class=\"hljs-string\">\"source/scss/**/*.scss\"</span>,\n  mainSassFile = <span class=\"hljs-string\">\"source/scss/main.scss\"</span>,\n  cssFiles = <span class=\"hljs-string\">\"assets/css/\"</span>,\n  localHostPath = <span class=\"hljs-string\">\"/wordpress/wp-content/\"</span>,\n  remotePath = <span class=\"hljs-string\">\"/wp-content/\"</span>,\n  pathFiles = <span class=\"hljs-string\">\"*\"</span>;\n\n<span class=\"hljs-comment\">//Compile main sass into css</span>\ngulp.task(<span class=\"hljs-string\">\"sassy\"</span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function</span>(<span class=\"hljs-params\"></span>) </span>{\n  gulp\n    .src(mainSassFile)\n    .pipe(sourcemaps.init())\n    .pipe(sass().on(<span class=\"hljs-string\">\"error\"</span>, sass.logError)) <span class=\"hljs-comment\">//Using gulp-sass</span>\n    .pipe(sourcemaps.write(<span class=\"hljs-string\">\"../maps\"</span>))\n    .pipe(gulp.dest(cssFiles));\n});\n\n<span class=\"hljs-comment\">//Watch for changes in sass files and running sass compile</span>\ngulp.task(<span class=\"hljs-string\">\"watch\"</span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function</span>(<span class=\"hljs-params\"></span>) </span>{\n  gulp.watch(sassFiles, [<span class=\"hljs-string\">\"sassy\"</span>]);\n});\n\n<span class=\"hljs-comment\">//Replace file paths for local host with remote server</span>\ngulp.task(<span class=\"hljs-string\">\"replaceLocalDev\"</span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function</span>(<span class=\"hljs-params\"></span>) </span>{\n  gulp\n    .src([pathFiles, <span class=\"hljs-string\">\"!gulpfile.js\"</span>])\n    .pipe(replace(localHostPath, remotePath))\n    .pipe(gulp.dest(<span class=\"hljs-string\">\"./\"</span>));\n\n  gulp\n    .src([sassFiles, <span class=\"hljs-string\">\"!gulpfile.js\"</span>])\n    .pipe(replaceString(localHostPath, remotePath))\n    .pipe(gulp.dest(<span class=\"hljs-string\">\"source/scss/\"</span>));\n\n  gulp.start(<span class=\"hljs-string\">\"sassy\"</span>);\n});</code></pre><p>Make sure that you add the node_modules folder to your gitignore file otherwise it will take forever.</p></section>\n<section><h2 id=\"setup-codeship\"><a href=\"#setup-codeship\" aria-hidden=\"true\" tabindex=\"-1\"><span class=\"icon icon-link\"></span></a>Setup Codeship</h2><p>Codeship allows users with free accounts to have unlimited projects setup, run 1 build at a time and have 100 builds each month. For most freelancers or small businesses this should be fine otherwise plans start at $49/month and include unlimited builds.</p><p>Once you’ve created your account, setup a project linked to your GitHub account and follow the instructions to link it to your desired repository.</p><p>When prompted, select to run node.js commands and add to those to install any gulp modules you’re using. It should end up looking similar to this:</p><pre><code class=\"hljs language-shell\"><span class=\"hljs-meta\">#</span><span class=\"bash\"> By default we use the Node.js version <span class=\"hljs-built_in\">set</span> <span class=\"hljs-keyword\">in</span> your package.json or the latest</span>\n<span class=\"hljs-meta\">#</span><span class=\"bash\"> version from the 0.10 release</span>\n<span class=\"hljs-meta\">#</span><span class=\"bash\">\n<span class=\"hljs-comment\"># You can use nvm to install any Node.js (or io.js) version you require.</span></span>\n<span class=\"hljs-meta\">#</span><span class=\"bash\"> nvm install 4.0</span>\nnvm install 0.10\nnpm install\nnpm install --save gulp-install\nnpm install gulp-sass --save-dev\nnpm install --save-dev gulp-replace\nnpm install gulp-string-replace --save-dev</code></pre><p>Navigate to <strong>Project Settings</strong> -> <strong>Deployment</strong> and select to setup a new deployment pipeline. You can choose whether to trigger the build to happen when you push to the master branch or another one, for example I’ve set it to build when I push the the dev branch (which means I can specifically target when I want to build).</p><p>For this setup, you will need to select <strong>Custom Script</strong> but if your deployment is different, you can choose another option. You can also choose to run the build in multiple locations if you like. Again, add anything you’d like to run to the scripts but I use the following:</p><pre><code class=\"hljs language-shell\">gulp replaceLocalDev\ngulp sassy\nrm -rf node_modules\nlftp -c \"open -u $FTP_USER,$FTP_PASSWORD {insertFTPhosthere}; set ssl:verify-certificate no; mirror -R ~/clone/ /wp-content/themes/wordpresstheme\"</code></pre><p>This script runs the gulp functions to change file paths, removes the node_modules folder that was created (this isn’t needed for the website and both takes up a lot of room and has a tendency to break when moved to your hosting) and then moves the cloned files via FTP (you can also set this up to use SFTP or SSH). Make sure you update your version to include your FTP address and make sure that the target folder path is correct.</p><p>Navigate to <strong>Project Settings</strong> -> <strong>Environment</strong> and assign the variables for FTP_USER and FTP_PASSWORD for your login details. This way any logs for your build will include the variable name rather than your actual username and password.</p><p>If needed, edit the <strong>Projects Settings</strong> -> <strong>Notification</strong> to specify how you’d like to be notified of build successes or failures, for example you can get notified via <a href=\"https://slack.com/\" target=\"_blank\" rel=\"nofollow\">Slack</a>.</p></section>\n<section><h2 id=\"databases\"><a href=\"#databases\" aria-hidden=\"true\" tabindex=\"-1\"><span class=\"icon icon-link\"></span></a>Databases</h2><p>Unfortunately the process for migrating database information is still a manual process (although I’ll definitely let you know once I work out how to automate that as well). Open the database for your local installation and perform a full export, then import that into the database for your hosting (you may need to delete existing tables before you import). Once you’ve imported the database, you will need to adjust the wp_options table to update the siteurl and home values for the live URL rather than the localhost one.</p><p>This doesn’t have to be done every time, but includes any updates in content (such as posts and pages), plugins and other config. The build process only brings through theme files which includes any templates, styles and scripts being used.</p></section>\n<section><h2 id=\"plugins\"><a href=\"#plugins\" aria-hidden=\"true\" tabindex=\"-1\"><span class=\"icon icon-link\"></span></a>Plugins</h2><p>Again, unfortunately this method doesn’t result in the plugins being brought across. My method to approach this, is to install the plugins on both local and live and when the database gets imported, it will bring through any config that you’ve set.</p></section>","date":"04 Sep 2017"}},"pageContext":{"id":"5c353b03-5684-54d9-ab9f-f2b2807fe211"}},
    "staticQueryHashes": ["1737603086","259994666","2851563141"]}