Skip to content

Commit

Permalink
gcp auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarsh2001 committed Oct 4, 2023
1 parent 286f474 commit c36b364
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
41 changes: 41 additions & 0 deletions .github/auth/vm_auth.py
Original file line number Diff line number Diff line change
@@ -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)



25 changes: 12 additions & 13 deletions .github/workflows/gcp-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ jobs:
activate-vm:
runs-on: ubuntu-latest
steps:
- id: 'auth'
uses: 'google-github-actions/[email protected]'
- 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
Expand Down

0 comments on commit c36b364

Please sign in to comment.