github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/docs/content/en/hosting-and-deployment/deployment-with-wercker.md (about) 1 --- 2 title: Deployment with Wercker 3 linktitle: Deployment with Wercker 4 description: You can use a free tool called Wercker to automate deployments between your GitHub-hosted source and final website on GitHub pages. 5 date: 2017-02-01 6 publishdate: 2017-02-01 7 lastmod: 2017-02-01 8 categories: [hosting and deployment] 9 keywords: [wercker,deployment,github,git] 10 authors: [Arjen Schwarz, Samuel Debruyn] 11 menu: 12 docs: 13 parent: "hosting-and-deployment" 14 weight: 60 15 weight: 60 16 sections_weight: 60 17 draft: false 18 aliases: [/tutorials/automated-deployments/] 19 toc: true 20 wip: false 21 notesforauthors: 22 --- 23 24 ## Goals 25 26 By the end of this guide, you will have completed the following: 27 28 * Creating a basic Hugo project and website 29 * Version controlling your project with Git 30 * Adding your project to GitHub 31 * Automating site deployments with a free tool called Wercker 32 * Deploying your website to GitHub Pages for free hosting 33 34 ## Assumptions 35 36 1. You have a working familiarity with using Git for version control 37 2. You have a GitHub account 38 3. You have already created a basic Hugo project 39 40 If you do not meet these assumptions, the [GitHub help section][githubhelp] has an explanation of how to install and use git. [Signing up for a GitHub][ghsignup] account is free as well. If you are completely unfamiliar with creating a new Hugo website, visit the [Hugo Quick Start][quickstart]. 41 42 ## Create a Basic Hugo Site 43 44 {{% note "This Guide Uses the Hugo CLI" %}} 45 All the work for setting up a Hugo project and using this guide is done via the Hugo CLI's most basic commands. See the [command line reference page](/commands/) for a more exhaustive account of the CLI's features. 46 {{% /note %}} 47 48 First, create your new Hugo website using the [`hugo new site` command][basicusage] and change into the newly created directory for the project. In this guide, we are calling our new project `hugo-wercker-example`: 49 50 {{< code file="hugo-new-site.sh" >}} 51 hugo new site hugo-wercker-example 52 cd hugo-wercker-example 53 {{< /code >}} 54 55 We will use the [Herring Cove theme][] by first cloning the theme into the `themes` directory. 56 57 {{< code file="clone-herring-cove-theme.sh" >}} 58 cd themes 59 git clone https://github.com/spf13/herring-cove.git 60 {{< /code >}} 61 62 Cloning the project from the command line will conflict with our own version control. So, we need to remove the external git configuration that came with the clone of Herring Cove: 63 64 {{< code file="remove-herring-cove-git.sh" >}} 65 rm -rf herring-cove/.git 66 {{< /code >}} 67 68 We need content for Hugo to build. Let's add a quick `/about` page: 69 70 ``` 71 hugo new about.md 72 ``` 73 74 {{% note %}} 75 The preceding example for the about page leverages archetypes to scaffold a new content file with preconfigured front matter. [Find out more about Hugo's archetypes](/content-management/archetypes/). 76 {{% /note %}} 77 78 Now you can edit `contents/about.md` in your text editor of choice, but this is not necessary for the purposes of this guide. Running the following command will build your Hugo site into the `public` directory. 79 80 Once the website is build, it's a good idea to run the following command to start a local server and ensure you're changes have been implemented: 81 82 ``` 83 hugo server --theme=herring-cove 84 ``` 85 86 If everything is fine, you should see something similar to the image below when you go to <http://localhost:1313> in your browser. 87 88 ![][1] 89 90 ## Set up Version Control in Git 91 92 Adding Git to your project is done by running the `git init` command from the root directory of your project. 93 94 ``` 95 git init 96 ``` 97 98 Running `git status` at this point will show you the following entries: the `config.toml` file, the `themes` directory, the `contents` directory, and the `public` directory. However, we don't want the `public` directory version controlled because Wercker is responsible for generating the finished website later on. Therefore, we'll add a `.gitignore` file to our project that will exclude the `/public` directory from being tracked by Git: 99 100 {{< code file="gitignore.sh" >}} 101 echo "/public" >> .gitignore 102 {{< /code >}} 103 104 Wercker might complain when we try to build the site later on because we currently do not have any static files outside of the `themes` directory. We simply have to add *any* file to the static folder to prevent Wercker from complaining. To keep this guide simple, let's add a `robots.txt`. The following command creates the file in `/static`. The contents of the `robots.txt` lets search engines know they have full access to crawl the published website: 105 106 {{< code file="addrobotstxt.sh" >}} 107 echo "User-agent: *\nDisallow:" > static/robots.txt 108 {{< /code >}} 109 110 Now we need to add (i.e., [stage [see Git documentation]][gitbasics]) and commit all of our changes in the repository into Git: 111 112 ``` 113 git commit -a -m "Initial commit" 114 ``` 115 116 ## Add the Project to GitHub 117 118 Now we need to create a new repository on GitHub. Once you are signed in to GitHub, you can add a new repository by clicking on the **+▼** dropdown at the top right or by going to [https://github.com/new](https://github.com).. 119 120 We then choose a name for the project (`hugo-wercker-example`). When clicking on create repository GitHub displays the commands for adding an existing project to the site. The commands shown below are the ones used for this site, if you're following along you will need to use the ones shown by GitHub. Once we've run those commands the project is in GitHub and we can move on to setting up the Wercker configuration. Be sure to replace `YourUserName` with your GitHub account/username: 121 122 {{< code file="setup-gh-repo.sh" >}} 123 git remote add origin git@github.com:YourUsername/hugo-wercker-example.git 124 git push -u origin master 125 {{< /code >}} 126 127 ![][2] 128 129 ## Set Up Wercker 130 131 To sign up for a free Wercker account, go to <https://wercker.com> and click the **Sign Up** button on the top right of the home screen. 132 133 ![][3] 134 135 ### Register for Wercker with Your GitHub Account 136 137 Sign up for Wercker using your GitHub credentials. If you don't have a GitHub account, or don't want to use it for your account, you have the option to register with a username and password as well. However, the second half of this guide---devoted to hosting your website on GitHub pages---will no longer be of interest to you. 138 139 ![][4] 140 141 ### Connect GitHub or Bitbucket 142 143 After you are registered, you will need to link your GitHub or Bitbucket account to Wercker. You can link your account by navigating to your profile settings and then selecting "Git connections." 144 145 ![][17] 146 147 If you registered for Wercker using GitHub, it will most likely look like the following image. To connect a missing service, click the **Connect** button, which may send you to either GitHub or Bitbucket to sign into your respective account. 148 149 ![][5] 150 151 ### Add Your Project 152 153 Now that we've got all the preliminaries out of the way, it's time to set up our application. For this we click on the **+ Create** button next to Applications and choose GitHub as our provider. 154 155 ![][6] 156 157 ### Select a Repository 158 159 When selecting GitHub, Wercker will show all your GitHub repositories. You have the option to filter repositories using the search input at the top of the repositories list. Once you have your repository selected, click the **Use selected repo** button. 160 161 ![][7] 162 163 ### Select the Repository Owner 164 165 In the next step, Wercker asks you to select the repository owner. Select your GitHub account and continue. 166 167 ![][8] 168 169 ### Configure Access 170 171 {{% note %}} 172 This guide assumes you are using a public GitHub repository and understand that the [published GitHub Pages website will be available to everyone](https://help.github.com/articles/what-is-github-pages/#usage-limits). 173 {{%/note %}} 174 175 This step can be slightly tricky. Wercker does not have privileges to check out your private projects by default and therefore needs your permission to add a deploy key to your repository. By selecting the first option, you're simply allowing Wercker to check out the code via the same methods available to anyone visiting the project on GitHub. 176 177 ![][9] 178 179 ### Wercker.yml 180 181 Wercker will now attempt to create an initial `wercker.yml` file for you. More specifically, it will create a code block within the Wercker interface that you can copy to your finished file. Wercker gives us a `debian` box because our project does not have any special requirements. 182 183 Now we need to create a *wercker.yml* file in the root of our project. This file will contain our Wercker app's configuration. After we finish setting up our app, we will expand the contents of this file to build and deploy our website. 184 185 ![][10] 186 187 ### Public or Private 188 189 This is a personal choice. You can make an app public so that everyone can see more details about it. Keeping it private or public does not provide any overt benefits for you as the creator of the app. That said, [the app we are currently creating has been made public][publicappurl] to facilitate easier usage of this hosting and deployment guide. 190 191 ![][11] 192 193 #### App Successfully Created 194 195 The application is now added and Wercker will offer you the chance to trigger a build. However, we will decline the offer because we haven't yet pushed our `wercker.yml` file to our GitHub repository. 196 197 ![][12] 198 199 ### Add the Hugo-build Step 200 201 Now we need to add the Wercker steps to our build process. First, we go to the "Registry" action in the top menu. When in the registry, we can search for "hugo build". Select the "Hugo-Build by **arjen**" step. 202 203 ![][13] 204 205 ### Use the Hugo-build Step 206 207 A summary of very basic usage is available at the top of the details for the Hugo-Build step. Below the basic usage is the contents of the `README.md` file associated with the step's repository. `README.md`'s on Wercker usually contain more details about the advanced options and examples of usage. 208 209 We're not going to use any of the advanced features of Hugo-Build in this guide. Let's return to our project and add the first section of details we need to our `wercker.yml`. 210 211 {{% warning "Hugo Version in `wercker.yml`" %}} 212 The docs are a work in progress. As such, the `version` represented in this guide may not represent the version you've been using for local development. Be sure to use the appropriate Hugo version for your build step. 213 {{% /warning %}} 214 215 {{< code file="wercker-build-step.yml" >}} 216 box: debian 217 build: 218 steps: 219 - arjen/hugo-build: 220 version: "0.17" 221 theme: herring-cove 222 flags: --buildDrafts=true 223 {{< /code >}} 224 225 We can conclude this first step by pushing our `wercker.yml` to our GitHub repository and then seeing the magic at work within Wercker's interface. 226 227 {{< code file="push-wecker-to-gh.sh" >}} 228 git commit -a -m "Add wercker.yml" 229 git push origin master 230 {{< /code >}} 231 232 If completed and successful, a green check mark should appear in the commit column of your first build. However, this is only the build step. We still need to deploy the website to our free hosting on GitHub Pages. If you would like more details about the build, you can click the commit hash. 233 234 ![][14] 235 236 ### Add a GitHub Pages Deploy Step to `wercker.yml` 237 238 In order to deploy to GitHub Pages, we need to add a deploy step to our `wercker.yml`. We are going to add `lukevevier/gh-pages`, the most popular GitHub Pages step in the Wercker Steps repository. Additionally, we need to ensure the box Wercker uses for our deployments has git and ssh installed. We can do this using the `install-packages` command. Here is our *final* `wercker.yml` file: 239 240 {{< code file="wercker.yml" >}} 241 box: debian 242 build: 243 steps: 244 - arjen/hugo-build: 245 version: "0.17" 246 theme: herring-cove 247 flags: --buildDrafts=true 248 deploy: 249 steps: 250 - install-packages: 251 packages: git ssh-client 252 - lukevivier/gh-pages@0.2.1: 253 token: $GIT_TOKEN 254 domain: hugo-wercker.ig.nore.me 255 basedir: public 256 {{< /code >}} 257 258 ### How does the GitHub Pages Configuration Work? 259 260 We've provided a some important information in our `wercker.yml`. First, we've added the domain we want to use for our published website. Configuring the domain here will ensure that GitHub Pages is aware of the domain we want to use. 261 262 Secondly, we've configured the `basedir` to `public`. This is the directory that will be used as the website on GitHub Pages. `public` is also the default publishing directory in Hugo. (For more information, see [hugo's configuration docs][hugoconfig]). 263 264 Lastly, you'll notice a `$GIT_TOKEN` variable. This is used for pushing our changes to GitHub. We will need to configure this token before Wercker can build our website. 265 266 ### Set the App's Deploy Target 267 268 We can set our deploy target by going to our app's settings and clicking on **Deploy targets**. Now select **Add deploy target** and then **Custom deploy**. 269 270 ![][15] 271 272 ### Configure the Deploy Step in Wercker 273 274 The next screen requires you fill in the deploy target name. 275 276 1. Make sure you enable **auto deploy** from the **master** branch. 277 2. Add a variable for the **GIT_TOKEN**. You'll need to create an access token in GitHub. Follow the directions in [GitHub help][accesstokenghhelp]. 278 3. With the deploy step configured in Wercker, we can push the updated wercker.yml file to GitHub and it will create the GitHub pages site for us. 279 280 The website described in this guide is available at <http://hugo-wercker.ig.nore.me>. 281 282 ![][16] 283 284 ## Conclusion 285 286 Once this workflow is established, you can update your website automatically by pushing any content changes to your GitHub repository. 287 288 ### Code for the Wercker Deployment Guide 289 290 [The source code for the site used in this guide is available on GitHub][guidesource], as is the [Wercker Hugo Build step][guidestep]. 291 292 If you want to see an example of how you can deploy to S3 instead of GitHub pages, check [Wercker's documentation][werckerdocs] for guidance on setup. 293 294 [1]: /images/hosting-and-deployment/deployment-with-wercker/creating-a-basic-hugo-site.png 295 [2]: /images/hosting-and-deployment/deployment-with-wercker/adding-the-project-to-github.png 296 [3]: /images/hosting-and-deployment/deployment-with-wercker/wercker-sign-up.png 297 [4]: /images/hosting-and-deployment/deployment-with-wercker/wercker-sign-up-page.png 298 [5]: /images/hosting-and-deployment/deployment-with-wercker/wercker-git-connections.png 299 [6]: /images/hosting-and-deployment/deployment-with-wercker/wercker-add-app.png 300 [7]: /images/hosting-and-deployment/deployment-with-wercker/wercker-select-repository.png 301 [8]: /images/hosting-and-deployment/deployment-with-wercker/wercker-select-owner.png 302 [9]: /images/hosting-and-deployment/deployment-with-wercker/wercker-access.png 303 [10]: /images/hosting-and-deployment/deployment-with-wercker/werckeryml.png 304 [11]: /images/hosting-and-deployment/deployment-with-wercker/public-or-not.png 305 [12]: /images/hosting-and-deployment/deployment-with-wercker/and-we-ve-got-an-app.png 306 [13]: /images/hosting-and-deployment/deployment-with-wercker/wercker-search.png 307 [14]: /images/hosting-and-deployment/deployment-with-wercker/using-hugo-build.png 308 [15]: /images/hosting-and-deployment/deployment-with-wercker/adding-a-github-pages-step.png 309 [16]: /images/hosting-and-deployment/deployment-with-wercker/configure-the-deploy-step.png 310 [17]: /images/hosting-and-deployment/deployment-with-wercker/wercker-account-settings.png 311 312 313 [accesstokenghhelp]: https://help.github.com/articles/creating-an-access-token-for-command-line-use/ 314 [basicusage]: /getting-started/usage/ 315 [ghsignup]: https://github.com/join 316 [gitbasics]: https://git-scm.com/book/en/v2/Getting-Started-Git-Basics 317 [githubhelp]: https://help.github.com/articles/set-up-git/ 318 [guidesource]: https://github.com/ArjenSchwarz/hugo-wercker-example 319 [guidestep]: https://github.com/ArjenSchwarz/wercker-step-hugo-build 320 [Herring Cove theme]: https://github.com/spf13/herring-cove 321 [hugoconfig]: /getting-started/configuration/ 322 [publicappurl]: https://app.wercker.com/#applications/5586dcbdaf7de9c51b02b0d5 323 [quickstart]: /getting-started/quick-start/ 324 [werckerdocs]: http://devcenter.wercker.com/docs/deploy/s3.html