> 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 |

<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="/files/g5Ur6ScVY9i51bdr7EyW" 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="/files/GjqBXwAFIP56230Ct8hq" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/ziZCPtjDYJM24uZ2ZpAl" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/vzzqX8nglcBelbsQlX8S" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/7m4U3v6vKL4I0piaPbxr" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/RMNiwN9wp5OwxghHkc5h" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/QHpN51htHPq5KJxPQnr6" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/0yg7AD4AJto3VlE8SBxn" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/zPKy5X39IQuN080tqEqU" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/N2nR8Kmb9BQrWWVm3NTN" 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="/files/ibBRnwC69B10nqdCj7FJ" 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="/files/GckNiOnTsGaWSUJA85Xa" 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="/files/520ns8l06gUlRo0BbRwO" alt=""><figcaption></figcaption></figure>

Create a requriements.txt  in the templete folder

```
Flask==2.0.3
```

<figure><img src="/files/F4a7JAKSvdDNZjPXzwhe" 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="/files/AkPEatnDGNseBDxNcXML" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/dIv4VmyCuxZfG3ZxIW5p" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/Ikgpin2qX4A82rMlcDpp" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/1u7sPthLdOYwoMz4eb2y" 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="/files/NqcxcjHKUShiCn1YNX5h" 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="/files/ap08SU1k1UXINZjudF3M" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/s37R1fjEhnsknlDzDZbC" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/JadtKRgSOiT0q0U7QBYM" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/xbDHPus34UJJL4QNzOOn" alt=""><figcaption></figcaption></figure>

### Container Ragistry - images&#x20;

<figure><img src="/files/d0bKKryYvDtsdODcskqT" alt=""><figcaption></figcaption></figure>

### Create a Trigger -

<figure><img src="/files/VePUdKi0iyQpog6SEAeu" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/1Gk2xenPkdGKzNxkAlDU" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/WYW0TGxq8ipmuw4bUgCS" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/UJOB4yRhGq1XJXn4jng7" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/HGLTUTXFICBcvl1TXGTt" alt="" width="361"><figcaption></figcaption></figure>

<figure><img src="/files/Rh1TYfRBnZqveX2UqWWS" alt=""><figcaption></figcaption></figure>

## Cloud Build -&#x20;

<figure><img src="/files/JwIAUW4bk0yncUF8Pahk" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/Yp2SxLCjD1OQIABLuajJ" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/cWvn2ylibfcjjfm6MkCR" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/yMOSuewoEjLdn0CzM77K" alt=""><figcaption></figcaption></figure>

pushing all the files on the repo -

<figure><img src="/files/j4HNaNU9cbLvZ7ELhbHR" alt=""><figcaption></figcaption></figure>

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

Create a Trigger -

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

<figure><img src="/files/SM6DuktrfpucNEQVnilF" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/w3apFsK7vaR5oR7yjFNp" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/oXyCyH8NlDsUwP6TbneP" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/dCaVRBs7hgEGUNoSdx7G" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/bBAYg8jfkp3vVfhymDks" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/IxUUt4jIyRoGACJ1nTNC" alt="" width="369"><figcaption></figcaption></figure>

<figure><img src="/files/B9Y4uxNcwRLMa2tjMeya" alt=""><figcaption></figcaption></figure>
