new_blog.md (10120B)
1 # new blog 2 3 ## setting up docker 4 - create new centos8 droplet 5 - log in (as root) and update 6 ``` 7 dnf update 8 systemctl reboot 9 ``` 10 - set up docker repo 11 ``` 12 dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 13 ``` 14 - install `docker` (have to use version 18.09.1 due to version of containerd.io available 15 ``` 16 dnf install docker-ce-18.09.1 docker-ce-cli-18.09.1 containerd.io 17 systemctl enable docker 18 systemctl start docker 19 ``` 20 21 ## create and register gitlab runner 22 - install gitlab-runner on server 23 - register (shell executor) 24 - 25 - build and run gitlab-runner image - we need two, one to build and one to deploy (use tags) 26 ``` 27 docker run -d --name gitlab-runner1 --restart always -v /var/run/docker.sock:/var/run/docker.sock -v /srv/gitlab-runner/config:/etc/gitlab-runner:Z gitlab/gitlab-runner:latest 28 docker exec -ti gitlab-runner1 gitlab-runner register 29 ``` 30 _need to add --privileged to runner2_ 31 - when prompted enter 32 - coordinator URL: https://gitlab.com 33 - token: found under ... 34 - description: anything you want 35 - tags: build/deploy 36 - executor: 'docker' for both 37 - base image: 'gcr.io/kaniko-project/executor:debug' for 1, 'docker:latest' for 2 38 39 ## configure gitlab ci 40 - build and deploy using simple docker commands 41 - 42 - copy the following into a file called 'gitlab-ci.yml' in the project repo 43 ``` 44 build: 45 stage: build 46 image: 47 name: gcr.io/kaniko-project/executor:debug 48 entrypoint: [""] 49 script: 50 - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json 51 - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG 52 only: 53 - tags 54 ``` 55 - the env variables need to be added into the gitlab project ci/cd settings 56 57 ## setting up static site generator 58 - requires hugo to be installed 59 - start a new site, the `--force` option is because we already have files in the project repo 60 ``` 61 hugo new site . --force 62 git submodule add https://gitlab.com/pyratebeard/hugo-futuremyth.git themes/futuremyth 63 echo 'theme = "futuremyth"' >> config.toml 64 hugo new posts/my-first-post.md 65 ``` 66 - run server in development mode 67 ``` 68 hugo server -D 69 ``` 70 - change 'title' in 'config.toml' 71 - build static pages 72 ``` 73 hugo -D 74 ``` 75 - to deploy drafts update head to say 76 ``` 77 draft: false 78 ``` 79 80 81 #### setting up kubernetes (minikube - single node) 82 - create new centos8 droplet 83 - log in (as root) and run `dnf update` 84 - set up docker repo 85 ``` 86 dnf install device-mapper-persistent-data lvm2 87 dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 88 ``` 89 - install `docker` (have to use version 18.09.1 due to version of containerd.io available 90 ``` 91 dnf install docker-ce-18.09.1 docker-ce-cli-18.09.1 containerd.io 92 systemctl enable docker 93 systemctl start docker 94 ``` 95 - set up kubernetes repo 96 ``` 97 cat > /etc/yum.repos.d/kubernetes.repo << EOF 98 [kubernetes] 99 name=Kubernetes 100 baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 101 enabled=1 102 gpgcheck=1 103 repo_gpgcheck=1 104 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg 105 EOF 106 ``` 107 - install `kubectl` 108 ``` 109 dnf install kubectl 110 ``` 111 112 # attempt 2 113 114 - create new empty project on gitlab 115 - on local machine create new hugo site 116 ``` 117 hugo new site hugo_blog 118 cd hugo_blog 119 git init 120 git remote add origin git@gitlab.com:<username>/<project_name>.git 121 ``` 122 - add a theme to your site 123 ``` 124 git submodule add https://github.com/budparr/gohugo-theme-ananke.git themes/ananke 125 echo 'theme = "ananke"' >> config.toml 126 ``` 127 - change the [resource cache][] so gitlab ci/cd doesn't break 128 ``` 129 cat >> config.toml <<EOF 130 [caches.images] 131 dir = ":cacheDir/_gen" 132 [caches.assets] 133 dir = ":cacheDir/_gen" 134 EOF 135 ``` 136 - create your first new post 137 ``` 138 hugo new posts/initial_post.md 139 ``` 140 - to view your site locally run the development server with the `-D` flag 141 ``` 142 hugo server -D 143 ``` 144 - to publish your post change the 'draft' header to 'false' 145 - create a file called '.gitlab-ci.yml' with the following 146 ``` 147 build: 148 stage: build 149 image: docker:latest 150 services: 151 - docker:dind 152 before_script: 153 - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY 154 script: 155 - docker build --pull -t $CI_REGISTRY_IMAGE:latest . 156 - docker push $CI_REGISTRY_IMAGE:latest 157 158 deploy: 159 stage: deploy 160 image: docker:latest 161 services: 162 - docker:dind 163 tags: 164 - deploy 165 before_script: 166 - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY 167 script: 168 - docker pull $CI_REGISTRY_IMAGE 169 - docker run -d --name "blog_test_2" -p 80:1313 -v $(pwd):/site $CI_REGISTRY_IMAGE 170 ``` 171 - create a Dockerfile with the following 172 ``` 173 FROM jojomi/hugo 174 175 COPY . /site 176 WORKDIR /site 177 178 # enable hugo watch to keep container alive 179 ENV HUGO_WATCH=true 180 181 RUN hugo 182 183 # saw an issue with the resource directory permissions 184 RUN chown -R docker: /site/resources/ 185 ``` 186 - in you gitlab account settings under 'access tokens' add a new personal access token with 'api' scope enabled 187 - in your gitlab project ci/cd settings under variables add the following 188 | key | value | 189 | --- | --- | 190 | CI_REGISTRY | registry.gitlab.com | 191 | CI_REGISTRY_IMAGE | registry.gitlab.com/<username>/<project_name> | 192 | CI_REGISTRY_USER | <username> | 193 | CI_REGISTRY_PASSWORD | <personal_access_token> | 194 | CONTAINER_NAME | <anything> | 195 - mark the CI_REGISTRY_PASSWORD variable as 'Protected' 196 - create a [gitlab runner][] on your server. you *must* have docker installed and working for the runner to work 197 - use the registration token found under the runners section in the ci/cd settings 198 - apply the tag 'deploy' 199 - select the 'shell' executor 200 - make sure you have docker running on your server and add the gitlab-runner user to the docker group 201 ``` 202 sudo usermod -aG docker gitlab-runner 203 ``` 204 - you can now commit and push your repo to trigger the pipeline 205 ``` 206 git add . 207 git commit -m "initial commit" 208 git push -u origin master 209 ``` 210 211 - update submodule 212 ``` 213 git submodule update --remote 214 ``` 215 216 [gitlab runner]: https://docs.gitlab.com/runner/install/linux-repository.html 217 [resource cache]: https://gohugo.io/getting-started/configuration/#configure-file-caches 218 219 220 221 # final attempt 222 ## Getting setup 223 Generate a new [Hugo](https://gohugo.io) site, for this example I will be calling my 'pyratelog' 224 ``` 225 hugo new site pyratelog 226 ``` 227 228 Navigate into the new directory and initialise it as a git repository 229 ``` 230 cd pyratelog 231 git init 232 ``` 233 234 Create a new project in [Gitlab](https://gitlab.com) 235 236 ![new_gitlab_project](img/20200302-hugo_blog-new_project.png#fitwidth) 237 238 Add your new Gitlab project as a remote repo to your Hugo site and make an inital commit if you want 239 ``` 240 git remote add origin git@gitlab.com:pyratebeard/pyratelog.git 241 git add . 242 git commit -m "initial commit" 243 git push -u origin master 244 ``` 245 246 Your Gitlab project should now be populated with a `config.toml` file and the 'archetypes' directory. 247 248 ![inital_commit](img/20200302-hugo_blog-initial_commit.png#fitwidth) 249 250 I won't keep mentioning when to commit changes to git as we all work differently. We will come to it a bit later when we configure our CI/CD pipeline. 251 252 ## Configure Hugo 253 Let us add a theme to our Hugo project, in this case I will use my own 'futuremyth' theme 254 ``` 255 git submodule add https://gitlab.com/pyratebeard/hugo-futuremyth.git themes/futuremyth 256 echo 'theme = "futuremyth"' >> config.toml 257 ``` 258 259 I have added in the 'pagination' variable to change the default of 10 items to 5, and also set a static directory for use with images in my log entries 260 ``` 261 cat >> config.toml << EOF 262 pagination = "5" 263 staticDir = ["static"] 264 EOF 265 ``` 266 267 I found it is a good idea to change some of the cache directories. There was an issue I had in my Gitlab CI/CD pipeline with root permissions being set on a directory, causing the pipeline to fail 268 ``` 269 cat >> config.toml << EOF 270 [caches.images] 271 dir = ":cacheDir/_gen" 272 [caches.assets] 273 dir = ":cacheDir/_gen" 274 EOF 275 ``` 276 277 You should also edit the 'baseURL' and 'title' variables in your `config.toml` 278 279 You can start Hugo on your local machine in development mode using 280 ``` 281 hugo server -D 282 ``` 283 284 If you navigate to http://localhost:1313 you should see a fairly empty page. To add new content you run 285 ``` 286 hugo new posts/hello_world.md 287 ``` 288 You change the path to whatever you want, and it will be created under the 'content' directory. 289 290 If you left your deployment server running you should see that in your browser the site should automatically updates. You first entry should show the title of your post and the date. You can open the markdown file in your favourite editor and start writing below the second set of hyphens (`---`). Everything between the hyphens is metadata for the page. You can add more if you like, I add a 'summary', 'categories', and 'tags' in the following way 291 ``` 292 summary: How I set up a Hugo website and deployed with Gitlab's CI/CD pipeline 293 categories: [tech] 294 tags: [website, hugo, devops, gitlab, automation] 295 ``` 296 297 We can now build our site by running 298 ``` 299 hugo 300 ``` 301 302 This won't include our first post because we have left the `draft` variable as `true`. When you are ready to publish change it to `false` and build the site again. You can build with drafts included by running 303 ``` 304 hugo -D 305 ``` 306 307 ## AutoDevOps 308 There a many ways you can host a website, and many ways you can use Gitlab's CI/CD pipeline to automate the process. The method I have opted for is to run my Hugo site in a docker container on a DigitalOcean droplet. I have chosen *not* to use `docker-compose` to include the Nginx reverse proxy as I host other things behind Nginx and don't want it to be controlled by my Hugo container