Skip to content

Commit

Permalink
chore: add staging environment (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtyler authored Nov 14, 2022
1 parent cdb44f6 commit 071478d
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ module.exports = function (eleventyConfig) {
});

eleventyConfig.addShortcode("lastUpdated", function (component) {
if (process.env.STAGING) return '';

const dirPath = path.join(__dirname, "src/moj/components", component);
const [commit, lastUpdated] = execSync(
`LANG=en_GB git log -n1 --pretty=format:%H,%ad --date=format:'%e %B %Y' ${dirPath}`
Expand Down
48 changes: 48 additions & 0 deletions .github/workflows/cd-staging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Continuous Deployment

# For a description of how this works, see this Cloud Platform User Guide page:
# https://user-guide.cloud-platform.service.justice.gov.uk/documentation/deploying-an-app/github-actions-continuous-deployment.html

on:
workflow_dispatch:
push:
branches:
- 'staging'

env:
ECR_NAME: ${{ secrets.ECR_NAME }}
KUBE_CLUSTER: ${{ secrets.KUBE_CLUSTER }}
KUBE_NAMESPACE: ${{ secrets.KUBE_NAMESPACE }}

jobs:
staging:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Build
run: docker build -t foo .
- name: Push to ECR
id: ecr
uses: jwalton/gh-ecr-push@v1
with:
access-key-id: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }}
secret-access-key: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }}
region: eu-west-2
local-image: foo
image: ${ECR_NAME}:${{ github.sha }}
- name: Update image tag and branch name
run: export IMAGE_TAG=${{ github.sha }} && export BRANCH=${GITHUB_REF##*/} && cat kubernetes-deploy-${GITHUB_REF##*/}.tpl | envsubst > kubernetes-deploy.yaml
- name: Authenticate to the cluster
env:
KUBE_CERT: ${{ secrets.KUBE_CERT }}
KUBE_TOKEN: ${{ secrets.KUBE_TOKEN }}
run: |
echo "${KUBE_CERT}" > ca.crt
kubectl config set-cluster ${KUBE_CLUSTER} --certificate-authority=./ca.crt --server=https://${KUBE_CLUSTER}
kubectl config set-credentials deploy-user --token=${KUBE_TOKEN}
kubectl config set-context ${KUBE_CLUSTER} --cluster=${KUBE_CLUSTER} --user=deploy-user --namespace=${KUBE_NAMESPACE}
kubectl config use-context ${KUBE_CLUSTER}
- name: Apply the updated manifest
run: |
kubectl -n ${KUBE_NAMESPACE} apply -f kubernetes-deploy.yaml
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM node:lts-slim AS build

RUN apt-get update && apt-get -y install autoconf gcc make

WORKDIR /app
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm ci

COPY assets assets
COPY docs docs
COPY src src
COPY package package
COPY .eleventy.js .eleventy.js
COPY gulp gulp
COPY gulpfile.js gulpfile.js
COPY README.md README.md
COPY webpack.config.js webpack.config.js
RUN STAGING=1 npm run build:docs

FROM nginxinc/nginx-unprivileged:alpine AS nginx

EXPOSE 3000

RUN sed -i -e 's/8080;/3000;/g' /etc/nginx/conf.d/default.conf

COPY --from=build /app/public /usr/share/nginx/html
5 changes: 5 additions & 0 deletions docs/_data/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function() {
return {
isStaging: process.env.STAGING || false
};
};
18 changes: 18 additions & 0 deletions docs/_includes/layouts/partials/header.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
{% from "govuk/components/cookie-banner/macro.njk" import govukCookieBanner %}

{% if env.isStaging %}
<style>
body { border: 10px solid orangered; }
.app-staging {
padding: 20px;
text-align: center;
background-color: orangered;
color: white;
font-family: "GDS Transport",arial,sans-serif;
}
</style>
<aside class="app-staging">
<h1>This is a staging environment!</h1>
<p>This site is designed to show upcoming previews of changes to the documentation and may not reflect what's actually possible or best practices.</p>
<p>See <a href="https://design-patterns.service.justice.gov.uk/">the real documentation</a> for up-to-date information.</p>
</aside>
{% endif %}

{% set html %}
<p class="govuk-body">We’d like to use analytics cookies so we can understand how you use the service and make improvements.</p>
{% endset %}
Expand Down
68 changes: 68 additions & 0 deletions kubernetes-deploy-staging.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: moj-prototype-${BRANCH}
spec:
replicas: 1
selector:
matchLabels:
app: prototype-${BRANCH}
template:
metadata:
labels:
app: prototype-${BRANCH}
spec:
containers:
- name: prototype
image: 754256621582.dkr.ecr.eu-west-2.amazonaws.com/${ECR_NAME}:${IMAGE_TAG}
env:
- name: USERNAME
valueFrom:
secretKeyRef:
name: basic-auth
key: username
- name: PASSWORD
valueFrom:
secretKeyRef:
name: basic-auth
key: password
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: prototype-service-${BRANCH}
labels:
app: prototype-service-${BRANCH}
spec:
ports:
- port: 3000
name: http
targetPort: 3000
selector:
app: prototype-${BRANCH}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: prototype-ingress-${BRANCH}
annotations:
external-dns.alpha.kubernetes.io/set-identifier: prototype-ingress-${BRANCH}-${KUBE_NAMESPACE}-green
external-dns.alpha.kubernetes.io/aws-weight: "100"
spec:
ingressClassName: default
tls:
- hosts:
- ${KUBE_NAMESPACE}-${BRANCH}.apps.live.cloud-platform.service.justice.gov.uk
rules:
- host: ${KUBE_NAMESPACE}-${BRANCH}.apps.live.cloud-platform.service.justice.gov.uk
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: prototype-service-${BRANCH}
port:
number: 3000

0 comments on commit 071478d

Please sign in to comment.