Background

A few weeks ago I decided to replace my ageing and bloated Drupal 7 blog. I decided on the following criteria that the solution had to meet:

  1. The project git repo must be private.
  2. Hosting infrastructure had to be under my control and completely codified.
  3. The solution should require very little supporting infrastructure such as databases.
  4. Deployment of changes to the site or infrastructure must be automated.

These requirements immediately ruled out a few options including GitHub Pages and SaaS blogging platforms like wordpress.org.

In the end I decided on the following architecture:

Repository Structure

To ensure simplicity in workflow, I wanted everything to be codified within the main project repo:

  • The Hugo site source code.
  • The Terraform templates.
  • The BitBucket Pipelines build configuration.

Below is the structure I ended up with.

ni     c     ksbhtaiuentgrtborauccpstamvmcoouthfaaaknnbaeoirreftltmrniitieiiem.aa-gnccstb.p.tflniyeepmstel.ltifnes.ymlPHTiuepgreorlaisfnioetremcostnoefumirpgclueartafetisiloens

Terraform Configuration

There are a few AWS resources that are required to host the site on S3.

aws_s3_bucket to host the website files.

rE}eOsbafp{}Fw}oucooeuclrl""}bierkciVS"]snrce=ecetS""""]idret_yraiEPARteo"dstdfrce"exr"=pe=ie"fitsa__ausom:enior{ddw"btneccounooswlr<"n"tinr:cc_wioE:tP"p"cauuswcyO""u:a:ewmm3.-F2:bl["see_nr=0[l"""::nnbie1{iA:s[sttucat2cl33ckdr-Rl"::==ks"u1eo*G:eae0aw"e:""tn-d",twi4"t1G,Own0a7ebwd4"m"tj.e.wa,Oenxhwrbci.twijtchm"ae"ktl.c]sm"{nt,ale"n"t,t"amaria.net/*"

aws_route53_record CNAME record for www.nicksantamaria.net

r}esznttrooayteunmplcreeeoc_=rei==dd"s"""3a=wC0=wwN0s"wA"[_X.M"rXnE$oXi"{uXcat"kwess5a_3ns_t3ra_embcauorcrikdae".tn."ewwtww"ww."we{bsite_domain}"]

aws_s3_bucket for the apex domain redirect.

rE}eOsbafp{}Fw}oucooeuclrl""}brrkciVS"]sece=ecetS""""idet_yraiEPARti"dstdfrceer"=pe=ie"fitseausom:enio{cw"btne"ccoutsnlr<"nPtinr__iioE:tu"p"casccyO""b:a:el3k-F2:l"l["l_sr=0[iA"":_bae1{cl:s[runat2Rl3"ectdr-eo":aqka"u1aw*Grueme0d""eneta-G,,t:s"r1eOati7tbws"a"Ojs_a.,be:tpnjcsoeeet3xtc":=""t]:",:"{,nwiwcwk.snainctkasmaanrtiaam.anreita/.*n"e]t"

aws_route53_record A ALIAS record for nicksantamaria.net

r}esznta}ooaylunmpinzereeeaaovc_smnaei==eeld{_u"""=iaa=nAdtwi""es"c$=__Xk{trXsa"aoXaw$ruXns{gt"t_aeeaswt5m3s_3a__h_rbseriu3aeac_lc.kbtoneuhretcdt.k=""aeptf"e.aaxalp.pseweexex"b.sh{iotset_eddo_mzaoinne}_"id}"

If you want to adapt these for your own site:

  1. Replace ’nicksantamaria.net’ with your desired domain.
  2. Replace zone_id = ‘XXXX’ with the zone ID of your route 53 hosted zone.

Continuous Delivery Configuration

I chose BitBucket Pipelines to handle the CD pipeline due to its tight integration with BitBucket, plus it was a great opportunity to evaluate its suitability for future projects.

The first thing I needed to do was set up AWS credentials in BitBucket Pipelines. This is configured in the Repository configuration page in BitBucket.

BitBucket Pipelines environment variable configuration

Each build had to execute the following tasks:

  • Validate Terraform templates.
  • Provision Terraform resources.
  • Compile Hugo site.
  • Sync compiled artefact to S3.

Here is this workflow visualised -

CD Pipeline Diagram

Here is the bitbucket-pipelines.yml file I created to achieve this.

ipmiapbgerela#m-:inancmssgehattoseseesl:strpca:e::r###----#---#--#-###-#-#-#--###--#-###-#-nrigpSmceaDwtmDwuDpPcEtFtEttCchCeDaTc:itekdxpegavegneirdneeeneeodurceweu1s:tdptperpezpposrtrsrrmgehpssr.ui~-ethetiev$urcrurrp$oaoltl7tprrgnunpnii{raharaai{toshbtedhvgdhdnsBeffefflBe"y3t-ed-itetxoettesiIoroooeI-$hsepnPntx_ntentoTTrertrrTda{sseppAucpz0cprcanBemmmhmmsBeBiyhre~Tpysf.ysrylUroiiUsfItnlton/Hd::1::a:lTCrvtrsartCtiTecitddb=a/h7/feKaaeepeeKilBvpuei"tH/u_T/oAarEflmspm.EneUte:cnn$eugglerrWwrToisotloTaCo-/tcPgioiremSsa_rdtteyt_tcKd/iiA&ot_nrl_cfCmaaepeCioESeewoeT&.h0uae0CloLttLonT3lrwnsHu.xfa.LirOseecppOnt_.esw.:ab1_os7ImNyoauNaCti.b/p.7are.EnfnstsE$iOeonrrtc_mms8r_trfsrh_{nMniao-oLd.._eDaoieuDBiM$cnogmi6hlsIxmgseIInI{mkcte/n4aioRRTgTBash/tsu/snu}iS-s}B}Ita.bpxhhur/s3bo/Ut"Tcniif-uixctahChBhtnn16gc_eevbctuKe>Uea"s34ooasraukhgECsmt/b_rm.rlceaoTb$Kaahi0pdaiknt_u{Etrlut..6fdedCiBThilg.1c4ot=tLlI_iaot7o.rb.shOdTCs.-/a_mzme3eNBLnyrrl/ifEvUObee.itpos_eCNutulgner-tDrKEi/nezurebaIsE_lvzaxratRiTDdeis_apce}_I.rpeafrknCRssmooei_.L}ip/drcnspO/oyd6meduN_nto4/e-abEp.hw0dcll_utonh.iowiDbxnlu7nnacIlt-og.gfyRipao8.is}c|id/g-//p/t=pb_gve"uapsr0rbssu3e.ruheb:p1aceUl/7fkdRi/$/oe.Lcw{hrt/wBum=hvwIg_tte.To0ftrnB_.spsiU07t:icC..a/okK18t/nsE7_ew.aT_l.wtn_LinwxtCini.taOnucnmMuxkiaMx_scrI-aakiT6mnsa}4dta.b6anni4mtet.aat.zrm/tiiaaparr.i.na-ge.gztnr"eatn-tbsacvrkeeeranbddo=-suceroin=fhitgt=p":k/e/ya=cpsr.oadm.atzfosntaawtse."co-mb/agcrkoeunpds-/cgolnofbiagl=/"ArlelgUisoenr=sap-southeast-2"

Notes

There are a few things worth explaining in more detail.

Dependency setup

The first section of the pipeline is installing dependencies for the rest of the build. I plan on improving this by creating a custom docker image which has all these utilities pre-installed - this would reduce the build time from 2 minutes to 30 seconds.

Terraform remote state

To ensure that the terraform state is preserved between pipelines runs, the state file is stored in a S3 bucket called tfstate.nicksantamaria.net. I created this bucket manually (rather than with Terraform) to ensure there is no risk of the bucket being unintentionally destroyed during a terraform apply.

Version checking

After Hugo compiles the site, an additional file called version.txt is placed into the docroot. This file contains the git commit hash (from $BITBUCKET_COMMIT environment variable). The very last command in the pipeline makes a HTTP request to this file, and ensures the response matches the expected version string.

Conclusion

I am really happy with the end result which achieved all of goals I set out in the beginning.

BitBucket Pipelines is a brand-new service, and had some key features missing compared to competitors like TravisCI and CircleCI.

  • Environment variable definition in the build config file.
  • Separation of concerns between setup, test and deployment phases of the build.
  • Ability to have a subset of build steps shared between branches.

There are a few improvements I plan on making.

  • Add CloudFront as a CDN.
  • Use a custom docker image for the CD builds to reduce build time.