diff --git a/.github/auth/vm_auth.py b/.github/auth/vm_auth.py new file mode 100644 index 00000000..60d30197 --- /dev/null +++ b/.github/auth/vm_auth.py @@ -0,0 +1,41 @@ +import sys +import paramiko +from google.auth import compute_engine +from googleapiclient import discovery +from google.oauth2.service_account import Credentials + +def authenticate_vm(creds): + credentials = Credentials.from_service_account_info(creds) + return discovery.build('compute', 'v1', credentials=credentials) +def start_runner(document, pkey, id = "gpu-insatnce", zone='us-central1-a', instance='demos-tests'): + compute = authenticate_vm(document) + compute.instances().start(project=id, zone=zone, instance=instance).execute() + request = compute.instances().get(project=id, zone=zone, instance=instance) + response = request.execute() + + # Extract the external IP address of the instance + external_ip = response['networkInterfaces'][0]['accessConfigs'][0]['natIP'] + + # Establish an SSH connection to the instance + ssh = paramiko.SSHClient() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + ssh.connect(external_ip, pkey = key) + + # Execute the command on the instance + stdin, stdout, stderr = ssh.exec_command('cd actions-runner; nohup ./run.sh') + + # Read the output of the command + output = stdout.read().decode() + + # Close the SSH connection + ssh.close() + + return output + +if __name__ == "__main__": + creds, key = sys.argv[0], sys.argv[1] + # Start the instance + start_runner(creds, key) + + + diff --git a/.github/workflows/gcp-test.yaml b/.github/workflows/gcp-test.yaml index 59821ee2..9855da9f 100644 --- a/.github/workflows/gcp-test.yaml +++ b/.github/workflows/gcp-test.yaml @@ -12,22 +12,21 @@ jobs: activate-vm: runs-on: ubuntu-latest steps: - - id: 'auth' - uses: 'google-github-actions/auth@v0.4.0' + - name: Checkout Demos🛎 + uses: actions/checkout@v2 with: - credentials_json: '${{ secrets.GCP_AUTH }}' + path: demos + persist-credentials: false + submodules: "recursive" + fetch-depth: 1 - - name: 'Set up Cloud SDK' - uses: 'google-github-actions/setup-gcloud@v1' - with: - version: '>= 363.0.0' - - - name: 'Start VM instance' - run: 'gcloud compute instances start demos-tests --zone us-central1-a' + - name: Install clients + runs: + pip install google-api-python-client paramiko - - name: 'SSH into the instance' - run: | - gcloud compute ssh demos-tests --quiet --zone us-central1-a --ssh-key-file ${{ secrets.SSH_KEY }} --command "cd actions-runner; ./run.sh" + - name: Start GPU VM + runs: | + python3 .github/auth/vm_auth.py ${{ secrets.GCP_AUTH }} ${{ secrets.SSH_KEY }} run-test: needs: activate-vm