Deploy your Youtube downloader on Cloudrun

Here is a beginner-friendly step-by-step tutorial to create and deploy a YouTube video downloader service on Google Cloud Run using FastAPI. The tutorial assumes basic Python knowledge but no prior production deployment experience. Each step is explained in detail to help you understand what you're doing and why it's important. Step 1: Set Up a Python Project with Pyenv and Virtualenv What is Pyenv? Pyenv is a tool to manage multiple Python versions on your system. It ensures your project uses the right Python version without interfering with global settings. What is Virtualenv? Virtualenv creates isolated environments for Python projects. This ensures your project dependencies don't conflict with others. Instructions: Install Pyenv: curl https://pyenv.run | bash Add the following to your shell configuration file (~/.bashrc, ~/.zshrc, etc.): export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init --path)" eval "$(pyenv virtualenv-init -)" Restart your terminal or run: source ~/.bashrc # or ~/.zshrc Install a Python Version: pyenv install 3.10.12 pyenv global 3.10.12 Set Up Virtualenv: python -m venv venv source venv/bin/activate Create a Project Directory: mkdir youtube-downloader cd youtube-downloader Create a requirements.txt File: Add the following: fastapi uvicorn Install the dependencies: pip install -r requirements.txt Create a Hello World API: File: app.py from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"message": "Hello, World!"} Run the app locally: uvicorn app:app --reload Step 2: Create a Docker Image and Run Locally What is Docker? Docker allows you to package your application and its dependencies into a container that can run consistently anywhere. Instructions: Create a Dockerfile: FROM python:3.10-slim WORKDIR /app COPY requirements.txt requirements.txt RUN pip install -r requirements.txt COPY . . CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"] Build and Run the Docker Image: Build: docker build -t youtube-downloader . Run: docker run -p 8080:8080 youtube-downloader Access the App: Open your browser and go to http://localhost:8080. Step 3: Push the Docker Image to Google Container Registry (GCR) Why Use GCR? Google Container Registry stores your Docker images securely in Google Cloud, making them accessible for deployment. Instructions: Set Up GCP Authentication: Install the Google Cloud CLI: curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli--linux-x86_64.tar.gz tar -xvf google-cloud-cli--linux-x86_64.tar.gz ./google-cloud-sdk/install.sh gcloud auth login Tag and Push the Image: Tag the image: docker tag youtube-downloader gcr.io//youtube-downloader Push the image: docker push gcr.io//youtube-downloader Step 4: Deploy Using GitHub Actions Why GitHub Actions? GitHub Actions automates your CI/CD process, making deployment seamless. Instructions: Create a GitHub Repository and Push Code: git init git add . git commit -m "Initial commit" git branch -M main git remote add origin git push -u origin main Create a .github/workflows/deploy.yml File: name: Deploy to Cloud Run on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up Google Cloud CLI uses: google-github-actions/setup-gcloud@v1 with: project_id: service_account_key: ${{ secrets.GCP_SA_KEY }} - name: Authenticate Docker run: gcloud auth configure-docker - name: Build and Push Docker Image run: | docker build -t gcr.io//youtube-downloader . docker push gcr.io//youtube-downloader - name: Deploy to Cloud Run run: | gcloud run deploy youtube-downloader \ --image gcr.io//youtube-downloader \ --platform managed \ --region us-central1 \ --allow-unauthenticated Add a Secret for GCP Service Account Key: In your GitHub repository settings, add GCP_SA_KEY as a secret containing your GCP service account key. Step 5: Deploy Using Command Line Instructions: Deploy to Cloud Run: gcloud run deploy youtube-downloader \ --image gcr.io//youtube-downloader \ --platform managed \ --region us-central1 \ --allow-unauthenticated Update GitHub Actions for Command Line Deployment: Add steps in deploy.yml to notify abo

Jan 19, 2025 - 07:05
Deploy your Youtube downloader on Cloudrun

Here is a beginner-friendly step-by-step tutorial to create and deploy a YouTube video downloader service on Google Cloud Run using FastAPI. The tutorial assumes basic Python knowledge but no prior production deployment experience. Each step is explained in detail to help you understand what you're doing and why it's important.

Step 1: Set Up a Python Project with Pyenv and Virtualenv

What is Pyenv?

Pyenv is a tool to manage multiple Python versions on your system. It ensures your project uses the right Python version without interfering with global settings.

What is Virtualenv?

Virtualenv creates isolated environments for Python projects. This ensures your project dependencies don't conflict with others.

Instructions:

  1. Install Pyenv:
   curl https://pyenv.run | bash

Add the following to your shell configuration file (~/.bashrc, ~/.zshrc, etc.):

   export PATH="$HOME/.pyenv/bin:$PATH"
   eval "$(pyenv init --path)"
   eval "$(pyenv virtualenv-init -)"

Restart your terminal or run:

   source ~/.bashrc  # or ~/.zshrc
  1. Install a Python Version:
   pyenv install 3.10.12
   pyenv global 3.10.12
  1. Set Up Virtualenv:
   python -m venv venv
   source venv/bin/activate
  1. Create a Project Directory:
   mkdir youtube-downloader
   cd youtube-downloader
  1. Create a requirements.txt File: Add the following:
   fastapi
   uvicorn

Install the dependencies:

   pip install -r requirements.txt
  1. Create a Hello World API:
    • File: app.py
   from fastapi import FastAPI

   app = FastAPI()

   @app.get("/")
   def read_root():
       return {"message": "Hello, World!"}
  • Run the app locally:

     uvicorn app:app --reload
    

Step 2: Create a Docker Image and Run Locally

What is Docker?

Docker allows you to package your application and its dependencies into a container that can run consistently anywhere.

Instructions:

  1. Create a Dockerfile:
   FROM python:3.10-slim

   WORKDIR /app

   COPY requirements.txt requirements.txt
   RUN pip install -r requirements.txt

   COPY . .

   CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
  1. Build and Run the Docker Image:

    • Build:
     docker build -t youtube-downloader .
    
  • Run:

     docker run -p 8080:8080 youtube-downloader
    
  1. Access the App: Open your browser and go to http://localhost:8080.

Step 3: Push the Docker Image to Google Container Registry (GCR)

Why Use GCR?

Google Container Registry stores your Docker images securely in Google Cloud, making them accessible for deployment.

Instructions:

  1. Set Up GCP Authentication: Install the Google Cloud CLI:
   curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli--linux-x86_64.tar.gz
   tar -xvf google-cloud-cli--linux-x86_64.tar.gz
   ./google-cloud-sdk/install.sh
   gcloud auth login
  1. Tag and Push the Image:

    • Tag the image:
     docker tag youtube-downloader gcr.io//youtube-downloader
    
  • Push the image:

     docker push gcr.io//youtube-downloader
    

Step 4: Deploy Using GitHub Actions

Why GitHub Actions?

GitHub Actions automates your CI/CD process, making deployment seamless.

Instructions:

  1. Create a GitHub Repository and Push Code:
   git init
   git add .
   git commit -m "Initial commit"
   git branch -M main
   git remote add origin 
   git push -u origin main
  1. Create a .github/workflows/deploy.yml File:
   name: Deploy to Cloud Run

   on:
     push:
       branches:
         - main

   jobs:
     deploy:
       runs-on: ubuntu-latest
       steps:
       - name: Checkout code
         uses: actions/checkout@v3

       - name: Set up Google Cloud CLI
         uses: google-github-actions/setup-gcloud@v1
         with:
           project_id: 
           service_account_key: ${{ secrets.GCP_SA_KEY }}

       - name: Authenticate Docker
         run: gcloud auth configure-docker

       - name: Build and Push Docker Image
         run: |
           docker build -t gcr.io//youtube-downloader .
           docker push gcr.io//youtube-downloader

       - name: Deploy to Cloud Run
         run: |
           gcloud run deploy youtube-downloader \
             --image gcr.io//youtube-downloader \
             --platform managed \
             --region us-central1 \
             --allow-unauthenticated
  1. Add a Secret for GCP Service Account Key: In your GitHub repository settings, add GCP_SA_KEY as a secret containing your GCP service account key.

Step 5: Deploy Using Command Line

Instructions:

  1. Deploy to Cloud Run:
   gcloud run deploy youtube-downloader \
     --image gcr.io//youtube-downloader \
     --platform managed \
     --region us-central1 \
     --allow-unauthenticated
  1. Update GitHub Actions for Command Line Deployment: Add steps in deploy.yml to notify about the deployment process.

Step 6: Add YouTube Video Metadata and Download Endpoints

Instructions:

  1. Install pytube:
   pip install pytube
  1. Update app.py:
   from fastapi import FastAPI
   from pytube import YouTube

   app = FastAPI()

   @app.get("/metadata")
   def get_metadata(video_url: str):
       yt = YouTube(video_url)
       return {
           "title": yt.title,
           "description": yt.description,
           "duration": yt.length,
           "thumbnail": yt.thumbnail_url
       }

   @app.get("/download")
   def download_video(video_url: str, quality: str = "high"):
       yt = YouTube(video_url)
       stream = yt.streams.filter(progressive=True, file_extension="mp4").order_by("resolution")
       if quality == "low":
           video = stream.first()
       elif quality == "medium":
           video = stream[len(stream) // 2]
       else:
           video = stream.last()

       file_path = video.download(output_path="/tmp")
       return {"file_path": file_path}

Conclusion

Now you've created a FastAPI-based YouTube downloader deployed on Cloud Run! This service includes endpoints for retrieving metadata and downloading videos with quality options. Each step builds on the previous one, ensuring a smooth learning process for beginners.