<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vaibhav Pawar</title>
    <description>The latest articles on DEV Community by Vaibhav Pawar (@vaibhav_pawar_3a8be338ee8).</description>
    <link>https://dev.to/vaibhav_pawar_3a8be338ee8</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2637914%2F0c2bc391-1199-4f44-acca-aa105fe75cf3.jpeg</url>
      <title>DEV Community: Vaibhav Pawar</title>
      <link>https://dev.to/vaibhav_pawar_3a8be338ee8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vaibhav_pawar_3a8be338ee8"/>
    <language>en</language>
    <item>
      <title>Running Playwright Tests in Python with Flask on Cloud Run</title>
      <dc:creator>Vaibhav Pawar</dc:creator>
      <pubDate>Sat, 04 Jan 2025 09:29:28 +0000</pubDate>
      <link>https://dev.to/vaibhav_pawar_3a8be338ee8/running-playwright-tests-in-python-with-flask-on-cloud-run-4932</link>
      <guid>https://dev.to/vaibhav_pawar_3a8be338ee8/running-playwright-tests-in-python-with-flask-on-cloud-run-4932</guid>
      <description>&lt;p&gt;Automating browser tasks is a breeze with Playwright, and integrating it into a cloud-based service can take your automation capabilities to the next level. In this blog post, we’ll walk you through setting up a Python-based application that uses Flask and Playwright, deploy it to Google Cloud Run, and enable running browser tests through a simple API.&lt;/p&gt;

&lt;p&gt;Step 1: Setting Up the Application&lt;/p&gt;

&lt;p&gt;We’ll build a Flask app that exposes a POST API to accept a test URL and execute a Playwright test on that URL. To simplify the setup, we’ll keep everything in a single file:&lt;br&gt;
Flask App with Playwright Integration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, request, jsonify
from playwright.sync_api import sync_playwright
app = Flask(__name__)
# Playwright Test Function
def run_test(test_url):
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page()
        page.goto(test_url)
        title = page.title()
        browser.close()
    return f"Test completed for {test_url}: {title}"

# Flask Route
@app.route("/", methods=["POST"])
def execute_test():
    # Get JSON payload
    data = request.json
    if not data or "test_url" not in data:
        return jsonify({"error": "Missing 'test_url' in request body"}), 400
    test_url = data["test_url"]
    try:
        result = run_test(test_url)
        return jsonify({"message": result}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key Features&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Route Definition: The / route listens for POST requests and expects a JSON payload containing test_url.&lt;/li&gt;
&lt;li&gt;Error Handling: Proper validation ensures meaningful error messages for missing or invalid input.&lt;/li&gt;
&lt;li&gt;Playwright Test: The run_test function launches a browser, navigates to the provided URL, fetches the page title, and returns the result.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step 2: Dockerizing the Application&lt;/p&gt;

&lt;p&gt;To deploy the application on Cloud Run, we’ll containerize it using Docker. Create a Dockerfile in the same directory as the app.py file.&lt;br&gt;
Dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM python:3.10-slim
# Install Playwright and Flask
RUN apt-get update &amp;amp;&amp;amp; apt-get install -y wget curl gnupg libatk-bridge2.0-0 libnss3 libnspr4 &amp;amp;&amp;amp; rm -rf /var/lib/apt/lists/*
RUN pip install flask playwright
# Install browser binaries for Playwright
RUN python -m playwright install
# Copy application code
WORKDIR /app
COPY app.py .
# Expose port 8080
EXPOSE 8080
# Run the application
CMD ["python", "app.py"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3: Deploying to Google Cloud Run&lt;/p&gt;

&lt;p&gt;Once your application is containerized, you can deploy it to Google Cloud Run. Follow these steps:&lt;/p&gt;

&lt;p&gt;Step 3.1: Build and Push Docker Image&lt;br&gt;
Build the Docker image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t gcr.io/&amp;lt;YOUR_PROJECT_ID&amp;gt;/playwright-app .
Push the image to Google Container Registry (GCR):
docker push gcr.io/&amp;lt;YOUR_PROJECT_ID&amp;gt;/playwright-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3.2: Deploy to Cloud Run&lt;br&gt;
Deploy the service to Cloud Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
gcloud run deploy playwright-app \ --image=gcr.io/&amp;lt;YOUR_PROJECT_ID&amp;gt;/playwright-app \ --platform managed \ --region &amp;lt;YOUR_REGION&amp;gt; \--allow-unauthenticated
Note the URL provided by Cloud Run after successful deployment.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 4: Passing Parameters to the Cloud Run Service&lt;/p&gt;

&lt;p&gt;To test specific scenarios, you can pass parameters to your Cloud Run service using a JSON payload. This can be done via tools like curl, Postman, or any HTTP client.&lt;br&gt;
Example JSON Payload&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "test_url": "https://example.com"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example curl Command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -X POST https://&amp;lt;YOUR_CLOUD_RUN_URL&amp;gt; \
     -H "Content-Type: application/json" \
     -d '{"test_url": "https://example.com"}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected Response&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "message": "Test completed for https://example.com: Example Domain"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 5: Testing the Application&lt;/p&gt;

&lt;p&gt;You can test your Cloud Run service using tools like curl or Postman, as shown above. Modify the test_url parameter in the payload to run tests on different URLs.&lt;/p&gt;

&lt;p&gt;Troubleshooting&lt;br&gt;
Common Issues&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Playwright Dependencies: Ensure all required dependencies are installed. &lt;/li&gt;
&lt;li&gt;The Dockerfile includes the necessary packages.&lt;/li&gt;
&lt;li&gt;Port Configuration: Cloud Run expects the application to listen on port 8080.&lt;/li&gt;
&lt;li&gt;Permission Errors: Ensure the Docker image is accessible by Cloud Run.
Debugging Logs&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use the following command to view Cloud Run logs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud run services logs read playwright-app --region &amp;lt;YOUR_REGION&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conclusion&lt;br&gt;
In this blog, we’ve shown how to build a Python-based API that leverages Playwright for browser automation, deploy it to Google Cloud Run, and pass parameters to execute specific tests. This setup is ideal for scenarios like website testing, data scraping, or automating repetitive browser tasks. With Cloud Run’s scalability, your service is ready to handle increased demand effortlessly.&lt;br&gt;
Try it out and unlock the power of automated browser testing in the cloud!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Automating Google Cloude Platform Instance Updates with GitHub Actions</title>
      <dc:creator>Vaibhav Pawar</dc:creator>
      <pubDate>Tue, 31 Dec 2024 14:06:52 +0000</pubDate>
      <link>https://dev.to/vaibhav_pawar_3a8be338ee8/automating-gcp-instance-updates-with-github-actions-bll</link>
      <guid>https://dev.to/vaibhav_pawar_3a8be338ee8/automating-gcp-instance-updates-with-github-actions-bll</guid>
      <description>&lt;p&gt;In this guide, we’ll walk through automating the deployment of updates to a Google Cloud Platform (GCP) instance using GitHub Actions. This workflow simplifies the process of authenticating with GCP, updating the codebase, and restarting the application inside a Docker container.&lt;/p&gt;

&lt;p&gt;Objective&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The goal is to create a seamless deployment pipeline for a web application running on a GCP Compute Engine instance. This workflow:&lt;/li&gt;
&lt;li&gt;Authenticates with GCP using a service account key.&lt;/li&gt;
&lt;li&gt;Configures the gcloud CLI for the target project and zone.&lt;/li&gt;
&lt;li&gt;Pulls the latest changes from the code repository.&lt;/li&gt;
&lt;li&gt;Builds and deploys the application using Docker.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;GitHub Actions Workflow Configuration&lt;br&gt;
Below is the GitHub Actions workflow file for the pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: Authenticate with GCP and Update Instance
on:
  push:
    branches:
      - main
jobs:
  gcloud-auth:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
    - name: Set up Google Cloud Authentication
      uses: 'google-github-actions/auth@v2'
      with:
        credentials_json: '${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}'



     - name: Install gcloud CLI
      run: |
        sudo apt-get update
        sudo apt-get install -y apt-transport-https ca-certificates gnupg
  echo "deb https://packages.cloud.google.com/apt cloud-sdk main" | sudo      tee   -a /etc/apt/sources.list.d/google-cloud-sdk.list
        curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - sudo apt-get update &amp;amp;&amp;amp; sudo apt-get install -y google-cloud-cli
    - name: Configure gcloud CLI
      run: |
        gcloud config set project ${{ secrets.GCP_SERVICE_ACCOUNT_PROJECT }}
        gcloud config set compute/zone us-central1-a
    - name: Verify Authentication
      run: gcloud compute instances list
    - name: Update Instance with New Changes
      run: |
        gcloud compute ssh ${{ secrets.GCP_SERVICE_ACCOUNT_INSTANCE }} \
          --zone us-central1-a \
          --command "
            sudo su -c 'ls -l / &amp;amp;&amp;amp; \
            cd /automation/ &amp;amp;&amp;amp; \
            ls -l &amp;amp;&amp;amp; \
            git pull &amp;amp;&amp;amp; \
            docker build -t client-end . &amp;amp;&amp;amp; \
            docker stop client-end-container || true &amp;amp;&amp;amp; \
            docker rm client-end-container || true &amp;amp;&amp;amp; \
            docker run -d -p 3000:3000 --name client-end-container client-end'"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Steps Breakdown&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Checkout the Code
The workflow begins by checking out the repository's code using the actions/checkout action. This ensures that the latest changes from the repository are available.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- name: Checkout code
  uses: actions/checkout@v3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Authenticate with GCP
This step uses the google-github-actions/auth action to authenticate with GCP. The credentials are securely stored in GitHub Secrets as GCP_SERVICE_ACCOUNT_KEY.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- name: Set up Google Cloud Authentication
  uses: 'google-github-actions/auth@v2'
  with:
    credentials_json: '${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Install gcloud CLI
The gcloud CLI is installed on the runner. This tool is necessary for managing GCP resources and executing commands on the Compute Engine instance.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- name: Install gcloud CLI
  run: |
    sudo apt-get update
    sudo apt-get install -y apt-transport-https ca-certificates gnupg
    echo "deb https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
    curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    sudo apt-get update &amp;amp;&amp;amp; sudo apt-get install -y google-cloud-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Configure gcloud CLI
The gcloud CLI is configured to use the target GCP project and compute zone.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- name: Configure gcloud CLI
  run: |
    gcloud config set project ${{ secrets.GCP_SERVICE_ACCOUNT_PROJECT }}
    gcloud config set compute/zone us-central1-a

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Verify Authentication
The workflow verifies that the authentication and configuration are correct by listing the Compute Engine instances in the project.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- name: Verify Authentication
  run: gcloud compute instances list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Update the Instance
Finally, the workflow connects to the Compute Engine instance via SSH, pulls the latest changes from the repository, builds the Docker image, and deploys it.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- name: Update Instance with New Changes
  run: |
    gcloud compute ssh ${{ secrets.GCP_SERVICE_ACCOUNT_INSTANCE }} \
      --zone us-central1-a \
      --command "
        sudo su -c 'ls -l / &amp;amp;&amp;amp; \
        cd /automation/ &amp;amp;&amp;amp; \
        ls -l &amp;amp;&amp;amp; \
        git pull &amp;amp;&amp;amp; \
        docker build -t client-end . &amp;amp;&amp;amp; \
        docker stop client-end-container || true &amp;amp;&amp;amp; \
        docker rm client-end-container || true &amp;amp;&amp;amp; \
        docker run -d -p 3000:3000 --name client-end-container client-end'
      "
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conclusion&lt;br&gt;
This GitHub Actions workflow ensures that the application is automatically updated and redeployed on the GCP Compute Engine instance whenever changes are pushed to the main branch. By integrating GitHub Actions with GCP, this approach provides a reliable, repeatable, and scalable solution for managing deployments.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
