content_update #178
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Django CI/CD | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
repository_dispatch: | |
types: [content_update] | |
workflow_dispatch: | |
jobs: | |
test-python: | |
runs-on: ubuntu-latest | |
services: | |
postgres: | |
image: postgres:15 | |
env: | |
POSTGRES_USER: postgres | |
POSTGRES_PASSWORD: postgres | |
options: >- | |
--health-cmd pg_isready | |
--health-interval 10s | |
--health-timeout 5s | |
--health-retries 5 | |
ports: | |
# Maps tcp port 5432 on service container to the host | |
- 5432:5432 | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: 3.11 | |
- name: Install Dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
- name: Copy environment variable file for CI | |
run: cp .env-ci .env | |
- name: Setup Playwright | |
run: playwright install | |
- name: Run Tests | |
run: pytest | |
env: | |
# This is provided by GitHub Actions. | |
# We use it to make authenticated API requests in our app. | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
lint-js: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-node@v4 | |
- name: Install npm dependencies | |
run: npm ci | |
- name: Lint | |
run: npm run lint | |
check-js-formatting: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-node@v4 | |
- name: Install npm dependencies | |
run: npm ci | |
- name: Prettier check | |
run: npm run check-formatting | |
deploy: | |
# This workflow is used to check PRs, in which case we don't want to deploy. | |
# Ensure we only deploy after a push to the main branch, or when triggered manually. | |
if: | | |
(github.event_name == 'push' || | |
github.event_name == 'workflow_dispatch' || | |
github.event_name == 'repository_dispatch') && | |
github.ref == 'refs/heads/main' | |
# Don't deploy unless these jobs ran successfully. | |
needs: [test-python, lint-js, check-js-formatting] | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: 3.11 | |
- name: Install Dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
- name: Create .env with production values | |
run: | | |
printf "%s" "${{ secrets.PROD_ENV_VARS }}" > .env | |
- name: Create private key file for GitHub API interactions | |
run: | | |
printf "%s" "${{ secrets.GH_API_PRIVATE_KEY_FILE }}" > portability-map.2023-11-08.private-key.pem | |
- uses: 'google-github-actions/auth@v2' | |
with: | |
project_id: portability-map | |
credentials_json: '${{ secrets.GCP_CREDENTIALS }}' | |
- name: Set up Cloud SDK | |
uses: 'google-github-actions/setup-gcloud@v2' | |
- name: Download and run the Cloud SQL Auth Proxy | |
run: | | |
curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.10.1/cloud-sql-proxy.linux.amd64 | |
chmod +x cloud-sql-proxy | |
./cloud-sql-proxy --address 0.0.0.0 --port 1234 ${{ secrets.CLOUD_SQL_ICN }} & | |
- name: Run migrations | |
env: | |
DJ_DATABASE_CONN_STRING: postgres://${{ secrets.POSTGRES_CREDENTIALS }}@localhost:1234/portmap | |
run: | | |
python3 manage.py migrate | |
- name: Update DB and generate index.html | |
env: | |
DJ_DATABASE_CONN_STRING: postgres://${{ secrets.POSTGRES_CREDENTIALS }}@localhost:1234/portmap | |
run: | | |
python3 manage.py refresh | |
- name: Stop Cloud SQL Auth Proxy | |
run: pkill cloud-sql-proxy | |
- name: Collect static files | |
run: python3 manage.py collectstatic --no-input | |
- name: Deploy to App Engine | |
run: gcloud app deploy --project=portability-map | |