Skip to content

Commit

Permalink
Configurer les review apps (#114)
Browse files Browse the repository at this point in the history
* Add review app Github actions

* Delete staging deploy Github action

* Additional config to review apps urls

* Add review app after success init script

* Add script to load fixtures
  • Loading branch information
raphodn committed Oct 27, 2021
1 parent 394a814 commit 4469d16
Show file tree
Hide file tree
Showing 13 changed files with 104,900 additions and 2 deletions.
121 changes: 121 additions & 0 deletions .github/workflows/review-app-creation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# https://github.com/betagouv/itou/blob/master/.github/workflows/review-app-creation.yml
# See https://developer.github.com/v3/
# and https://help.github.com/en/actions
name: 🕵 Review app

# Run this pipeline when a label is added and when a push is made on this PR.
# `types: [ synchronize ]` targets a push event made on a PR.
on:
pull_request:
types: [ labeled, synchronize ]

env:
CLEVER_TOOLS_DOWNLOAD_URL: https://clever-tools.clever-cloud.com/releases/latest/clever-tools-latest_linux.tar.gz
CLEVER_TAR_FILE: clever-tools-latest_linux.tar.gz
CLEVER_CLI: clever-tools-latest_linux/clever
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }}
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }}
REVIEW_APPS_ORGANIZATION_NAME: ${{ secrets.CLEVER_REVIEW_APPS_ORG }}
CONFIGURATION_ADDON: ${{ secrets.CLEVER_REVIEW_APPS_CONFIGURATION_ADDON }}
# S3_ADDON: ${{ secrets.CLEVER_REVIEW_APPS_S3_ADDON }}
BRANCH: ${{ github.head_ref }}

jobs:
create:
runs-on: ubuntu-latest
if: github.event.action == 'labeled' && github.event.label.name == 'recette-jetable'

steps:
- name: 📥 Checkout to the PR branch
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: 📥 Fetch git branches
run: git fetch --prune --unshallow

# Environment variables
- name: 🏷 Set review app name
run:
echo "REVIEW_APP_NAME=`echo \"c4-review-$BRANCH\" | sed -r 's/[-;\\/._]+/-/g'`" >> $GITHUB_ENV

- name: 🏷 Set database addon name
run:
echo "REVIEW_APP_DB_NAME=`echo $REVIEW_APP_NAME | sed -r 's/-/_/g'`" >> $GITHUB_ENV

- name: 🏷 Set deploy url
run:
echo "DEPLOY_URL=`echo \"$REVIEW_APP_NAME.cleverapps.io\"`" >> $GITHUB_ENV
# End of environment variables

- name: 🧫 Create a review app on Clever Cloud
run: |
curl $CLEVER_TOOLS_DOWNLOAD_URL > $CLEVER_TAR_FILE
tar -xvf $CLEVER_TAR_FILE
$CLEVER_CLI login --token $CLEVER_TOKEN --secret $CLEVER_SECRET
# Create a new application on Clever Cloud.
# -t: application type (Python).
# --org: organization name.
# --region: server location ("par" means Paris).
# --alias: custom application name, used to find it with the CLI.
$CLEVER_CLI create $REVIEW_APP_NAME -t python --org $REVIEW_APPS_ORGANIZATION_NAME --region par --alias $REVIEW_APP_NAME
$CLEVER_CLI domain add $DEPLOY_URL --alias $REVIEW_APP_NAME
$CLEVER_CLI link $REVIEW_APP_NAME --org $REVIEW_APPS_ORGANIZATION_NAME
- name: 🗃 Create database addon
run: |
$CLEVER_CLI addon create postgresql-addon $REVIEW_APP_DB_NAME --org $REVIEW_APPS_ORGANIZATION_NAME --plan xxs_sml --yes
$CLEVER_CLI service link-addon $REVIEW_APP_DB_NAME
# - name: 🤝 Link S3 addon
# run: |
# $CLEVER_CLI link $REVIEW_APP_NAME --org $REVIEW_APPS_ORGANIZATION_NAME
# $CLEVER_CLI service link-addon $S3_ADDON

- name: 🗺 Add environment variables to the review app
run: |
$CLEVER_CLI link $REVIEW_APP_NAME --org $REVIEW_APPS_ORGANIZATION_NAME
$CLEVER_CLI service link-addon $CONFIGURATION_ADDON
$CLEVER_CLI env import-vars REVIEW_APP_DB_NAME
$CLEVER_CLI env import-vars DEPLOY_URL
- name: 🚀 Deploy to Clever Cloud
run: $CLEVER_CLI deploy --branch $BRANCH --force

- name: 🍻 Add link to pull request
uses: thollander/actions-comment-pull-request@main
with:
message: "🥁 La recette jetable est prête ! [👉 Je veux tester cette PR !](https://${{ env.DEPLOY_URL }})"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

redeploy:
runs-on: ubuntu-latest
# A push event targets a new deployment.
if: github.event.action == 'synchronize' && contains( github.event.pull_request.labels.*.name, 'recette-jetable')

steps:
- name: 📥 Checkout to the PR branch
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: 📥 Fetch git branches
run: git fetch --prune --unshallow

- name: 🏷 Set review app name
run:
echo "REVIEW_APP_NAME=`echo \"c4-review-$BRANCH\" | sed -r 's/[-;\\/._]+/-/g'`" >> $GITHUB_ENV

- name: 🤝 Find the application on Clever Cloud
run: |
curl $CLEVER_TOOLS_DOWNLOAD_URL > $CLEVER_TAR_FILE
tar -xvf $CLEVER_TAR_FILE
$CLEVER_CLI login --token $CLEVER_TOKEN --secret $CLEVER_SECRET
$CLEVER_CLI link $REVIEW_APP_NAME --org $REVIEW_APPS_ORGANIZATION_NAME
- name: ⏭ Skip fixtures
run:
$CLEVER_CLI env set SKIP_FIXTURES true

- name: 🚀 Deploy to Clever Cloud
run: $CLEVER_CLI deploy --branch $BRANCH --force
48 changes: 48 additions & 0 deletions .github/workflows/review-app-removal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# https://github.com/betagouv/itou/blob/master/.github/workflows/review-app-removal.yml
name: 🔪 Review app removal

# Run this pipeline when a pull request having the label "review-app" is closed.
on:
pull_request:
types: [ unlabeled, closed ]

env:
CLEVER_TOOLS_DOWNLOAD_URL: https://clever-tools.clever-cloud.com/releases/latest/clever-tools-latest_linux.tar.gz
CLEVER_TAR_FILE: clever-tools-latest_linux.tar.gz
CLEVER_CLI: clever-tools-latest_linux/clever
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }}
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }}
REVIEW_APPS_ORGANIZATION_NAME: ${{ secrets.CLEVER_REVIEW_APPS_ORG }}
BRANCH: ${{ github.head_ref }}

jobs:
delete:
runs-on: ubuntu-latest
if: github.event.label.name == 'recette-jetable' || contains( github.event.pull_request.labels.*.name, 'recette-jetable')

steps:
- name: 📥 Checkout to the PR branch
uses: actions/checkout@v2

- name: 🏷 Set review app name
run:
echo "REVIEW_APP_NAME=`echo \"c4-review-$BRANCH\" | sed -r 's/[-;\\/._]+/-/g'`" >> $GITHUB_ENV

- name: 🏷 Set database addon name
run:
echo "REVIEW_APP_DB_NAME=`echo $REVIEW_APP_NAME | sed -r 's/-/_/g'`" >> $GITHUB_ENV

- name: 🤝 Find the application on Clever Cloud
run: |
curl $CLEVER_TOOLS_DOWNLOAD_URL > $CLEVER_TAR_FILE
tar -xvf $CLEVER_TAR_FILE
$CLEVER_CLI login --token $CLEVER_TOKEN --secret $CLEVER_SECRET
$CLEVER_CLI link $REVIEW_APP_NAME --org $REVIEW_APPS_ORGANIZATION_NAME
- name: 🗑 Delete the review app
run: |
$CLEVER_CLI delete --yes
- name: 🗑 Delete the review app database
run: |
$CLEVER_CLI addon delete $REVIEW_APP_DB_NAME --org $REVIEW_APPS_ORGANIZATION_NAME --yes
23 changes: 23 additions & 0 deletions clevercloud/review-app-after-success.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

# https://github.com/betagouv/itou/blob/master/clevercloud/review-app-after-success.sh

###################################################################
###################### Review apps entrypoint #####################
###################################################################

# Skip this step when redeploying a review app.
if [ "$SKIP_FIXTURES" = true ] ; then
echo "Skipping fixtures."
exit
fi

echo "Loading perimeters"
django-admin import_regions
django-admin import_departements
# django-admin import_communes

# `ls $APP_HOME` does not work as the current user
# does not have execution rights on the $APP_HOME directory.
echo "Loading fixtures"
ls -d $APP_HOME/lemarche/fixtures/django/* | xargs django-admin loaddata
4 changes: 4 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@

WSGI_APPLICATION = "config.wsgi.application"

# for review apps
# if none, will use Site.objects.get_current().domain instead
DEPLOY_URL = env.str("DEPLOY_URL", None)


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
Expand Down
2 changes: 2 additions & 0 deletions config/settings/staging.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"staging.inclusion.beta.gouv.fr",
"staging.lemarche.inclusion.beta.gouv.fr",
"bitoubi-django-staging.cleverapps.io",
# for review apps
".cleverapps.io",
]


Expand Down
Loading

0 comments on commit 4469d16

Please sign in to comment.