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

Start/stop integration test server on-demand #65

Merged
merged 29 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
22daa21
Check the server is healthy before testing
Andy-Grigg Jan 18, 2022
3eed227
Install requests first
Andy-Grigg Jan 18, 2022
52df274
Merge branch 'main' into cicd/start-stop-vm
Andy-Grigg Feb 2, 2022
5fbfcf1
Try logging into azure
Andy-Grigg Feb 3, 2022
1269252
Start VM
Andy-Grigg Feb 3, 2022
001431b
Set account name
Andy-Grigg Feb 3, 2022
8f5d607
Change arg name, remove 'needs' to speed up debugging
Andy-Grigg Feb 3, 2022
0969730
Bump az cli version
Andy-Grigg Feb 3, 2022
f298191
Re-org steps
Andy-Grigg Feb 3, 2022
09c085d
Get the code to check the machine state
Andy-Grigg Feb 3, 2022
466e696
Stop VM once docs are built
Andy-Grigg Feb 3, 2022
d507f28
Try a different way of logging in
Andy-Grigg Feb 3, 2022
b5c5503
Fix yaml
Andy-Grigg Feb 3, 2022
7c2a3da
Don't start the VM if we're doing a dependency check only
Andy-Grigg Feb 3, 2022
faa340a
Actually deallocate the VM
Andy-Grigg Feb 3, 2022
5373137
Change to trigger new action instance
Andy-Grigg Feb 3, 2022
2eaa86a
Try and make VM stop conditional
Andy-Grigg Feb 3, 2022
dcfcfe6
Trivial commit to trigger cicd
Andy-Grigg Feb 3, 2022
4d4fc39
Remove braces
Andy-Grigg Feb 3, 2022
a4a4e8f
Use a different method to determine whether to shut down the VM
Andy-Grigg Feb 3, 2022
eb24117
Remove unneeded checkout
Andy-Grigg Feb 3, 2022
b93be2e
Trigger another action run
Andy-Grigg Feb 3, 2022
602b5d5
Revert change
Andy-Grigg Feb 3, 2022
ad34b43
Tidy up yaml
Andy-Grigg Feb 3, 2022
97aabd4
Log response status code from server / gateway
Andy-Grigg Feb 4, 2022
9114c28
Make the ci/cd python logging a bit clearer
Andy-Grigg Feb 4, 2022
1c94072
Fix attempt numbering
Andy-Grigg Feb 4, 2022
35116fd
Clean up logs
Andy-Grigg Feb 4, 2022
c6cda0a
Fix logging again
Andy-Grigg Feb 4, 2022
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
65 changes: 55 additions & 10 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
name: Pre-merge checks

# run only on main branch. This avoids duplicated actions on PRs
# Run only on main branch. This avoids duplicated actions on PRs
on:
pull_request:
push:
tags:
- "*"
- '*'
branches:
- main
workflow_dispatch:

jobs:
start_vm:
name: Start Azure VM
runs-on: ubuntu-latest
if: ${{ !contains( github.event.pull_request.labels.*.name, 'dependencies') }}

steps:
- uses: actions/checkout@v2

- name: Start VM with Azure CLI
id: azure_cli
uses: azure/CLI@v1
with:
azcliversion: 2.32.0
inlineScript: |
az login --service-principal -u ${{ secrets.AZURE_APP_ID }} -p ${{ secrets.AZURE_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }}
az vm start -g ${{ secrets.AZURE_RESOURCE_GROUP }} -n ${{ secrets.AZURE_VM_NAME }}

- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: '3.10'

- name: Check server is ready
run: |
pip install requests
python cicd/check_server.py
env:
TEST_SL_URL: ${{secrets.TEST_SERVER_URL}}
TEST_USER: ${{secrets.TEST_SERVER_READ_USER}}
TEST_PASS: ${{secrets.TEST_SERVER_READ_PASS}}

style:
name: Style check
runs-on: ubuntu-latest
Expand All @@ -21,7 +52,7 @@ jobs:
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: "3.10"
python-version: '3.10'

- name: Create local deps folder
run: mkdir -p deps
Expand Down Expand Up @@ -121,8 +152,7 @@ jobs:
integration_tests:
name: Integration tests
runs-on: ${{ matrix.os }}
needs: [style, unit_tests]
if: ${{ !contains( github.event.pull_request.labels.*.name, 'dependencies') }}
needs: [style, unit_tests, start_vm]

strategy:
max-parallel: 1
Expand All @@ -136,10 +166,10 @@ jobs:
if: ${{ matrix.os == 'ubuntu-latest' }}
run: sudo apt install libkrb5-dev

- name: Set up Python 3.8
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: '3.8'
python-version: '3.10'

- name: Create local deps folder
run: mkdir -p deps
Expand Down Expand Up @@ -188,7 +218,7 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Setup Python
- name: Setup Python 3.10
uses: actions/setup-python@v2
with:
python-version: '3.10'
Expand Down Expand Up @@ -228,7 +258,7 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Setup Python
- name: Setup Python 3.10
uses: actions/setup-python@v2
with:
python-version: '3.10'
Expand Down Expand Up @@ -303,12 +333,27 @@ jobs:
path: doc/build/latex/*.pdf
retention-days: 7

stop_vm:
name: Stop Azure VM
runs-on: ubuntu-latest
needs: [docs, start_vm]
if: always() && needs.start_vm.result != 'skipped'

steps:
- name: Stop Azure VM
uses: azure/CLI@v1
with:
azcliversion: 2.32.0
inlineScript: |
az login --service-principal -u ${{ secrets.AZURE_APP_ID }} -p ${{ secrets.AZURE_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }}
az vm deallocate -g ${{ secrets.AZURE_RESOURCE_GROUP }} -n ${{ secrets.AZURE_VM_NAME }} --no-wait

Release:
if: contains(github.ref, 'refs/tags') && github.event_name == 'push'
needs: [package, integration_tests, docs]
runs-on: ubuntu-latest
steps:
- name: Set up Python
- name: Set up Python 3.10
uses: actions/setup-python@v1
with:
python-version: '3.10'
Expand Down
56 changes: 56 additions & 0 deletions cicd/check_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import requests
from requests.auth import HTTPBasicAuth
import os
import json
import time
from functools import wraps
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()

MAX_ATTEMPTS = 30
WAIT_TIME = 5


def block_until_server_is_ok(func):
@wraps(func)
def wrapper(*args, **kwargs):
check_count = 1
while True:
logger.info(f"Check {check_count}")
if func(*args, **kwargs):
logger.info(f"Server ready!")
break
check_count = check_count + 1
if check_count == MAX_ATTEMPTS:
logger.info(f"Failed after 30 attempts. Quitting...")
raise ConnectionError
logger.info(f"Check {check_count} failed. Waiting 5 seconds...")
time.sleep(WAIT_TIME)
return wrapper


@block_until_server_is_ok
def check_status(url: str, auth_header: HTTPBasicAuth) -> bool:
try:
response = requests.get(url + "/Health/v2.svc/", auth=auth_header)
except requests.exceptions.RequestException:
return False
if response.status_code != 200:
Andy-Grigg marked this conversation as resolved.
Show resolved Hide resolved
return False
content = json.loads(response.content)
for check in content['HealthChecks']:
if check['Name'] == 'Database Check' and check['Status'] == 'Ok':
return True
return False


if __name__ == "__main__":
sl_url = os.getenv("TEST_SL_URL")
username = os.getenv("TEST_USER")
password = os.getenv("TEST_PASS")

logger.info(f"Checking if Granta MI server is ready for requests")
auth = HTTPBasicAuth(username, password)
check_status(sl_url, auth)