Skip to content

Commit

Permalink
Merge pull request #108 from ocadotechnology/configure_js_services
Browse files Browse the repository at this point in the history
add prettier
  • Loading branch information
SKairinos authored May 29, 2024
2 parents c63c25b + cabab07 commit 70537cd
Show file tree
Hide file tree
Showing 12 changed files with 430 additions and 106 deletions.
23 changes: 23 additions & 0 deletions .github/actions/gcloud/deploy-app/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: "Code for Life - GCloud - Deploy App"
description: "Deploy an app to Google Cloud."
inputs:
gcp-credentials:
description: "The JSON credentials used to access GCP."
required: true
deploy-args:
description: "Arguments to pass to `gcloud app deploy`."
required: false
runs:
using: composite
steps:
- name: 🗝 Authenticate with GCloud
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ inputs.gcp-credentials }}

- name: 🤖 Set up GCloud SDK
uses: google-github-actions/setup-gcloud@v2

- name: 🚀 Deploy App on GCloud
shell: bash
run: gcloud app deploy ${{ inputs.deploy-args }}
42 changes: 42 additions & 0 deletions .github/actions/javascript/setup-environment/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "Code for Life - JavaScript - Setup Environment"
description: "Set up a JavaScript environment."
inputs:
checkout:
description: "A flag to designate if the code should be checked out."
required: true
default: "true"
node-version:
description: "The Node.js version to set up."
required: true
default: "18"
working-directory:
description: "The current working directory."
required: true
default: "."
install-args:
description: "Arguments to pass to pipenv install."
required: false
runs:
using: composite
steps:
- name: 🛫 Checkout
if: ${{ inputs.checkout == 'true' }}
uses: actions/checkout@v4

- name: 🌐 Set up Node
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}

- name: ⬆️ Upgrade npm
shell: bash
run: npm install --global npm

- name: 🛠 Install Yarn
shell: bash
run: npm install --global yarn

- name: 🛠 Install Dependencies
shell: bash
working-directory: ${{ inputs.working-directory }}
run: yarn install ${{ inputs.install-args }}
84 changes: 84 additions & 0 deletions .github/workflows/backend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Backend

on:
workflow_call:
inputs:
python-version:
description: "The Python version to set up."
type: number
required: false
default: 3.8
secrets:
CODECOV_TOKEN:
description: "The token used to gain access to Codecov."
required: false
GCP_CREDENTIALS:
description: "The JSON credentials used to access GCP."
required: false

jobs:
validate-pr-refs:
uses: ocadotechnology/codeforlife-workspace/.github/workflows/validate-pull-request-refs.yaml@main

test:
uses: ocadotechnology/codeforlife-workspace/.github/workflows/test-python-code.yaml@main
secrets: inherit
with:
python-version: ${{ inputs.python-version }}

deploy:
runs-on: ubuntu-latest
needs: [validate-pr-refs, test]
# Only deploy if the repo's owner is Ocado Tech. and a change is made to an environment's branch.
if: github.repository_owner_id == 2088731 && (github.ref_name == 'production' || github.ref_name == 'development' || github.ref_name == 'staging')
environment: ${{ github.ref_name }}
steps:
- name: 🐍 Set up Python ${{ inputs.python-version }} Environment
uses: ocadotechnology/codeforlife-workspace/.github/actions/python/setup-environment@main
with:
python-version: ${{ inputs.python-version }}
install-args: --dev

- name: 🏗️ Generate requirements.txt
run: pipenv requirements > requirements.txt

- name: 🏗️ Collect Static Files
run: pipenv run python ./manage.py collectstatic --noinput --clear

# https://mikefarah.gitbook.io/yq/
# TODO: clean up app.yaml environment variables
- name: 🖊️ Configure App Deployment
uses: mikefarah/yq@master
with:
cmd: |
# Set name with convention "{ENV_NAME}-{REPO_NAME}"
name=${{ github.repository }}
name=${name#"ocadotechnology/codeforlife-"}
name="${{ github.ref_name }}-${name}"
# Check if service is the client-facing service.
is_root=$(
if [ ${{ github.ref_name }} == "production" ]
then echo "1"
else echo "0"
fi
)
# Set runtime with convention "python{PY_VERSION}".
# The version must have the dot removed: "python3.8" -> "python38".
runtime=python${{ inputs.python-version }}
runtime=${runtime//.}
yq -i '
.runtime = "'$runtime'" |
.service = "'$name'" |
.env_variables.SECRET_KEY = "${{ secrets.SECRET_KEY }}" |
.env_variables.SERVICE_NAME = "$name" |
.env_variables.SERVICE_IS_ROOT = "$is_root" |
.env_variables.MODULE_NAME = "${{ github.ref_name }}"
' app.yaml
- name: 🚀 Deploy App on GCloud
uses: ocadotechnology/codeforlife-workspace/.github/actions/gcloud/deploy-app@main
with:
gcp-credentials: ${{ secrets.GCP_CREDENTIALS }}
17 changes: 6 additions & 11 deletions .github/workflows/cron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@ on:
branches:
- main
paths:
- 'cron.yaml'
- "cron.yaml"
workflow_dispatch:

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 🛫 Checkout
uses: actions/checkout@v3

- name: 🗝 Authenticate with GCloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_CREDENTIALS }}

- name: 🤖 Set up GCloud SDK
uses: google-github-actions/setup-gcloud@v1
uses: actions/checkout@v4

- name: 🚀 Deploy Cron Jobs on GCloud
run: gcloud app deploy cron.yaml
uses: ocadotechnology/codeforlife-workspace/.github/actions/gcloud/deploy-app@main
with:
gcp-credentials: ${{ secrets.GCP_CREDENTIALS }}
deploy-args: cron.yaml
17 changes: 6 additions & 11 deletions .github/workflows/dispatch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@ on:
branches:
- main
paths:
- 'dispatch.yaml'
- "dispatch.yaml"
workflow_dispatch:

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 🛫 Checkout
uses: actions/checkout@v3

- name: 🗝 Authenticate with GCloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_CREDENTIALS }}

- name: 🤖 Set up GCloud SDK
uses: google-github-actions/setup-gcloud@v1
uses: actions/checkout@v4

- name: 🚀 Deploy Routing Rules on GCloud
run: gcloud app deploy dispatch.yaml
uses: ocadotechnology/codeforlife-workspace/.github/actions/gcloud/deploy-app@main
with:
gcp-credentials: ${{ secrets.GCP_CREDENTIALS }}
deploy-args: dispatch.yaml
63 changes: 63 additions & 0 deletions .github/workflows/frontend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Frontend

on:
workflow_call:
inputs:
node-version:
description: "The Node.js version to set up."
type: number
required: false
default: 18
secrets:
CODECOV_TOKEN:
description: "The token used to gain access to Codecov."
required: false
GCP_CREDENTIALS:
description: "The JSON credentials used to access GCP."
required: false

jobs:
validate-pr-refs:
uses: ocadotechnology/codeforlife-workspace/.github/workflows/validate-pull-request-refs.yaml@main

test:
uses: ocadotechnology/codeforlife-workspace/.github/workflows/test-javascript-code.yaml@main
secrets: inherit
with:
node-version: ${{ inputs.node-version }}

deploy:
runs-on: ubuntu-latest
needs: [validate-pr-refs, test]
# Only deploy if the repo's owner is Ocado Tech. and a change is made to an environment's branch.
if: github.repository_owner_id == 2088731 && (github.ref_name == 'production' || github.ref_name == 'development' || github.ref_name == 'staging')
environment: ${{ github.ref_name }}
steps:
- name: 🌐 Set up JavaScript ${{ inputs.node-version }} Environment
uses: ocadotechnology/codeforlife-workspace/.github/actions/javascript/setup-environment@main
with:
node-version: ${{ inputs.node-version }}
install-args: --production=false

- name: 🏗️ Build App
run: yarn build

# https://mikefarah.gitbook.io/yq/
- name: 🖊️ Configure App Deployment
uses: mikefarah/yq@master
with:
cmd: |
# Set name with convention "{ENV_NAME}-{REPO_NAME}"
name=${{ github.repository }}
name=${name#"ocadotechnology/codeforlife-"}
name="${{ github.ref_name }}-${name}"
yq -i '
.runtime = "nodejs${{ inputs.node-version }}" |
.service = "'$name'"
' app.yaml
- name: 🚀 Deploy App on GCloud
uses: ocadotechnology/codeforlife-workspace/.github/actions/gcloud/deploy-app@main
with:
gcp-credentials: ${{ secrets.GCP_CREDENTIALS }}
80 changes: 80 additions & 0 deletions .github/workflows/test-javascript-code.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Test JavaScript Code

on:
workflow_call:
inputs:
node-version:
description: "The Node.js version to set up."
type: number
required: false
default: 18
working-directory:
description: "The current working directory."
type: string
required: false
default: "."
codecov-slug:
description: "The slug provided to Codecov for the coverage report."
type: string
required: false
default: ${{ github.repository }}
codecov-yml-path:
description: "The path of the Codecov YAML file."
type: string
required: false
default: "./codecov.yml"
codecov-file:
description: "The path to the coverage file to upload."
type: string
required: false
default: "coverage/cobertura-coverage.xml"
secrets:
CODECOV_TOKEN:
description: "The token used to gain access to Codecov."
required: false # Needs to be false to support contributors

jobs:
test-js-code:
runs-on: ubuntu-latest
env:
OCADO_TECH_ORG_ID: 2088731
steps:
- name: 🌐 Set up JavaScript ${{ inputs.node-version }} Environment
uses: ocadotechnology/codeforlife-workspace/.github/actions/javascript/setup-environment@main
with:
node-version: ${{ inputs.node-version }}
working-directory: ${{ inputs.working-directory }}
install-args: --production=false

- name: 🔎 Check Code Format
working-directory: ${{ inputs.working-directory }}
run: yarn format:check

- name: 🔎 Check Static Type Hints
working-directory: ${{ inputs.working-directory }}
run: yarn type-check

- name: 🔎 Check Static Code
working-directory: ${{ inputs.working-directory }}
run: yarn lint

- name: 🧪 Test Code Units
working-directory: ${{ inputs.working-directory }}
run: |
if [ ${{ github.repository_owner_id }} = ${{ env.OCADO_TECH_ORG_ID }} ]
then
yarn test:coverage
else
yarn test:verbose
fi
- name: 📈 Upload Coverage Reports to Codecov
if: github.repository_owner_id == env.OCADO_TECH_ORG_ID
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
slug: ${{ inputs.codecov-slug }}
codecov_yml_path: ${{ inputs.codecov-yml-path }}
working-directory: ${{ inputs.working-directory }}
file: ${{ inputs.codecov-file }}
2 changes: 1 addition & 1 deletion .github/workflows/test-python-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ on:
secrets:
CODECOV_TOKEN:
description: "The token used to gain access to Codecov."
required: false
required: false # Needs to be false to support contributors

jobs:
test-py-code:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/validate-pull-request-refs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:
jobs:
validate-pr-refs:
runs-on: ubuntu-latest
# If the repo's owner is Ocado Tech. and the triggering event is a pull request.
if: github.repository_owner_id == 2088731 && github.event_name == 'pull_request'
env:
PROD_REF: production
STAGING_REF: staging
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
[submodule "codeforlife-sso"]
path = codeforlife-sso
url = https://github.com/ocadotechnology/codeforlife-sso.git
[submodule "codeforlife-portal-frontend"]
path = codeforlife-portal-frontend
url = https://github.com/ocadotechnology/codeforlife-portal-frontend.git
Loading

0 comments on commit 70537cd

Please sign in to comment.