chore: use DigitalOcean spaces for deployment

This commit is contained in:
Evelyn Hathaway
2021-05-07 17:41:51 -07:00
parent 40a2782367
commit 0a0ed073b7
9 changed files with 327 additions and 80 deletions

74
.github/workflows/deploy-beta.yml vendored Normal file
View File

@@ -0,0 +1,74 @@
name: deploy-beta
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
env:
CI: true
steps:
- name: Create GitHub deployment
uses: bobheadxi/deployments@v0.4.3
id: deployment
with:
step: start
token: ${{ github.token }}
env: beta
- uses: actions/checkout@v2
- name: Use Node.js 14.x
uses: actions/setup-node@v1
with:
node-version: '14'
# From: https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#example-using-the-cache-action
- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install Dependencies
run: npm ci
- name: Build
run: npm run build-beta
- uses: jakejarvis/s3-sync-action@v0.5.1
name: Sync files to DigitalOcean Spaces
with:
args: --acl public-read --follow-symlinks --delete
env:
AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_S3_ENDPOINT: https://sfo2.digitaloceanspaces.com
SOURCE_DIR: public
DEST_DIR: beta
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Flush DigitalOcean Spaces cache
run: doctl compute cdn flush ${{ secrets.DIGITALOCEAN_CDN_ID }} --files beta/*
- name: Update deployment status
uses: bobheadxi/deployments@v0.4.3
if: always()
with:
step: finish
token: ${{ github.token }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: https://beta.unicorn-utterances.com

83
.github/workflows/deploy-dev.yml vendored Normal file
View File

@@ -0,0 +1,83 @@
name: deploy-dev
on: pull_request
jobs:
deploy:
if: github.repository_owner === 'unicorn-utterances'
runs-on: ubuntu-latest
env:
CI: true
steps:
# see https://dev.to/bobheadxi/branch-previews-with-google-app-engine-and-github-actions-3pco
- name: Extract branch name
id: get_branch
shell: bash
env:
PR_HEAD: ${{ github.head_ref }}
run: echo "##[set-output name=branch;]$(echo ${PR_HEAD#refs/heads/} | tr / -)"
- name: Create GitHub deployment
uses: bobheadxi/deployments@v0.4.3
id: deployment
with:
step: start
token: ${{ github.token }}
env: ${{ steps.get_branch.outputs.branch }}
ref: ${{ github.head_ref }}
- uses: actions/checkout@v2
- name: Use Node.js 14.x
uses: actions/setup-node@v1
with:
node-version: '14'
# From: https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#example-using-the-cache-action
- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install Dependencies
run: npm ci
- name: Build
run: npm run build-beta
env:
SITE_URL: https://${{ steps.get_branch.outputs.branch }}.unicorn-utterances.com
- uses: jakejarvis/s3-sync-action@v0.5.1
name: Sync files to DigitalOcean Spaces
with:
args: --acl public-read --follow-symlinks --delete
env:
AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_S3_ENDPOINT: https://sfo2.digitaloceanspaces.com
SOURCE_DIR: public
DEST_DIR: ${{ steps.get_branch.outputs.branch }}
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Flush DigitalOcean Spaces cache
run: doctl compute cdn flush ${{ secrets.DIGITALOCEAN_CDN_ID }} --files ${{ steps.get_branch.outputs.branch }}/*
- name: Update deployment status
uses: bobheadxi/deployments@v0.4.3
if: always()
with:
step: finish
token: ${{ github.token }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: https://${{ steps.get_branch.outputs.branch }}.unicorn-utterances.com

71
.github/workflows/deploy-prod.yml vendored Normal file
View File

@@ -0,0 +1,71 @@
name: deploy-prod
on: workflow_dispatch
jobs:
deploy:
runs-on: ubuntu-latest
env:
CI: true
steps:
- name: Create GitHub deployment
uses: bobheadxi/deployments@v0.4.3
id: deployment
with:
step: start
token: ${{ github.token }}
env: production
- uses: actions/checkout@v2
- name: Use Node.js 14.x
uses: actions/setup-node@v1
with:
node-version: '14'
# From: https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#example-using-the-cache-action
- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install Dependencies
run: npm ci
- name: Build
run: npm run build
- uses: jakejarvis/s3-sync-action@v0.5.1
name: Sync files to DigitalOcean Spaces
with:
args: --acl public-read --follow-symlinks --delete
env:
AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_S3_ENDPOINT: https://sfo2.digitaloceanspaces.com
SOURCE_DIR: public
DEST_DIR: prod
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Flush DigitalOcean Spaces cache
run: doctl compute cdn flush ${{ secrets.DIGITALOCEAN_CDN_ID }} --files prod/*
- name: Update deployment status
uses: bobheadxi/deployments@v0.4.3
if: always()
with:
step: finish
token: ${{ github.token }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: https://unicorn-utterances.com

40
.github/workflows/destroy-dev.yml vendored Normal file
View File

@@ -0,0 +1,40 @@
name: destroy-dev
on:
pull_request:
types:
- closed
jobs:
destroy:
if: github.repository_owner === 'unicorn-utterances'
runs-on: ubuntu-latest
env:
CI: true
steps:
# see https://dev.to/bobheadxi/branch-previews-with-google-app-engine-and-github-actions-3pco
- name: Extract branch name
id: get_branch
shell: bash
env:
PR_HEAD: ${{ github.head_ref }}
run: echo "##[set-output name=branch;]$(echo ${PR_HEAD#refs/heads/} | tr / -)"
- name: Remove files from DigitalOcean Spaces
uses: vitorsgomes/s3-rm-action@v1.0.1
with:
args: --recursive
env:
AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_S3_ENDPOINT: https://sfo2.digitaloceanspaces.com
PATH_TO_DELETE: "${{ steps.get_branch.outputs.branch }}"
- name: Mark environment as deactivated
uses: bobheadxi/deployments@v0.4.3
with:
step: deactivate-env
token: ${{ github.token }}
env: ${{ steps.get_branch.outputs.branch }}
desc: Deployment was pruned

39
.github/workflows/test.yml vendored Normal file
View File

@@ -0,0 +1,39 @@
name: test
on: push
jobs:
test-lint-build:
env:
CI: true
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 14.x
uses: actions/setup-node@v1
with:
node-version: '14'
# From: https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#example-using-the-cache-action
- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install Dependencies
run: npm ci
- name: Test
run: npm test
- name: Lint
run: npm run lint
- name: Build
run: npm run build

View File

@@ -1,42 +0,0 @@
name: testing-deploying
on: push
jobs:
testing-deploying:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 14.x
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Run npm install, test, and build
run: |
npm ci
npm test
npm run build
env:
CI: true
- name: Deploy to beta.unicorn-utterances.com
if: github.ref == 'refs/heads/integration'
uses: AEnterprise/rsync-deploy@v1.0
env:
DEPLOY_KEY: ${{ secrets.JAILED_PRIVATE_KEY }}
ARGS: "-rlt --delete --delete-delay --delay-updates"
SERVER_PORT: 22
FOLDER: "./public/"
SERVER_IP: unicorn-utterances.com
USERNAME: ${{ secrets.JAILED_USERNAME }}
SERVER_DESTINATION: ${{ secrets.JAILED_BETA_PATH }}
- name: Deploy to unicorn-utterances.com
if: github.ref == 'refs/heads/master'
uses: AEnterprise/rsync-deploy@v1.0
env:
DEPLOY_KEY: ${{ secrets.JAILED_PRIVATE_KEY }}
ARGS: "-rlt --delete --delete-delay --delay-updates"
SERVER_PORT: 22
FOLDER: "./public/"
SERVER_IP: unicorn-utterances.com
USERNAME: ${{ secrets.JAILED_USERNAME }}
SERVER_DESTINATION: ${{ secrets.JAILED_PROD_PATH }}

View File

@@ -6,8 +6,6 @@ As a site and community alike, we have a myriad of ways to contribute to the sit
- [Markdown Post](#Markdown-Post) - [Markdown Post](#Markdown-Post)
- [Editing a Blog Post](#Editing-a-Blog-Post) - [Editing a Blog Post](#Editing-a-Blog-Post)
- [Code Contributions](#Code) - [Code Contributions](#Code)
- [Deployment](#Deployment)
# Blog Posts # Blog Posts
@@ -149,32 +147,6 @@ Keep in mind that we request developers reach out [via our Discord](https://disc
To start the development server, run `npm run develop`, it will then start the local instance at `http://localhost:8000`. You also can check out the GraphiQL tool at `http://localhost:8000/___graphql`. This is a tool you can use to experiment with querying your data. Learn more about using this tool in the [Gatsby tutorial](https://www.gatsbyjs.org/tutorial/part-five/#introducing-graphiql). To start the development server, run `npm run develop`, it will then start the local instance at `http://localhost:8000`. You also can check out the GraphiQL tool at `http://localhost:8000/___graphql`. This is a tool you can use to experiment with querying your data. Learn more about using this tool in the [Gatsby tutorial](https://www.gatsbyjs.org/tutorial/part-five/#introducing-graphiql).
## Debugging Plugins ## Debugging Plugins
We have a few local plugins for Gatsby. However, I've found that while debugging these plugins, Gatsby's cache can often get in the way. If you run `npm run debug`, it will create a debuggable ([using Chrome](https://unicorn-utterances.com/posts/debugging-nodejs-programs-using-chrome/)) instance of the Gatsby develop mode and clear the cache of Gatsby. This script will ensure that your debugger provides proper insight into what's happening in the build process. We have a few local plugins for Gatsby. However, I've found that while debugging these plugins, Gatsby's cache can often get in the way. If you run `npm run debug`, it will create a debuggable ([using Chrome](https://unicorn-utterances.com/posts/debugging-nodejs-programs-using-chrome/)) instance of the Gatsby develop mode and clear the cache of Gatsby. This script will ensure that your debugger provides proper insight into what's happening in the build process.
# Deployment
## Git Strategy
We loosely follow [the Gitflow branching strategy](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow)
where our development branch is called `integration`, and our mainline branch is called `master`.
This means that any new pull requests should be made against `integration`
unless it is an emergency hotfix (to be approved by the DevOps team).
We also have the `master` branch, which is a live reflection of the code
hosted on the server. Any time `integration` is pulled into `master`, the
site will be deployed. A PR from `integration` to `master` should only be
opened by a Unicorn Utterances team member and must be approved by at
least one DevOps member
## CI/CD
As mentioned before, we follow the GitFlow branching strategy. What we didn't say before is how we utilize those branches to deploy our code. Upon a PR being merged into `integration`, we have [a GitHub Action to deploy](https://github.com/unicorn-utterances/unicorn-utterances/actions?query=workflow%3Atesting-deploying) those changes to our beta link:
[https://beta.unicorn-utterances.com](https://beta.unicorn-utterances.com)
Likewise, we make periodic PRs to the `master` branch. We call these PR's `Integration merge MM/DD/YY`. These PRs **must** be approved by one or more of the members of the DevOps team before being able to be merged. Upon merging, there will be an automated deployment ([using the same GitHub action](https://github.com/unicorn-utterances/unicorn-utterances/actions?query=workflow%3Atesting-deploying)) to our homepage:
[https://unicorn-utterances.com](https://unicorn-utterances.com)

View File

@@ -3,11 +3,20 @@ const { SeriesToC } = require("./src/components/series-toc");
let CONSTS = require("./config/gatsby-config-consts"); let CONSTS = require("./config/gatsby-config-consts");
if (!CONSTS) CONSTS = {}; if (!CONSTS) CONSTS = {};
const buildMode = process.env.BUILD_ENV || "production";
const siteUrl =
process.env.SITE_URL ||
(buildMode === "production"
? "https://unicorn-utterances.com"
: "https://beta.unicorn-utterances.com");
console.log(`Building for ${buildMode} at ${siteUrl}`);
module.exports = { module.exports = {
siteMetadata: { siteMetadata: {
title: `Unicorn Utterances`, title: `Unicorn Utterances`,
description: `Learning programming from magically majestic words. A place to learn about all sorts of programming topics from entry-level concepts to advanced abstractions`, description: `Learning programming from magically majestic words. A place to learn about all sorts of programming topics from entry-level concepts to advanced abstractions`,
siteUrl: `https://unicorn-utterances.com/`, siteUrl,
disqusShortname: "unicorn-utterances", disqusShortname: "unicorn-utterances",
repoPath: "unicorn-utterances/unicorn-utterances", repoPath: "unicorn-utterances/unicorn-utterances",
relativeToPosts: "/content/blog", relativeToPosts: "/content/blog",
@@ -65,7 +74,7 @@ module.exports = {
options: { options: {
maxWidth: 590, maxWidth: 590,
linkImagesToOriginal: false, linkImagesToOriginal: false,
backgroundColor: 'transparent' backgroundColor: `transparent`
} }
}, },
{ {
@@ -340,11 +349,11 @@ module.exports = {
{ {
resolve: "gatsby-plugin-robots-txt", resolve: "gatsby-plugin-robots-txt",
options: { options: {
resolveEnv: () => process.env.GITHUB_REF, resolveEnv: () => buildMode,
env: { env: {
"refs/heads/integration": { development: {
host: "https://beta.unicorn-utterances.com/sitemap.xml", host: siteUrl,
sitemap: "https://beta.unicorn-utterances.com/sitemap.xml", sitemap: `${siteUrl}/sitemap.xml`,
policy: [ policy: [
{ {
userAgent: "*", userAgent: "*",
@@ -352,9 +361,9 @@ module.exports = {
} }
] ]
}, },
"refs/heads/master": { production: {
host: "https://unicorn-utterances.com/sitemap.xml", host: siteUrl,
sitemap: `https://unicorn-utterances.com/sitemap.xml`, sitemap: `${siteUrl}/sitemap.xml`,
policy: [ policy: [
{ {
userAgent: "*", userAgent: "*",
@@ -364,7 +373,7 @@ module.exports = {
} }
} }
} }
}, }
], ],
mapping: { mapping: {
"MarkdownRemark.frontmatter.authors": `UnicornsJson`, "MarkdownRemark.frontmatter.authors": `UnicornsJson`,

View File

@@ -20,6 +20,7 @@
}, },
"scripts": { "scripts": {
"build": "gatsby build", "build": "gatsby build",
"build-beta": "BUILD_ENV=development npm run build",
"debug": "gatsby clean && node --nolazy --inspect-brk --max-old-space-size=2000 ./node_modules/gatsby/dist/bin/gatsby.js develop", "debug": "gatsby clean && node --nolazy --inspect-brk --max-old-space-size=2000 ./node_modules/gatsby/dist/bin/gatsby.js develop",
"develop": "gatsby develop", "develop": "gatsby develop",
"format": "run-s format:**", "format": "run-s format:**",