I am trying from last two days and not a big success yet, I am not sure if I am on right path: Here is my scenario:
Created a VM in google cloud compute engine and enabled http & https
Enabled CLOUD BUILDER API and created trigger for GitHub push in main branch
Also enabled the compute instance admin v1 (compute engine) inside settings -> Service account permissions
So I am developing an sample test NodeJS app to check how to automate everything from push to live deployment and I can see examples and guide for push and deployment via cloud run , app engine, GKE etc. but didn't find any guide for deploying to compute engine virtual machine;
here are some of files how I am trying to deploy my NodeJS application
index.js
const express = require('express');
const app = express()
const port=3000;
app.get('/',(req,res)=>{
res.send('Hello World')
});
app.listen(port,()=>{
console.log('sample app is running on port ${port}')
})
------------------------------------------------------
Dockerfile
FROM node:16
RUN mkdir -p /usr/src/app
# Create app directory
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 3000
CMD [ "npm", "start" ]
--------------------------------------------------
cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build','-t', 'eu.gcr.io/mysample-app/sampletestapp:latest','.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push','eu.gcr.io/mysample-app/sampletestapp:latest',]
- name: 'gcr.io/cloud-builders/docker'
args: ['run','--publish', '3000:3000','eu.gcr.io/mysample-app/sampletestapp:latest']
images: ['eu.gcr.io/mysample-app/sampletestapp:latest']
-----------------------------------------------------------
With all above setup when I commit some changes, i can see trigger is running and it build the application successfully, and it fails at docker run
Finished Step #1
Starting Step #2
Step #2: Already have image (with digest): gcr.io/cloud-builders/docker
Step #2:
Step #2: > testapp@1.0.0 start
Step #2: > dist/index.js
Step #2:
Step #2: sh: 1: dist/index.js: Permission denied
Finished Step #2
ERROR
ERROR: build step 2 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 126
But even after successfully build I am unable to access my API (which is simple / root) Also when login to VM via SSH I don't see anything on that WORKDIR (/usr/src/app) /app directory doesn't exist, also found that docker is not installed
So I need help in these :
How cloud builder know where to build and deploy because I didn't mentioned any linking with my virtual machine in cloudbuilder.yml, which VM it will use to deploy or if we don't mention then it choose the first running VM
Is it possible to build and run automatically on VM via this approach ?, if not what I can do?
Is something wrong in my code ?
sorry for my bad explanation, Any help would be much appreciated
Thanks