It is essential to have multiple environments for setting up continuous delivery for any type of application. But for a serverless world, there is no clear demarcation for the environment. So we always have to come up with alternatives. We have to do the same for Cloud Functions. We set up environment separation in two ways—first, we could separate the functions for the environment with separate names, for example, my-serverless-dev, my-serverless-sit or my-serverless-prod, but this will add unnecessary complications. So, the ideal way would be to separate the environment by creating a different project, as follows:
- Log on to the GCP console, go to the Manage Resource Page (https://console.cloud.google.com/cloud-resource-manager?_ga=2.108039562.-900655901.1524348645), click on CREATE PROJECT, enter the project name as Serverless-SIT in the Project Name and click on Create. You will see that the project is being created in the notification bell image on the top right-hand side of the page. Once it has stopped, refresh the page and you will see the project in the list. Likewise, let's create Serverless-UAT and Serverless-PROD projects. I am not creating dev as we are using a local emulator for dev environment.
- Let's enable the cloud API for the project by opening the link: https://console.cloud.google.com/flows/enableapi?apiid=cloudfunctions&redirect=https://cloud.google.com/functions/quickstart. The page will have a drop-down list for projects. Select My Serverless Project and click on Continue, then API will be enabled for the project.
- Log in to the Jenkins portal (http://localhost:8080), which we created in a previous section of the chapter. Then click on New Item.
- Enter the item name in the textbox as my-serverless-google-function-pipeline and then select Pipeline from the list and then click OK.
- Click on the checkbox This project is parameterized and, in the Add Parameter drop-down, select String Parameter. Then let's add DEV_PROJECT_ID in the Name textbox and the Default Value textbox should have project id for the dev which we created in Step 1. Likewise, let's create a text parameter for each environment until PROD, and then the last text parameter should be YOUR_GCF_REGION.
- Click on the Pipeline tab, in the definition dropdown Pipeline script from SCM, and in the SCM dropdown select Git. Now, in the repository URL textbox add https://github.com/shzshi/google-functions-helloworld.git and then keep everything as default and click on Save.
- Once the job is saved, we will build the pipeline, for which we need to click on Build with Parameters, so we will see default values for each parameter. In case you want to change the environment or add a different project id, we can feed it through the textbox.
- Now click on Build, the job should first init, which is setting up pre-requisites, then it will run a unit test, then the function is deployed on the local dev environment, then integrate test run. In the next stage, functions are deployed into the UAT environment and the same will happen to other environments. The gated approval is been setup for production deployment. This pipeline can be done in many ways.