├── Dockerfile ├── README.md ├── index.html ├── layout.html ├── main.py └── requirements.txt /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.7 2 | WORKDIR /app 3 | COPY . . 4 | RUN pip install gunicorn 5 | RUN pip install -r requirements.txt 6 | ENV PORT=80 7 | CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DevOpsProjectWithGCP 2 | 3 | In this project, you will build a continuous integration pipeline using Cloud Source Repositories, Cloud Build, build triggers, and Container Registry. 4 | 5 | ![0hYrmLCtV96fbLY7rct7Ue94xgf6OUG+O930FbxOeuA=](https://github.com/logicopslab/DevOpsProjectWithGCP/assets/82759985/c1dcc6c3-abb7-4925-8a71-b00b60ddbf80) 6 | 7 | **Prerequisites** 8 | 9 | 1) Google Cloud tools Cloud Source Repositories. 10 | 2) Cloud Build. 11 | 3) Build triggers. 12 | 4) Container Registry. 13 | 14 | And, a bit of familiarity with GCP. 15 | 16 | **Objectives - In this lab, you will learn how to perform the following tasks:** 17 | 18 | 1) Create a Git repository 19 | 2) Create a simple Python application 20 | 3) Test Your web application in Cloud Shell 21 | 4) Define a Docker build 22 | 5) Manage Docker images with Cloud Build and Container Registry 23 | 6) Automate builds with triggers 24 | 7) Test your build changes 25 | 8) You have done a great job! Pat ya back! 26 | 27 | **Task Distribution** 28 | 29 | **Task 1. Create a Git repository** 30 | 31 | First, you will create a Git repository using the Cloud Source Repositories service in Google Cloud. This Git repository will be used to store your source code. Eventually, you will create a build trigger that starts a continuous integration pipeline when code is pushed to it. 32 | 33 | 1) In the Cloud Console, on the Navigation menu, click Source Repositories. A new tab will open. 34 | 2) Click Add repository. 35 | 3) Select Create new repository and click Continue. 36 | 4) Name the repository devops-repo. 37 | 5) Select your current project ID from the list. 38 | 6) Click Create. 39 | 7) Return to the Cloud Console, and click Activate Cloud Shell (Cloud Shell icon). 40 | 8) If prompted, click Continue. 41 | 9) Enter the following command in Cloud Shell to create a folder called gcp-course: 42 | mkdir gcp-course 43 | 10) Change to the folder you just created: 44 | cd gcp-course 45 | 11) Now clone the empty repository you just created: 46 | gcloud source repos clone devops-repo 47 | 12) The previous command created an empty folder called devops-repo. Change to that folder: 48 | cd devops-repo 49 | 50 | **Task 2. Create a simple Python application** 51 | 52 | You need some source code to manage. So, you will create a simple Python Flask web application. The application will be only slightly better than "hello world", but it will be good enough to test the pipeline you will build. 53 | 54 | 1) In Cloud Shell, click Open Editor (Editor icon) to open the code editor. If prompted click Open in a new window. 55 | 2) Select the gcp-course > devops-repo folder in the explorer tree on the left. 56 | 3) Click on devops-repo 57 | 4) On the File menu, click New File 58 | 5) Paste the following code into the file you just created: 59 | https://github.com/logicopslab/DevOpsProjectWithGCP/blob/main/main.py 60 | 6) To save your changes. Press CTRL + S, and name the file as main.py. 61 | 7) Click on SAVE 62 | 8) Click on the devops-repo folder. 63 | 9) Click on the File menu, click New Folder, Enter folder name as templates. 64 | 10) Click OK 65 | 11) Right-click on the templates folder and create a new file called layout.html. 66 | 12) Add the following code and save the file as you did before: 67 | https://github.com/logicopslab/DevOpsProjectWithGCP/blob/main/layout.html 68 | 13) Also in the templates folder, add another new file called index.html. Add the following code and save the file as you did before: 69 | https://github.com/logicopslab/DevOpsProjectWithGCP/blob/main/index.html 70 | 14) In Python, application prerequisites are managed using pip. Now you will add a file that lists the requirements for this application. 71 | 15) In the devops-repo folder (not the templates folder), create a New File and add the following to that file and save it as requirements.txt: 72 | https://github.com/logicopslab/DevOpsProjectWithGCP/blob/main/requirements.txt 73 | 16) You have some files now, so save them to the repository. First, you need to add all the files you created to your local Git repo. In Cloud Shell, enter the following code: 74 | cd ~/gcp-course/devops-repo 75 | git add --all 76 | 17) To commit changes to the repository, you have to identify yourself. Enter the following commands, but with your information (you can just use your Gmail address or any other email address): 77 | git config --global user.email "you@example.com" 78 | git config --global user.name "Your Name" 79 | 18) Now, commit the changes locally: 80 | git commit -a -m "Initial Commit" 81 | 19) You committed the changes locally, but have not updated the Git repository you created in Cloud Source Repositories. Enter the following command to push your changes to the cloud: 82 | git push origin master 83 | 20) Refresh the Source Repositories web page. You should see the files you just created. 84 | 85 | **Task 3. Define a Docker build** 86 | 87 | The first step to using Docker is to create a file called Dockerfile. This file defines how a Docker container is constructed. You will do that now. 88 | 89 | 1) In the Cloud Shell Code Editor, expand the gcp-course/devops-repo folder. With the devops-repo folder selected, on the File menu, click New File and name the new file Dockerfile. 90 | The file Dockerfile is used to define how the container is built. 91 | https://github.com/logicopslab/DevOpsProjectWithGCP/blob/main/Dockerfile 92 | 93 | **Task 4. Manage Docker images with Cloud Build and Container Registry** 94 | 95 | The Docker image has to be built and then stored somewhere. You will use Cloud Build and Container Registry. 96 | 97 | 1) Return to Cloud Shell. Make sure you are in the right folder: 98 | cd ~/gcp-course/devops-repo 99 | 2) The Cloud Shell environment variable DEVSHELL_PROJECT_ID automatically has your current project ID stored. The project ID is required to store images in Container Registry. Enter the following command to view your project ID: 100 | echo $DEVSHELL_PROJECT_ID 101 | 3) Enter the following command to use Cloud Build to build your image: 102 | gcloud builds submit --tag gcr.io/$DEVSHELL_PROJECT_ID/devops-image:v0.1 . 103 | Notice the environment variable in the command. The image will be stored in Container Registry. 104 | 4) If asked to enable Cloud Build in your project, type Yes. Wait for the build to complete successfully. 105 | **Note #1**: If you receive the error "INVALID_ARGUMENT: unable to resolve source", wait a few minutes and try again. 106 | **Note**: In Container Registry, the image name always begins with gcr.io/, followed by the project ID of the project you are working in, followed by the image name and version. 107 | 108 | The period at the end of the command represents the path to the Dockerfile: in this case, the current directory. 109 | 110 | 5) Return to the Cloud Console and on the Navigation menu ( Navigation menu icon), click Container Registry. Your image should be on the list. 111 | 6) Now navigate to the Cloud Build service, and your build should be listed in the history. 112 | 113 | You will now try running this image from a Compute Engine virtual machine. 114 | 115 | 7) Navigate to the Compute Engine service. 116 | 8) Click Create Instance to create a VM. 117 | 9) On the Create an instance page, specify the following, and leave the remaining settings as their defaults: 118 | image 119 | 120 | 10) Click Create. 121 | 11) Once the VM starts, create a browser tab and make a request to this new VM's external IP address. The program should work as before. 122 | **Note**: You might have to wait a minute or so after the VM is created for the Docker container to start. 123 | 12) You will now save your changes to your Git repository. In Cloud Shell, enter the following to make sure you are in the right folder and add your new Dockerfile to Git: 124 | cd ~/gcp-course/devops-repo 125 | git add --all 126 | 13) Commit your changes locally: 127 | git commit -am "Added Docker Support" 128 | 14) Push your changes to Cloud Source Repositories: 129 | git push origin master 130 | 15) Return to Cloud Source Repositories and verify that your changes were added to source control. 131 | 132 | **Task 5. Automate builds with triggers** 133 | 134 | 1) On the Navigation menu (Navigation menu icon), click Container Registry. At this point, you should have a folder named devops-image with at least one container in it. 135 | 2) On the Navigation menu, click Cloud Build. The Build history page should open, and one or more builds should be in your history. 136 | 3) Click the Triggers link on the left. 137 | 4) Click Create trigger. 138 | 5) Name the trigger devops-trigger. 139 | 6) Select your devops-repo Git repository under repository dropdown. 140 | 7) Select .*(any branch) for the branch. 141 | 8) Choose Dockerfile for Configuration and select the default image. 142 | 9) Accept the rest of the defaults, and click Create. 143 | 10) To test the trigger, click Run and then Run trigger. 144 | 11) Click the History link and you should see a build running. Wait for the build to finish, and then click the link to it to see its details. 145 | 12) Scroll down and look at the logs. The output of the build here is what you would have seen if you were running it on your machine. 146 | 13) Return to the Container Registry service. You should see a new folder, devops-repo, with a new image in it. 147 | 14) Return to the Cloud Shell Code Editor. Find the file main.py in the gcp-course/devops-repo folder. 148 | 15) In the main() function, change the title property to "Hello Build Trigger." as shown below: 149 | 150 | @app.route("/") 151 | def main(): 152 | model = {"title": "Hello Build Trigger."} 153 | return render_template(`index.html`, model=model) 154 | 155 | 16) Commit the change with the following command: 156 | cd ~/gcp-course/devops-repo 157 | git commit -a -m "Testing Build Trigger" 158 | 159 | 17) Enter the following to push your changes to Cloud Source Repositories: 160 | git push origin master 161 | 18) Return to the Cloud Console and the Cloud Build service. You should see another build running. 162 | 163 | **Task 6. Test your build changes** 164 | 165 | 1) When the build completes, click on it to see its details. Under Execution Details, copy the Image link, format should be gcr.io/qwiklabs-gcp-00-f23112/devops-repoxx34345xx. 166 | 2) Go to the Compute Engine service. As you did earlier, create a new virtual machine to test this image. Click DEPLOY CONTAINER and paste the image you just copied. 167 | 3) Select Allow HTTP traffic. 168 | 4) When the machine is created, test your change by making a request to the VM's external IP address in your browser. Your new message should be displayed. 169 | **Note**: You might have to wait a few minutes after the VM is created for the Docker container to start. 170 | 171 | **Congratulations!** 172 | In this lab, you built a continuous integration pipeline using the Google Cloud tools Cloud Source Repositories, Cloud Build, build triggers, and Container Registry. 173 | 174 | **Credits:-** 175 | 176 | Lab link - https://partner.cloudskillsboost.google/course_templates/41 177 | Disclaimer - I do not own the code/process/tech material used in this project. This code/process is taken from QuickLabs for studying/information purposes. 178 | Website - https://go.qwiklabs.com/ 179 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | {% extends "layout.html" %} 2 | {% block content %} 3 |
4 |
5 |

{{model.title}}

6 |
7 |
8 | {% endblock %} 9 | -------------------------------------------------------------------------------- /layout.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | {{model.title}} 5 | 6 | 7 | 8 | 9 |
10 | {% block content %}{% endblock %} 11 | 12 |
13 | 14 | 15 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request 2 | app = Flask(__name__) 3 | @app.route("/") 4 | def main(): 5 | model = {"title": "Hello! Welcome to the GCP DevOps Project!!"} 6 | return render_template('index.html', model=model) 7 | if __name__ == "__main__": 8 | app.run(host='0.0.0.0', port=8080, debug=True, threaded=True) 9 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==2.0.3 2 | --------------------------------------------------------------------------------