<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>cloudflare pages on Andrew Beaton</title>
    <link>https://andrewbeaton.net/tags/cloudflare-pages/</link>
    <description>Recent content in cloudflare pages on Andrew Beaton</description>
    <image>
      <title>Andrew Beaton</title>
      <url>https://andrewbeaton.net/me.jpeg</url>
      <link>https://andrewbeaton.net/me.jpeg</link>
    </image>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Wed, 31 May 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://andrewbeaton.net/tags/cloudflare-pages/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Deploying to Cloudflare Pages with Drone</title>
      <link>https://andrewbeaton.net/posts/2023/05/drone-io-cloudflare-pages-deploy/</link>
      <pubDate>Wed, 31 May 2023 00:00:00 +0000</pubDate>
      
      <guid>https://andrewbeaton.net/posts/2023/05/drone-io-cloudflare-pages-deploy/</guid>
      <description>Deploying to Cloudflare Pages with Drone</description>
      <content:encoded><![CDATA[<h2 id="introduction">Introduction</h2>
<p>Due to some unexpected downtime with my current web host, and a bit of spare time on my hands, I thought I would give a go at configuring Drone to deploy to Cloudflare Pages.</p>
<p>A very basic description of Cloudflare Pages, is that it&rsquo;s a platform that makes it easy to create and host websites whilst Cloudflare takes care of the more technical side of website hosting. For a more technical overview, take a look at the official documentation.</p>
<p>For my use case, as I use Hugo to generate all my static website files, I can just use Cloudflare Pages to host the content. With my domain name already proxying through Cloudflare, everything is now all handled in one place, and so far, it all just works.</p>
<p>So let&rsquo;s cover off what&rsquo;s needed to get Drone to deploy your Hugo (or static files) to Cloudflare Pages.</p>
<h2 id="prerequisites">Prerequisites</h2>
<p>Before getting started, make sure you have the following prerequisites in place:</p>
<ul>
<li><em><strong>A working Hugo website:</strong></em>
<ul>
<li>Set up a Hugo website.</li>
</ul>
</li>
<li><em><strong>A Git repository:</strong></em>
<ul>
<li>Host your Hugo website&rsquo;s code in a Gitea / Git repository.</li>
</ul>
</li>
<li><em><strong>A working Drone instance:</strong></em>
<ul>
<li>Your Drone instance should be working correctly with your Gitea repository.</li>
</ul>
</li>
<li><em><strong>A Cloudflare account:</strong></em>
<ul>
<li>Your Cloudflare account is running, and you have configured both a new Cloudflare Page and have a Cloudflare Workers API set up.</li>
</ul>
</li>
</ul>
<h2 id="a-drone-build-step-for-cloudflare-pages">A Drone Build Step for Cloudflare Pages</h2>
<p>This guide follows on from my previous post where I walk through setting up a full drone pipeline with Test, Staging and Production builds and deployments. To catch up have a look at <a href="/posts/2023/05/complete-drone-pipeline/">Creating a Full Drone Pipeline with Gitea and Drone</a></p>
<p>The following is a step that will build static content located in /drone/src/<!-- raw HTML omitted -->/. Deploy to a Cloudflare Page defined by <!-- raw HTML omitted -->, and a <a href="https://docs.drone.io/secret/repository/">Drone secret</a> called &lt;CLOUDFLARE_API_TOKEN&gt; containing a <a href="https://developers.cloudflare.com/workers/wrangler/ci-cd/#create-a-cloudflare-api-token">Cloudflare API token</a>.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-YAML" data-lang="YAML"><span style="display:flex;"><span>- <span style="color:#f92672">name</span>: <span style="color:#ae81ff">Build Node.js</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">node:latest</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">commands</span>: 
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">npm install npm@latest -g </span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">mkdir -p dist</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">cp -R /drone/src/&lt;your static files location&gt;/* dist/ </span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  - <span style="color:#f92672">name</span>: <span style="color:#ae81ff">Deploy to Cloudflare Pages</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">node:latest</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">environment</span>:
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">CLOUDFLARE_API_TOKEN</span>:
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">from_secret</span>: <span style="color:#ae81ff">CLOUDFLARE_API_TOKEN</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">commands</span>: 
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">npm install wrangler --save-dev  </span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">cd /drone/src/dist/</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">npx wrangler pages deploy /drone/src/dist --project-name &#34;&lt;project&gt;&#34; --env production --branch main --commit-dirty=true</span>
</span></span></code></pre></div><p>There are a number of Docker images out there using Wrangler to deploy to Cloudflare. Unfortunately they all seem to use the previous version of the Cloudflare API which will deprecated shortly. So I went down the route of building Wrangler and NodeJS directly.</p>
<p>Do get in touch if you find a more recent Docker image for me to try.</p>
<h2 id="a-drone-pipeline-for-cloudflare-pages">A Drone Pipeline for Cloudflare Pages</h2>
<p>This is a direct copy of my current production build pipeline. This has added debugging information that I find useful whilst making changes.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-YAML" data-lang="YAML"><span style="display:flex;"><span>---
</span></span><span style="display:flex;"><span><span style="color:#f92672">kind</span>: <span style="color:#ae81ff">pipeline</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">type</span>: <span style="color:#ae81ff">docker</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">name</span>: <span style="color:#e6db74">&#34;Production Environment&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">trigger</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">branch</span>: 
</span></span><span style="display:flex;"><span>    - <span style="color:#ae81ff">main </span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">event</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">include</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">push</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">steps</span>:   
</span></span><span style="display:flex;"><span>  - <span style="color:#f92672">name</span>: <span style="color:#ae81ff">Clone Git Submodules</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">alpine/git </span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">commands</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">git submodule init</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">git submodule update --recursive --remote</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  - <span style="color:#f92672">name</span>: <span style="color:#ae81ff">Build with Hugo</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">binaryronin/drone-hugo:latest</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">pull</span>: <span style="color:#ae81ff">always </span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">commands</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">echo &#34;Checking Hugo version.&#34;</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">hugo version</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">cd /drone/src/hugo/</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">hugo --environment production --verbose --debug </span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">ls -al /drone/src/hugo/public</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  - <span style="color:#f92672">name</span>: <span style="color:#ae81ff">Build Node.js</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">node:latest</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">commands</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">ls -la /drone/src/</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">npm install npm@latest -g</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">pwd</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">mkdir -p dist</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">cp -R /drone/src/hugo/public/* dist/</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">ls -la dist/</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  - <span style="color:#f92672">name</span>: <span style="color:#ae81ff">Deploy to Cloudflare Pages</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">node:latest</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">environment</span>:
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">CLOUDFLARE_API_TOKEN</span>:
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">from_secret</span>: <span style="color:#ae81ff">CLOUDFLARE_API_TOKEN</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">commands</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">ls -la /drone/src/</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">npm install wrangler --save-dev </span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">npx wrangler --version</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">cd /drone/src/dist/</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">npx wrangler pages deploy /drone/src/dist --project-name &#34;&lt;project-name&gt;&#34; --env production --branch main --commit-message &#34;Drone Production Build&#34; --commit-dirty=true</span>
</span></span></code></pre></div><h2 id="summary">Summary</h2>
<p>Once deployed, a new Deployment will be available for you in your Cloudflare Pages area that can be accessed either by the temporary <a href="https://developers.cloudflare.com/pages/platform/preview-deployments/">preview deployment</a> domain or your <a href="https://developers.cloudflare.com/pages/platform/preview-deployments/#preview-aliases">preview alias</a> project domain.</p>
<p>Once happy you can now set up a <a href="https://developers.cloudflare.com/pages/platform/custom-domains/">custom domain</a> to point to the new Page.</p>
<p>Although a few extra steps than an existing Docker Cloudflare setup, this method does allow you to clearly see what&rsquo;s going on and debug any issues should they happen.</p>
<p>If you want to look further in to any of the above tools, do take a look at my previous posts and read the official documentation from the following sites:</p>
<p><a href="https://developers.cloudflare.com/workers/">Cloudflare Workers</a>
<a href="https://gitea.io/en-us/">Gitea</a>
<a href="https://www.drone.io/">Drone</a>
<a href="https://gohugo.io/">Hugo</a></p>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
