> For the complete documentation index, see [llms.txt](https://bhushans-devops-organization.gitbook.io/gcp-devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bhushans-devops-organization.gitbook.io/gcp-devops/gcp-devops-handson-or-cloud-build-cloud-build-trigger-container-registry-or.md).

# GCP DevOps - HandsOn | Cloud Build , Cloud Build Trigger , Container Registry |

In this lab, you built a continuous integration pipeline using the Google Cloud tools Cloud Source Repositories, Cloud Build, build triggers, and Container Registry.

<mark style="color:red;">**Cloud Source Repositories , Cloud Build,  Cloud Build Trigger ,  Container Registry ,**</mark>&#x20;

## DevOpsProjectWithGCP

In this project, you will build a continuous integration pipeline using Cloud Source Repositories, Cloud Build, build triggers, and Container Registry.

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/ULJia0LyLTBIpLjgXRDn/image.png" alt=""><figcaption></figcaption></figure>

### **Prerequisites**

1. Google Cloud tools Cloud Source Repositories.
2. Cloud Build.
3. Build triggers.
4. Container Registry.

And, a bit of familiarity with GCP.

### **Objectives - In this lab, you will learn how to perform the following tasks:**

1. Create a Git repository
2. Create a simple Python application
3. Test Your web application in Cloud Shell
4. Define a Docker build
5. Manage Docker images with Cloud Build and Container Registry
6. Automate builds with triggers
7. Test your build changes
8. You have done a great job! Pat ya back!

## Task 1 : Create a Repository&#x20;

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/DZ4YBktfdsiKmA6tBV26/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/U7gnbtUK1SJLZEpih7ZQ/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/12Av4C4XxGZdLugZUZmo/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/FXcYfaVAUtg5Dnu0uOOF/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/AkOBMZjmYT1D1Pu4cBdJ/image.png" alt=""><figcaption></figcaption></figure>

After creating this repo  ->  got Cloud Console  ->  Activate Cloud shell&#x20;

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/RWserhEiiH1dKNFyChrq/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/SDnirlE18RUjIMHtCmL9/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/akp3T6rQZy4svp1rAWfp/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/7nU0VdGuAZ4b31z97a2c/image.png" alt=""><figcaption></figcaption></figure>

```
mkdir gcp-course                                     #creating the folder
ls
cd gcp-course
gcloud repos clone DevOps-repo                      #cloning the repo in our folder

```

## Task 2: Create a Simple python application

open the editor on gcp -> click on new window  -> select the folder or file of you repo&#x20;

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/bYh3m1Mb4yr19ySIrOVg/image.png" alt=""><figcaption></figcaption></figure>

### **main.py**

```

from flask import Flask, render_template, request
app = Flask(__name__)
@app.route("/")
def main():
    model = {"title": "Hello! Welcome to the GCP DevOps Project!!"}
    return render_template('index.html', model=model)
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080, debug=True, threaded=True)

```

Save this file cntrl+S

Create a new folder in the gcp-course/DevOps-repo folder "templetes"

Afte that -

### Create a new file "layout.html" inside the templetes folder

paste the htm code here -&#x20;

```
<!doctype html>
<html lang="en">
<head>
    <title>{{model.title}}</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        {% block content %}{% endblock %}
        <footer></footer>
    </div>
</body>
</html>
```

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/BNLocifBibB53YvWBxw7/image.png" alt=""><figcaption></figcaption></figure>

### Create a index.html file in the tempplete folder

```
{% extends "layout.html" %}
{% block content %}
<div class="jumbotron">
    <div class="container">
        <h1>{{model.title}}</h1>
    </div>
</div>
{% endblock %}
```

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/yH4uIcPhdHiBDHp4Wr5l/image.png" alt=""><figcaption></figcaption></figure>

Create a requriements.txt  in the templete folder

```
Flask==2.0.3
```

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/crwvVAXBXFn6h0VanQwg/image.png" alt=""><figcaption></figcaption></figure>

goto on the Cloud shell

```
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git status

```

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/y4yzQVrGWdFjl5KoqcvO/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/HUQtkfiyLGtpt5MLsO4P/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/dxINRc6yyDjJAT7BMQZS/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/tOcBVvIkTL8eHYhw5Tlx/image.png" alt=""><figcaption></figcaption></figure>

```
git add --all
git status
git commit -a -m "Message"
git push origin master

```

## **Task 3. Define a Docker build**

Create a dockerfile in templete folder and push it on th repo

### **Dockerfile-**

```
FROM python:3.7
WORKDIR /app
COPY . .
RUN pip install gunicorn
RUN pip install -r requirements.txt
ENV PORT=80
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app

```

```
git add --all
git status
git commit -a -m "Message"
git push origin master
```

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/s3PdKgQNOOceL00OaV5Q/image.png" alt=""><figcaption></figcaption></figure>

## **Task 4. Manage Docker images with Cloud Build and Container Registry**

For checking the project id Commands-

```
 echo $DEVSHELL_PROJECT_ID
```

So now , For building the docker file   ->  goto the templete folder , where your docker file is present&#x20;

```
gcloud builds submit --tag gcr.io/$DEVSHELL_PROJECT_ID/devops-image:v0.
```

### IMAGE CREATE

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/DOb4hEWa6K88EQaX8kCG/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/uFr3WolE8h9ROU7M72XH/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/U4bySqBBsLIjOcyWsyZM/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/biCOUaVUuHlDymEnKTdm/image.png" alt=""><figcaption></figcaption></figure>

### Container Ragistry - images&#x20;

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/Ki7y9QC8kTwCr98men9i/image.png" alt=""><figcaption></figcaption></figure>

### Create a Trigger -

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/CLcBm4FgUixYKngKAqzD/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/i89fyFtIu3p5LFWmzoeY/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/HSn1YpZUEdwLCbDTb8N4/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/xjQHMhP74aLZBm27FCa2/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/64XFVFv8E8Vtjjbt48Bh/image.png" alt="" width="361"><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/oosAm5MxRfzgVBa6vrKE/image.png" alt=""><figcaption></figcaption></figure>

## Cloud Build -&#x20;

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/5nWKhdHGYZ2AflZ4dEPM/image.png" alt=""><figcaption></figcaption></figure>

for building the code crreate an virtusl machine - Compute Engine  & allow the HTTP traffic

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/KgAcoCdjWluPbwcgXkoG/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/VHJWCaL7mlAG9splx75Q/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/0dIahUxNa927qkgUKdYw/image.png" alt=""><figcaption></figcaption></figure>

pushing all the files on the repo -

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/645vDbJIQf1NIqx3ICY2/image.png" alt=""><figcaption></figcaption></figure>

## **Task 5. Automate builds with triggers**

Create a Trigger -

Cloud Build -> Trigger ->  Create Trigger -> Follow Below images

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/oRqbJf5lV2vPigVDoE0e/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/5Y1RsTnBTQ9py1XCIzMy/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/dVjkjelmn8dL05vIJ8WC/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/NWXaWTZPiy4oCBWyMD2D/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/jA3r8peVZHihB3uat21D/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/aUt98OBO7YSY2lFZPczn/image.png" alt="" width="369"><figcaption></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/NPLqce0CFOiVM3fTJRBY/blobs/SkS2Q3fhNneDSHaEURFe/image.png" alt=""><figcaption></figcaption></figure>
