In the previous article, we deployed an Adonis.js 5.0 application from our local development environment to Google App Engine. In this post, I will show you how to set up Google Cloud Build to automate the deployment of our application. We'll connect a remote GitHub repository such that every time we push to the master branch our changes are deployed to Google App Engine.
To get started let's run the following commands to push our code to a remote GitHub repository.
git add .
git commit -m "first commit"
git remote add origin <your-github-repository>
git push -u origin master
Connecting Our GitHub Repository to Google Cloud
To connect our GitHub repository:
- Open up Cloud Source Repositories in Google Cloud Console.
- On the top right, click the Add Repository button then select the Connect external repository option.
- Select your project from the dropdown menu, select GitHub from the Git provider menu and then click the Connect to GitHub button.
- Once you have authorized Google Cloud on your GitHub account you can then select the required repository and click the Connect selected repository button
Now every time you push code to your GitHub repository it gets mirrored on Google Cloud Source Repositories.
Defining our Cloud Build Steps
To automate our deployment we need to tell Google Cloud Build what to do with the code it mirrored from GitHub. We do this in the cloudbuild.yaml
file. For our Adonis.js 5.0 application, we will require the following steps:
- Install the Node.js dependencies in our
package.json
file: We need to do this because we askedgit
to ignore thenode_modules
folder in the.gitignore
file of our project. - Set environment variables: Adonis.js has a
.env
file where environment variables are stored but we also removed it from version control for security reasons. However, we will keep the.env
file safe in a Google Cloud Storage Bucket. To make the.env
file available in our App Engine deployment, we will need to copy it from the Google Cloud Storage bucket to the deployment folder. - Run the Adonis.js 5.0 build command: This will compile our Adonis.js 5.0 application into the
build
folder. You can see more details about that here. - Copy the
app.yaml
file into the build folder: Since we will be deploying only the contents of the build folder, we need to copy ourapp.yaml
file into it so Google App Engine knows how to run our application. - Run the application on App Engine: In this step, we push the contents of the
build
folder to Google App Engine for deployment.
Here is an example of how we specify these steps in the cloudbuild.yaml
file:
steps:
# Install node.js dependencies
- name: node
entrypoint: npm
args: ["install"]
# Fetch environment variables from storage
- name: gcr.io/cloud-builders/gsutil
args:
[
"cp",
"gs://crudecoding.appspot.com/deployment-files/${_ENV}.env",
".env",
]
# Run build script
- name: node
entrypoint: node
args: ["ace", "build", "--production"]
# Copy app.yaml into build folder
- name: gcr.io/cloud-builders/gsutil
args: ["cp", "app.yaml", "build/app.yaml"]
# Deploy application
- name: "gcr.io/cloud-builders/gcloud"
dir: "build"
args: ["app", "deploy"]
What's happening in our cloudbuild.yaml
file?
The name parameter in each step specifies the tool Google Cloud Build should use in running the step. This could be a container image from DockerHub or one provided by Google. For example, when we specified node
in the Run build script
step, Google Cloud Build pulls the Node.js image from DockerHub.
The entrypoint parameter specifies the command-line tool to be used and then the args parameter specify each parameter passed to the command-line tool. For example in the Install node.js dependencies
step the Node.js image from DockerHub comes with npm installed so we can use it as an entry point and supply ["install"]
in args so that we have the npm install
, or in the Run build script
step we supply ["ace", "build", "--production"]
to node
to get node ace build --production
.
Create a Build Trigger
Once we have created the cloudbuild.yaml
file in the root of our application and committed it to the remote repository, the final step is to create a trigger on Google Cloud Build. To do this:
- Open the Triggers page in Google Cloud Console, select your project and click Create Trigger
- Fill out the name and description.
- Under Event select push to a branch.
- Under Source select the repository you mirrored from GitHub earlier. Select ^master$ to trigger a build every time there's a push to the master branch.
- Under Build Configuration select Cloud Build Configuration file and leave the file location as cloudbuild.yaml then click the create
That should be all. If we did everything right when we push to the master branch on our GitHub repository our application gets deployed on Google App Engine. In the next article, we'll talk about how we can connect to the database and run migrations from Google Cloud Build.
Top comments (0)