Skip to content

Commit

Permalink
First iteration. (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattops authored May 16, 2024
1 parent c7f6192 commit 9468d00
Show file tree
Hide file tree
Showing 18 changed files with 766 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/actions/cloud-platform-auth/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Cloud Platform Auth
description: Authenticate with MOJ Cloud Platform

inputs:
api:
description: The KUBE_ENV_API
required: true
cert:
description: The KUBE_CERT
required: true
cluster:
description: The KUBE_CLUSTER
required: true
namespace:
description: The KUBE_NAMESPACE
required: true
token:
description: The KUBE_TOKEN
required: true

runs:
using: composite
steps:
- name: Authenticate
shell: bash
run: |
echo "${{ inputs.cert }}" > ca.crt
kubectl config set-cluster ${{ inputs.cluster }} --certificate-authority=./ca.crt --server=${{ inputs.api }}
kubectl config set-credentials cd-serviceaccount --token=${{ inputs.token }}
kubectl config set-context ${{ inputs.cluster }} --cluster=${{ inputs.cluster }} --user=cd-serviceaccount --namespace=${{ inputs.namespace }}
kubectl config use-context ${{ inputs.cluster }}
59 changes: 59 additions & 0 deletions .github/actions/cloud-platform-deploy/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Cloud Platform Deploy
description: Deploy to Cloud Platform using Helm

inputs:
environment:
description: The environment to deploy to (dev/preprod/prod)
required: true
version:
description: The version of the service to deploy
required: true
api:
description: The KUBE_ENV_API
required: true
cert:
description: The KUBE_CERT
required: true
cluster:
description: The KUBE_CLUSTER
required: true
namespace:
description: The KUBE_NAMESPACE
required: true
token:
description: The KUBE_TOKEN
required: true

runs:
using: composite
steps:
- uses: actions/checkout@v3

- name: Authenticate
uses: ./.github/actions/cloud-platform-auth
with:
api: ${{ inputs.api }}
cert: ${{ inputs.cert }}
cluster: ${{ inputs.cluster }}
namespace: ${{ inputs.namespace }}
token: ${{ inputs.token }}

- name: Deploy
shell: bash
run: |
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
brew install helm
cd helm_deploy/${{ github.event.repository.name }}
yq -i ".appVersion = \"${{ inputs.version }}\"" "Chart.yaml"
helm dependency update .
exec helm upgrade '${{ github.event.repository.name }}' . \
--atomic \
--history-max 10 \
--force \
--install \
--reset-values \
--set 'generic-service.image.tag=${{ inputs.version }}' \
--set 'version=${{ inputs.version }}' \
--timeout 10m \
--values '${{ steps.env.outputs.values-file }}' \
--wait
34 changes: 34 additions & 0 deletions .github/actions/docker-build/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build Docker image
description: Build, and optionally push, a Docker image

inputs:
project:
description: Project name
push:
description: Whether to push images to the registry
default: 'false'
version:
description: Version

runs:
using: "composite"
steps:
- uses: docker/setup-qemu-action@v2
- uses: docker/setup-buildx-action@v2
- uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}

- name: Build Docker images
uses: docker/build-push-action@v4
with:
cache-from: type=gha
cache-to: type=gha,mode=max
context: .
push: ${{ inputs.push }}
provenance: false
tags: |
ghcr.io/ministryofjustice/${{ inputs.project }}:latest
ghcr.io/ministryofjustice/${{ inputs.project }}:${{ inputs.version }}
54 changes: 54 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Build

permissions:
packages: write
contents: read

on:
workflow_call:
inputs:
push:
type: boolean
default: false
force-deploy:
type: boolean
default: false
outputs:
version:
value: ${{ jobs.build-docker.outputs.version }}
workflow_dispatch:
inputs:
push:
description: Push images
type: boolean
default: false

env:
push: ${{ inputs.push }}

jobs:
build-docker:
name: Docker build
runs-on: ubuntu-latest
strategy:
matrix:
project:
- hmpps-terraform-discovery
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- uses: actions/checkout@v3

- name: Set version
id: version
run: |
version=$(date '+%Y-%m-%d').${{ github.run_number }}.$(echo ${{ github.sha }} | cut -c1-7)
echo "version=$version" | tee -a "$GITHUB_OUTPUT"
- name: Build Docker images
uses: ./.github/actions/docker-build
id: build
with:
project: ${{ matrix.project }}
push: ${{ env.push }}
version: ${{ steps.version.outputs.version }}
58 changes: 58 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Deploy

on:
workflow_call:
inputs:
github_environment:
description: The name of the github environment for deployment secrets
type: string
required: true
environment:
description: The name of the environment to deploy to
type: string
required: true
version:
description: The image version to deploy
type: string
required: true

workflow_dispatch:
inputs:
github_environment:
description: The name of the github environment for deployment secrets
type: choice
required: true
options:
- development
- production
environment:
description: Environment
type: choice
required: true
options:
- dev
version:
description: Image version
type: string
required: true

jobs:
deploy:
runs-on: ubuntu-latest
strategy:
fail-fast: false
environment:
name: ${{ inputs.github_environment }}
steps:
- uses: actions/checkout@v3

- name: Deploy to Cloud Platform
uses: ./.github/actions/cloud-platform-deploy
with:
environment: ${{ inputs.environment }}
version: ${{ inputs.version }}
api: https://${{ secrets.DEVELOPMENT_KUBE_CLUSTER }}
cert: ${{ secrets.DEVELOPMENT_KUBE_CERT }}
cluster: ${{ secrets.DEVELOPMENT_KUBE_CLUSTER }}
namespace: ${{ secrets.DEVELOPMENT_KUBE_NAMESPACE }}
token: ${{ secrets.DEVELOPMENT_KUBE_TOKEN }}
29 changes: 29 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Pipeline

permissions:
packages: write
contents: read

on:
push:
branches:
- main
workflow_dispatch: # Can be triggered manually from a branch

jobs:
build:
name: Build
uses: ./.github/workflows/build.yml
with:
push: true
secrets: inherit

deploy-to-dev:
name: Deploy to dev
uses: ./.github/workflows/deploy.yml
needs: build
with:
github_environment: development
environment: dev
version: ${{ needs.build.outputs.version }}
secrets: inherit
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# dotenv environment variables file
.env*
tmp*
helm_deploy/hmpps-terraform-discovery/charts/*.tgz

.python-version
.idea
.vscode
**/Chart.lock
__pycache__/
**/.DS_Store
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM python:3.10 AS builder
COPY requirements.txt .

RUN addgroup --gid 2000 --system appgroup && \
adduser --uid 2000 --system appuser --gid 2000 --home /home/appuser

USER 2000

# install dependencies to the local user directory
RUN pip install --user -r requirements.txt

FROM python:3.10-slim
WORKDIR /app

RUN addgroup --gid 2000 --system appgroup && \
adduser --uid 2000 --system appuser --gid 2000 --home /home/appuser

# copy the dependencies from builder stage
COPY --chown=appuser:appgroup --from=builder /home/appuser/.local /home/appuser/.local
COPY ./terraform_discovery.py .

# update PATH environment variable
ENV PATH=/home/appuser/.local:$PATH

USER 2000

CMD [ "python", "-u", "terraform_discovery.py" ]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020-2021 Crown Copyright (Ministry of Justice)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions helm_deploy/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
Loading

0 comments on commit 9468d00

Please sign in to comment.