Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recettes jetables: utilisation de git #4769

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/review-app-creation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ jobs:
$CLEVER_CLI env rm CC_PYTHON_VERSION

- name: 🚀 Deploy to Clever Cloud
timeout-minutes: 15
run: |
$CLEVER_CLI link $REVIEW_APP_NAME --org itou_review_apps
$CLEVER_CLI deploy --branch $BRANCH --force
scripts/clever-deploy --clever-cli "$CLEVER_CLI" --branch "$BRANCH" --app-alias "$REVIEW_APP_NAME"

- name: 🍻 Add link to pull request
uses: thollander/actions-comment-pull-request@v2.5.0
Expand Down Expand Up @@ -121,4 +122,5 @@ jobs:
$CLEVER_CLI env set SKIP_FIXTURES true

- name: 🚀 Deploy to Clever Cloud
run: $CLEVER_CLI deploy --branch $BRANCH --force
timeout-minutes: 15
run: scripts/clever-deploy --clever-cli "$CLEVER_CLI" --branch "$BRANCH" --app-alias "$REVIEW_APP_NAME"
87 changes: 87 additions & 0 deletions scripts/clever-deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/env python

import argparse
import json
import os
import stat
import subprocess
import sys
import tempfile
import time


def get_deployments(clever_cli, app_alias):
activity_output = subprocess.check_output([clever_cli, "activity", "--format=json", "--alias", app_alias])
return json.loads(activity_output)


def deploy():
parser = argparse.ArgumentParser()
parser.add_argument("--branch", required=True)
parser.add_argument("--app-alias", required=True)
parser.add_argument("--clever-cli", default="clever")

args = parser.parse_args()
branch = args.branch
clever_cli = args.clever_cli
app_alias = args.app_alias

if not os.getenv("CLEVER_TOKEN") or not os.getenv("CLEVER_SECRET"):
sys.exit("Environment variables CLEVER_TOKEN & CLEVER_SECRET are mandatory")

applications_data = subprocess.check_output([clever_cli, "applications", "--json"])
try:
[deploy_url] = [info["deploy_url"] for info in json.loads(applications_data) if info["alias"] == app_alias]
except ValueError:
sys.exit(f"Could not find the deploy url for {app_alias} - check your linked applications")

known_deployment_uids = {d["uuid"] for d in get_deployments(clever_cli, app_alias)}

# TODO(xfernandez: switch to delete_on_close=False in 3.12
with tempfile.NamedTemporaryFile(prefix="clever_git_alias", delete=False) as f:
f.write(b"""#!/bin/bash
case "$1" in
Username*) echo "$CLEVER_TOKEN" ;;
Password*) echo "$CLEVER_SECRET" ;;
esac
""")
f.close()
os.chmod(f.name, stat.S_IRUSR | stat.S_IXUSR)
subprocess.check_call(
["git", "push", deploy_url, f"{branch}:master", "--force", "--progress"],
env={
"CLEVER_TOKEN": os.environ["CLEVER_TOKEN"],
"CLEVER_SECRET": os.environ["CLEVER_SECRET"],
"GIT_ASKPASS": f.name,
},
)
os.remove(f.name)

for _attempt in range(10):
new_deployments = [d for d in get_deployments(clever_cli, app_alias) if d["uuid"] not in known_deployment_uids]
if new_deployments:
break
time.sleep(1)
else:
sys.exit("No new deployment despite successfull push")

if len(new_deployments) != 1:
print(f"Multiple new deployements - all bets are off: {new_deployments}")

deployment_uid = new_deployments[0]["uuid"]
print(f"Following deployment {deployment_uid}")

while True:
deployments = get_deployments(clever_cli, app_alias)
deployment_info = [d for d in deployments if d["uuid"] == deployment_uid][0]
print(deployment_info)
if deployment_info["state"] != "WIP":
break
time.sleep(10)

if deployment_info["state"] != "OK":
sys.exit(f"Something went wrong in deployment {deployment_uid}")


if __name__ == "__main__":
deploy()
2 changes: 1 addition & 1 deletion scripts/create-fast-machine.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ clever service link-addon c1-redis --alias "$APP_NAME"
clever service link-addon c1-s3 --alias "$APP_NAME"

git fetch
clever deploy --alias "$APP_NAME" --branch origin/master --force
./scripts/clever-deploy --app-alias "$APP_NAME" --branch origin/master

cat << EOF

Expand Down
Loading