GitHub autodeploy node js app in Google cloud - google-app-engine

I have successfully started my nodejs app (discord bot) in Google App engine and Google Compute Engine but i couldn't auto deploy the commits made on github
Is there and way to do those, so that if i commit a change the it should be updated in Google Cloud Programs
I tried Google open cloud source repositories but didn't work

I tried GitHub Actions as mentioned by #Ismail and it worked for me
# This is a basic workflow to help you get started with Actions
name: Upload to Google Cloud App Engine
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
pull_request:
branches: [ master ]
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v2
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud#master
with:
project_id: myid
service_account_key: ${{ secrets.GCP_AUTHCode }}
export_default_credentials: true
- id: Deploy
name: Deploy to App Engine
uses: google-github-actions/deploy-appengine#main
with:
project_id: myid
- uses: actions/setup-node#v1
with:
node-version: 12
- run: npm install
- run: npm install pm2 -g
- run: pm2 start index.js --watch

You can automate App Engine deployments with Cloud Build. Study and follow this GitHub App trigger guide.
With regards to build configuration, you can use this sample build config file (cloudbuild.yaml):
steps:
- name: "gcr.io/cloud-builders/gcloud"
args: ["app", "deploy"]
timeout: "1600s"
This configuration will make Cloud Build run gcloud app deploy on your repo whenever you push commits. The step will timeout if the deployment takes 27 minutes but you're free to change it.
As additional reference, here's another guide on how to automate app deployment with Cloud Source Repositories.
To summarize, you need to enable Cloud Build on your project to use its trigger feature and it will build your app from source then deploy based on your build config.

Related

GitHub Action for Google App Engine (deliverables)

I am trying to set up a GitHub action to automatically deploy to Google App Engine when I push to the master branch. However, I am quite new in the pipelines. I tried to do my homework and I ended up stuck with this.
First, this is my .github/workflows/main.yml:
name: Deployment
on:
push:
branches:
- master
jobs:
deploy:
name: Deploy to App Engine
runs-on: ubuntu-latest
steps:
- name: Google App Engine
uses: google-github-actions/deploy-appengine#v0.4.0
with:
project_id: my-gae-project-id
version: master
credentials: ${{secrets.GOOGLE_APP_ENGINE_KEY}}
flags: --no-cache
deliverables: app.yaml
Now I tried to create the app.yaml in the root directory and also in the .github/workflows directory. None of them works. For now, my app.yaml is just:
runtime: php74
env: standard
Here is the outcome of my GH action everytime:
I tried to do some research and found recommendation to put the app.yaml in a folder, so I also tried .github/workflows/gae/app.yaml and then set it as deliverables: gae/app.yaml - no success. Also tried to put the path in quotes like deliverables: "gae/app.yaml" - no success. Also tried to put the app.yaml to the root and set it as deliverables: ../../app.yaml. In some cases, the app.yaml creates a new GitHub action which is even more irritating.
Now I'm just getting frustrated, because I know this is going to be something silly, but I didn't find many resources about this specific case.
There is a missing step in your main.yml that's why your workflow can't find and access the files. To fix it, add checkout#v2 action in your steps:
name: Deployment
on:
push:
branches:
- master
jobs:
deploy:
name: Deploy to App Engine
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Google App Engine
uses: google-github-actions/deploy-appengine#v0.4.0
with:
project_id: my-gae-project-id
version: master
credentials: ${{secrets.GOOGLE_APP_ENGINE_KEY}}
flags: --no-cache
deliverables: app.yaml
You can also visit this official github example for your reference.

Standard App Engine migrating to Go 1.11 cloud build failed

I am trying to migrate from Go 1.9 to Go 1.11. I am using cloudbuild.yaml to get dependencies and deploy the application. However, I am getting this error:
Failed to build app: Your app is not on your GOPATH, please move it there and try again.
I checked the migration document, and it says I need to put the application into the GOPATH. However, I am not sure if that's possible with cloudbuild.yaml. If yes, could someone share an example?
A simplified version of my cloudbuild.yaml. As far as I know the application source code is under /workspace in this situation. I have tried to include /workspace in GOPATH in the deploy step but no luck.
steps:
- name: 'gcr.io/cloud-builders/go'
args: ['get', 'github.com/go-sql-driver/mysql']
env: ['GOPATH=go']
- name: 'gcr.io/cloud-builders/gcloud'
args: ['app', 'deploy', 'go/src/batch.yaml']
env: ['GOPATH=go']
File structure:
/repo
|-go
|-src
|-batch.yaml
|cronjob.go

How to use private, self hosted NPM package with Google App Engine node, standard environment

I have an NPM package hosted on a private Bitbucket git repo (not in the official NPM registry).
I have this in my package.json, under the "dependencies" key:
"a-private-package" git+ssh://git#bitbucket.org:myusername/a-private-package.git
It works when I run npm install locally as my SSH keys are used.
But when I use gcloud app deploy to deploy to the app engine standard environment for node, I get a Host key verification failed from Google Cloud Build.
I have tried:
Adding a custom SSH key to Cloud Build.
https://cloud.google.com/cloud-build/docs/access-private-github-repos
Issue: No access to cloudbuild.yaml for GAE standard; cannot tell git to use the SSH key.
Adding my private git repo to Google Sources.
Issue: No access to cloudbuild.yaml for GAE standard; cannot tell git to use the SSH key.
npm pack; npm install
Issue: Does not keep repo history/URL.
Is it actually possible?
It is not possible to modify the cloudbuild.yaml when you are running gcloud app deploy. Instead, you must create a new cloudbuild.yaml and execute it with gcloud builds submit --config=cloudbuild.yaml . In this case, the gcloud app deploy is going to be executed inside the cloudbuild.yaml.
I have tried the steps described for connecting to a private Github repository and changing the values to fit it with bitbucket, but was not able to. Thus, I have created this Feature Request for better documentation
Using Cloud Source Repositories
I believe that, as you already have a dependency on the private repo, then it will be simpler to host you entire app on it. Given this, you will have to clone the entire repo, run npm install and deploy.
In this case, Cloud Source Repositories has a built in feature to mirror directly to Bitbucket (public and private repos).
Steps:
1) Create on your app root folder a cloudbuild.yaml with the following code:
steps:
# NPM install
- name: 'gcr.io/cloud-builders/npm'
args: ['install']
#Test
- name: 'gcr.io/cloud-builders/npm'
args: ['test']
#Deploy
- name: "gcr.io/cloud-builders/gcloud"
args: ["app", "deploy"]
2) Connect Cloud Source Repositories to Bitbucket
3) Create a Cloud Build Trigger (so new code pushed to the repo will be automatically deployed)
4) Push the root folder containing the app.yaml and the cloudbuild.yaml to the repo
It should now be Synced to Cloud Source Repositories and it should trigger the Cloud Build for the deploy.
Unfortunately you will need to embed a username/password in the package.json, but you can probably use the https endpoint:
"a-private-package": "git+https://myusername:password#bitbucket.org/myusername/a-private-package.git"
If this works for you I'd suggest creating a separate account on bitbucket and restricting it to view-only on that repo.

Successive build/releases to azure web app through DevOps pushes new build one directory deeper

I'm trying to deploy a ReactJS app to my Azure Web App through Azure DevOps. I've created a project on DevOps for it, and I'm attempting to set up a continuous integration pipeline so that every commit to master triggers a new build and deploys it to the web app. The azure-pipelines.yml file that specifies the build instructions is as below:
# Node.js with React
# Build a Node.js project that uses React.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript
pool:
vmImage: 'Ubuntu 16.04'
trigger:
- master
steps:
- task: NodeTool#0
inputs:
versionSpec: '8.x'
displayName: 'Install Node.js'
- script: |
npm install
npm run build
displayName: 'npm install and build'
- task: ArchiveFiles#2
inputs:
rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
includeRootFolder: false
- task: PublishBuildArtifacts#1
I created this file using the instructions provided at Build, test, and deploy Javascript Apps in Azure Pipelines
The build succeeds just fine, and I then created a release for the build using the following settings:
Azure release pipeline task setup
(Deploy Azure App Service > App Type: Web App > Package/Folder:$(System.DefaultWorkingDirectory)/**/*.zip)
I've also configured the physical path for the web app on azure to be site\wwwroot\build
The first time I create the build and release it, it gets deployed fine, and I see the changes reflected on the web app. However, every subsequent build does not automatically get updated to show on the web app. Using the diagnostic console on Kudu, I see that new builds are going one directory deeper, eg the next build will have the physical path of site\wwwroot\build\build. To see the new build, I have to manually change the path for the web app each time.
Edit: Here are some snapshots of the Azure Web App settings, and of the console on Kudu.
My question is this: How can I set up this entire process so that new builds REPLACE old builds on web app, and avoid them instead being nested inside the old build?

How to use environment variables in Github Page?

I want to deploy my create-react-app project to GitHub Pages. But I have a few secret keys. How can I manage these keys inside my React app?
Edited June 2020
Reference #alicia-jasmine
"React is purely a front-end framework. Everything accessible to React (even if you embed it through a build step) will later be visible in the front-end code and someone relatively basic to find. To really keep them secret you MUST have something server side!"
The following answer will actually expose the key in the gh-page branch on GitHub, also the keys will be accessible through the network tab in the developer console.
Original Answer
I'm also using create-react-app, and I found that this can be done by customizing your CI script with GitHub secret settings. (After the setting, you can use environment variables like this in your project.)
const apiKey = process.env.REACT_APP_APIKey
const apiSecret = process.env.REACT_APP_APISecret
To add a secret to your repository, go to your repository's Setting > Secrets, click on Add a new secret. In the screenshot below, I added 2 env variables: REACT_APP_APIKey and REACT_APP_APISecret.
Notice: All the environment variable you want to access with create-react-app need to be prefixed with REACT_APP.
After you have your secret ready, you can take a look at this post, it's about how to add your own Action upon push.
To setup your action script, go to your repository > Actions, an click on Setup workflow your self, and paste in the script provided in the post or take a look at mine script below.
I use the following script to access the 2 environment variables I set on GitHub secret. (You can access the secret you set in the script by ${{ secrets.REACT_APP_APIKey }}.)
name: CI
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v1
- name: Build
run: |
npm install
npm run-script build
env:
REACT_APP_APIKey: ${{ secrets.REACT_APP_APIKey }}
REACT_APP_APISecret: ${{ secrets.REACT_APP_APISecret }}
- name: Deploy
uses: JamesIves/github-pages-deploy-action#releases/v3
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: gh-pages
FOLDER: build
After you setup the script, the action will be triggered by any push to master
branch. After you push any commits, you can take a look at the deployment status at actions status.
You can see how hard it is for me to figure it out... so many fail attempts lol. Anyway, hope this will help :)
name: Deploy to GitHub Pages
on:
push:
branches:
- master
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v1
- name: Build
run: |
npm install
npm run-script build
env:
REACT_APP_INSTAGRAM_ACCESS_TOKEN: ${{ secrets.REACT_APP_INSTAGRAM_ACCESS_TOKEN }}
REACT_APP_SMTP_SECURE_TOKEN: ${{ secrets.REACT_APP_SMTP_SECURE_TOKEN }}
- name: Deploy
uses: JamesIves/github-pages-deploy-action#releases/v3
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN_KEY }}
BRANCH: gh-pages
FOLDER: dist
You can use like this to add your environment variables from GitHub secrets. This solved my problem.
I support this answer (above)
But I advice to update the gh-pages YML to version 4
Also take a look at environment variables solution because i spent hours to find out the solution
- name: Deploy
uses: JamesIves/github-pages-deploy-action#4.0.0
with:
branch: gh-pages
folder: front-app/dist
To use environment variables, the general approach which is followed is to:
Not expose them to the public
Keep it local at the time of development/production and ignore in .gitignore file.
Make the
static build out of your application Then deploy it to either github
pages or any other static website host.
While working with create-react-app you have their benefits, you can create .env in your root folder.
The structure for the .env file should follow below key-value structure:-
REACT_APP_SECRET_CODE1=dev123
REACT_APP_SECRET_CODE2=prod456
Keys in the file should be prefixed with REACT_APP and you can use these keys to access the variable in your application. For eg. process.env.REACT_APP_SECRET_CODE, this will have the value dev123
If they are truly secret, and so should not be in a repository, then there isn't a way to manage that with github-pages.
If you are okay with having them in a repository, then put them in .env and access via process.env
You can deploy your project on Heroku where you can set up your secret key.

Resources