CD - Deploy to onto-ns.com #115
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: CD - Deploy to onto-ns.com | |
on: | |
workflow_run: | |
workflows: ["CI - Tests"] | |
types: [completed] | |
branches: [main] | |
workflow_dispatch: | |
jobs: | |
check-service-changes: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }} | |
outputs: | |
service-changes: ${{ steps.check-changes.outputs.service-changes || steps.passthrough.outputs.service-changes }} | |
permissions: | |
actions: read | |
steps: | |
- name: Passthrough if manually deploying | |
if: github.event_name == 'workflow_dispatch' | |
id: passthrough | |
run: | | |
echo "service-changes=true" >> $GITHUB_OUTPUT | |
- name: Checkout repository | |
if: github.event_name != 'workflow_dispatch' | |
uses: actions/checkout@v4 | |
- name: Set up Python 3.10 | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.10" | |
- name: Download deployment artifact | |
if: github.event_name != 'workflow_dispatch' | |
uses: actions/download-artifact@v4 | |
with: | |
name: onto-ns-deployment | |
run-id: ${{ github.event.workflow_run.id }} | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
repository: ${{ github.repository }} | |
- name: Check changes | |
if: github.event_name != 'workflow_dispatch' | |
id: check-changes | |
run: | | |
git fetch --all --quiet | |
before_sha="$(python -c "import json, pathlib; print(json.load(pathlib.Path('onto-ns-deployment.json').read_text())['before'])")" | |
git diff --name-only ${before_sha} -- | grep -qE "(pyproject.toml|entities_service/((models|service)/(.+/)*)?[^/]*\.py)" && SERVICE_CHANGES=true || SERVICE_CHANGES=false | |
echo "Changed files:" | |
git diff --name-only ${before_sha} -- | |
echo "SERVICE_CHANGES=${SERVICE_CHANGES}" | |
echo "service-changes=${SERVICE_CHANGES}" >> $GITHUB_OUTPUT | |
deploy: | |
runs-on: ubuntu-latest | |
needs: check-service-changes | |
if: ${{ needs.check-service-changes.outputs.service-changes == 'true' }} | |
steps: | |
- name: Set up Python 3.10 | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.10" | |
- name: Install dependencies | |
run: sudo apt update && sudo apt install -y curl | |
- name: Deploy updated main | |
run: | | |
curl -o response.json --silent -m 30 http://api.onto-ns.com/deploy/?service=entities-service | |
cat << EOF | python | |
import json | |
import sys | |
from pathlib import Path | |
response = json.loads(Path('response.json').read_bytes()) | |
if response["service"] != "entities-service": | |
sys.exit( | |
"ERROR: Did not deploy the correct service\n" | |
f" service: {response['service']}\n" | |
f" code: {response['returncode']}\n" | |
f" stderr: {response['stderr']}\n\n" | |
f" stdout: {response['stdout']}" | |
) | |
if response["returncode"] != 0: | |
sys.exit( | |
"ERROR: Non-zero return code\n" | |
f" code: {response['returncode']}\n" | |
f" stderr:\n{response['stderr']}\n\n" | |
f" stdout:\n{response['stdout']}" | |
) | |
if response["stderr"]: | |
print( | |
"WARNING: stderr is not empty\n" | |
f" stderr:\n{response['stderr']}\n\n" | |
) | |
print( | |
"SUCCESS!\n" | |
f" code: {response['returncode']}\n" | |
f" stdout:\n{response['stdout']}" | |
) | |
EOF |