|
| 1 | +name: Backend |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + python-version: |
| 7 | + description: "The Python version to set up." |
| 8 | + type: number |
| 9 | + required: false |
| 10 | + default: 3.8 |
| 11 | + secrets: |
| 12 | + CODECOV_TOKEN: |
| 13 | + description: "The token used to gain access to Codecov." |
| 14 | + required: false |
| 15 | + GCP_CREDENTIALS: |
| 16 | + description: "The JSON credentials used to access GCP." |
| 17 | + required: false |
| 18 | + |
| 19 | +jobs: |
| 20 | + validate-pr-refs: |
| 21 | + uses: ocadotechnology/codeforlife-workspace/.github/workflows/validate-pull-request-refs.yaml@main |
| 22 | + |
| 23 | + test: |
| 24 | + uses: ocadotechnology/codeforlife-workspace/.github/workflows/test-python-code.yaml@main |
| 25 | + secrets: inherit |
| 26 | + with: |
| 27 | + python-version: ${{ inputs.python-version }} |
| 28 | + |
| 29 | + deploy: |
| 30 | + runs-on: ubuntu-latest |
| 31 | + needs: [validate-pr-refs, test] |
| 32 | + # Only deploy if the repo's owner is Ocado Tech. and a change is made to an environment's branch. |
| 33 | + if: github.repository_owner_id == 2088731 && (github.ref_name == 'production' || github.ref_name == 'development' || github.ref_name == 'staging') |
| 34 | + environment: ${{ github.ref_name }} |
| 35 | + steps: |
| 36 | + - name: 🐍 Set up Python ${{ inputs.python-version }} Environment |
| 37 | + uses: ocadotechnology/codeforlife-workspace/.github/actions/python/setup-environment@main |
| 38 | + with: |
| 39 | + python-version: ${{ inputs.python-version }} |
| 40 | + install-args: --dev |
| 41 | + |
| 42 | + - name: 🏗️ Generate requirements.txt |
| 43 | + run: pipenv requirements > requirements.txt |
| 44 | + |
| 45 | + - name: 🏗️ Collect Static Files |
| 46 | + run: pipenv run python ./manage.py collectstatic --noinput --clear |
| 47 | + |
| 48 | + # https://mikefarah.gitbook.io/yq/ |
| 49 | + # TODO: clean up app.yaml environment variables |
| 50 | + - name: 🖊️ Configure App Deployment |
| 51 | + uses: mikefarah/yq@master |
| 52 | + with: |
| 53 | + cmd: | |
| 54 | + # Set name with convention "{ENV_NAME}-{REPO_NAME}" |
| 55 | + name=${{ github.repository }} |
| 56 | + name=${name#"ocadotechnology/codeforlife-"} |
| 57 | + name="${{ github.ref_name }}-${name}" |
| 58 | +
|
| 59 | + # Check if service is the client-facing service. |
| 60 | + is_root=$( |
| 61 | + if [ ${{ github.ref_name }} == "production" ] |
| 62 | + then echo "1" |
| 63 | + else echo "0" |
| 64 | + fi |
| 65 | + ) |
| 66 | +
|
| 67 | + # Set runtime with convention "python{PY_VERSION}". |
| 68 | + # The version must have the dot removed: "python3.8" -> "python38". |
| 69 | + runtime=python${{ inputs.python-version }} |
| 70 | + runtime=${runtime//.} |
| 71 | +
|
| 72 | + yq -i ' |
| 73 | + .runtime = "'$runtime'" | |
| 74 | + .service = "'$name'" | |
| 75 | + .env_variables.SECRET_KEY = "${{ secrets.SECRET_KEY }}" | |
| 76 | + .env_variables.SERVICE_NAME = "$name" | |
| 77 | + .env_variables.SERVICE_IS_ROOT = "$is_root" | |
| 78 | + .env_variables.MODULE_NAME = "${{ github.ref_name }}" |
| 79 | + ' app.yaml |
| 80 | +
|
| 81 | + - name: 🚀 Deploy App on GCloud |
| 82 | + uses: ocadotechnology/codeforlife-workspace/.github/actions/gcloud/deploy-app@main |
| 83 | + with: |
| 84 | + gcp-credentials: ${{ secrets.GCP_CREDENTIALS }} |
0 commit comments