Can Azure functions clone Git repo to a Blob storage?

Viewed 1782

I've a business requirement of cloning Github repository on every commit to trigger a workflow starting with a logic app first and then triggering an azure function which can clone Github repository to my azure storage blob. I've never seen a use case like this over the internet, someone has any suggestions of what can be done?

1 Answers

If you want to clone Github repository to my azure storage blob, we can use the python sdk PyGithub to download files from repo then we can upload these files to Azure blob.

For example (I use Azure HTTP trigger function)

  1. Install package
pip install PyGithub
pip install aiohttp
pip install azure-storage-blob
  1. Code
import azure.functions as func
from github import Github
from azure.storage.blob.aio import BlobServiceClient
async def upload(data,container_name,blob_name):
        connection_string='<your storage account connection string>'
        blob_service_client = BlobServiceClient.from_connection_string(connection_string)
        async with blob_service_client:
            container_client = blob_service_client.get_container_client(container_name)
            try:
                # Create new Container in the Service
                await container_client.create_container()
            except  Exception as ex:
                logging.info("the container has existed")

            # Get a new BlobClient
            blob_client = container_client.get_blob_client(blob_name)
            await blob_client.upload_blob(data, blob_type="BlockBlob")


async def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    g=Github('<your github usename>','<your github password>')
    repo = g.get_user().get_repo('<your repo path>')
    contents = repo.get_contents(path='',ref='master')
    while contents:
      file_content = contents.pop(0)
      if file_content.type == "dir":
          contents.extend(repo.get_contents(file_content.path))
      else:
          logging.info(file_content.path)
          await upload(file_content.decoded_content,container_name=repo.name,blob_name=file_content.path)

    return func.HttpResponse("OK")
Related