Gradle: Task for deploying to appengine with testing configuration? - google-app-engine

I have a gradle project with a very simple Google App Engine configuration:
apply plugin: 'com.google.cloud.tools.appengine'
// ...
appengine {
deploy {
stopPreviousVersion = true
promote = true
project = 'my-awesome-project'
}
run {
port = 3000
}
}
This configures the app engine gradle tasks, e.g. appengineDeploy.
What I would like to do is to create another gradle task named appengineDeployTesting which does exactly the same thing as appengineDeploy, except that it uses a different project name (e.g. my-awesome-project-testing).
Ultimately, I want to end up with two tasks:
appengineDeploy to deploy to the production instance
appengineDeployTesting to deploy to the testing instance
How would you do this in gradle? I know how to create a task dependency, but this case requires more than just a dependency.

First option
Well, if you only want to change the project where you are deploying you can do a simple condition check in Gradle, just before the appengine { ... } line, for example:
if (project.hasProperty('testProject')) {
appengine.deploy.project = "my-awesome-project"
}else{
appengine.deploy.project = "main-project"
}
Remember to remove the tag "project" inside the "appengine.deploy" field, so you don't overwrite the project name, for example:
appengine {
deploy {
stopPreviousVersion = true
promote = true
}
}
To deploy the application to your test project, you can just add the flag:
gradle appengineDeploy -PnewProject
And to deploy to the main project, just remove it:
gradle appengineDeploy
What you were asking was to create your own plugin in order to create a specific command to deploy to a specific project, you can follow the official documentation on how you can do it, however I think this solution is way simpler.
Second option
To avoid using flags, you can add the following code in the build.gradle file, after the appengine {...} structure:
task appengineDeployTesting {
doLast{
appengine.deploy.project = "my-awesome-project"
}
}
appengineDeployTesting.finalizedBy appengineDeploy
Then running the command:
gradle appengineDeployTesting
Will first run the appengineDeployTesting task, and once finished will set the new project name and run the appengineDeploy task to deploy.

Related

How to use Prisma with Electron

I am using Prisma (sqlite DB) with an Electron + Angular app
Everything works fine until I try to create a record
I'm getting this error in console few times repeated
Here is a part of my schema.prisma file:
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
binaryTargets = ["native","darwin","debian-openssl-1.1.x","linux-musl"]
}
I've tried using the same Prisma config with a scratch TS project and it works fine
When I've tried it with electron I was getting errors
As I understand it cannot find query engine binaries, but I don't know how to say to Electron where to get them from
Hi
I too had similar problem with prisma.
The issue is that the custom prisma client along with the downloaded binaries for the platform of use gets generated in node_modules/.prisma folder (default).
when webpack bundled the code, .prisma folder wasn't included with the generated app.asar package in node_modules folder and thus prisma client couldn't be loaded along with the binaries.
Solution
I changed the output path of the generated prisma client as per prisma doc
generator client {
provider = "prisma-client-js"
output = "../src/main/database/generated/client"
}
and included in my database.js file (located inside database folder) as follows
import { PrismaClient } from './generated/client';
As the downloaded binaries are also placed indside the output folder, there was no problem for prisma client in finding it.
I had the same issue when using electron-builder to build native binaries.
In my case I had to add .env file to the build block on package.json and change the output path as spc mentioned.
// package.json
{
...
"build": {
...
"files": [
...
".env"
],
...
}
}
// schema.prisma
generator client {
provider = "prisma-client-js"
output = "../electron/client"
binaryTargets = ["native"]
}

Specify Next.js build directory in build command?

Originally, I had my distDir set to a folder within my Firebase Functions directory for deployment, however, the duplication of React when running the app in dev mode led to some errors, so I've had to comment it out.
Is there a way to specify the next build commands output directory within the command? Like next build dir='../functions/next'.
Or is there a way to separate dev builds directory (from the next command) from the production builds (next build) within the config?
I found following solution for this problem:
You can set custom environment variable that decides where your build will be generated:
BUILD_DIR=myBuilds/buildXYZ yarn build
OR
BUILD_DIR=myBuilds/buildXYZ npm build
In the next.config.js you can use your environment variable like this:
module.exports = {
distDir: process.ENV.BUILD_DIR || '.next',
}
Now your build will be dynamically generated in the myBuilds/buildXYZ directory defined in the BUILD_DIR variable.
One thing to notice: If you want to use the next export command (or any other command using the build directory), you should also add the environment variable before it, e.g.:
BUILD_DIR=myBuilds/buildXYZ yarn export
According to https://github.com/zeit/next.js/issues/1513, this may need you to update to the next version. But it looks like they added a feature to next.config.js file. All you have to do is add the destination for your dist:
module.exports = {
options: {
dist: '.next'
}
}
Haven't tested it, but it was successfully merged to master and marked as resolved.

How to authenticate a private Go Module using go 1.11 and Google App Engine Standard

I've been updating my entire go gae standard project to use go 1.11's modules.
Main directory structure
app.yaml
app.go
go.mod
go.sum
app.go
package main
import "bitbucket.org/myPrivateRepo"
func main() {
myImportantModule.Run()
}
go.mod
module myProject
require bitbucket.org/myPrivateRepo v0.0.1
The Error
If I try to gcloud app deploy:
ERROR: (gcloud.app.deploy) Error Response: [9] Cloud build <GUI>
status: FAILURE.
Build error details: go: bitbucket.org/myPrivateRepo#v0.0.1:
https://api.bitbucket.org/2.0/repositories/myPrivateRepo?fields=scm:
403 Forbidden
(Note: obviously the repo I'm using has a real name).
So can I do it this way? I'll admit to not fully understanding the migration documentation, particularly when it talked about "Moving files to your GOPATH". https://cloud.google.com/appengine/docs/standard/go111/go-differences
I mean, I thought one of the benefits of the new module system is that you don't need everything under the go path. When I read https://github.com/golang/go/wiki/Modules for example, it very early on says "Create a directory outside of your GOPATH:"
So, to be clear, right now all of my code is outside the go path, but everything builds locally just fine.
I think it all works becausego automatically downloads and caches things within the go path when I run go mod tidy / go build etc.
Yet it fails when I try to gcloud app deploy. How would the google cloud build system ever have access to my private repositories anyway? I'm obviously missing something important. I also read you are not supposed to combine vendoring with the new module system so that can't be it.
I will be very happy if this works, as using DEP forced me to use goapp deploy very awkwardly.
Thanks!
UPDATE: Google has some better documentation now that go 1.14 is out: https://cloud.google.com/appengine/docs/standard/go/specifying-dependencies
My solution:
Instead of dealing with credentials, I'm using go's module replace functionality to point GAE to use my local code. This is working well.
Directory structure:
myService/
src/
service.go // has a run() function to set up routers etc.
go.mod // depends on my private module in bitbucket and other things
… // other source files
build/
gae/
src/ // simlink to ../../src
modules/ // git ignored, I clone or copy my modules in build scripts.
app.go // see below…
go.mod // has main() which calls service.run() and appEngine.Main()
app.yaml
Method
I use git module replace so that GAE uses my local code. Before building I parse myService/src/go.mod to find the correct version of my private module, then I clone it into the modules folder. I also made an option to copy wip module source code for debugging locally without committing to my module repositories.
go.mod from gae directory:
module myServiceGAE
require (
bitbucket.org/me/myService v0.0.0
google.golang.org/appengine v1.4.0
)
replace bitbucket.org/me/myService => ./src
replace bitbucket.org/me/myModule => ./modules/utils
Pros
The package under myService has no references or knowledge of GAE, so I can easily build it into a docker etc. I think parsing the service go.mod files would be like creating my own dependency manager, defeating the benefits of go modules.
Cons
If I had a private module which depended on another private module, I think things would get too complicated.
Another alternative is to also use Google Cloud Secret Manager
https://cloud.google.com/cloud-build/docs/access-private-github-repos
Google Cloud will have an SSH key to access and pull your private repository.
Set git credentials before deploying:
git config credential.helper '!f() { sleep 1; echo "username=${GIT_USER}\npassword=${GIT_PASSWORD}"; }; f'
export GIT_USER=put_git_user_here
export GIT_PASSWORD=put_git_password_here
gcloud app deploy

Google App Engine with Firebase - can't add dependencies

I'm trying to connect a backend module in Android Studio to Firebase using the tutorial here - https://cloud.google.com/solutions/mobile/firebase-app-engine-android-studio#adding_dependencies_to_the_backend_module
I cannot add the dependencies for some reason. I can't find them on the list. If I try to add them manually, I get a Gradle sync error.
For the main app module, Firebase is connected and it's functioning correctly. I set this up using Android Studio's build in Firebase tool. I've tried using this tool again but it's made no difference.
Here's my build.gradle for the backend:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.google.appengine:gradle-appengine-plugin:1.9.42'
}
}
repositories {
jcenter();
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.42'
compile 'javax.servlet:servlet-api:2.5'
compile 'com.google.appengine:appengine-api-1.0-sdk'
compile 'com.google.firebase:firebase-server-sdk'
compile 'org.apache.httpcomponents:httpclient'
}
appengine {
downloadSdk = true
appcfg {
oauth2 = true
}
}
Here's the error I get if I add the dependencies manually, as shown in the code above:
Click here
You're probably getting errors on these dependencies. They don't have versions specified:
compile 'com.google.appengine:appengine-api-1.0-sdk'
compile 'com.google.firebase:firebase-server-sdk'
compile 'org.apache.httpcomponents:httpclient'
Every maven dependency requires a group id, artifact id, and a version string.
Also, the "firebase-server-sdk" dependency doesn't make sense to me. Access to firebase backend services is now done through the Admin SDK.
My guess is that the tutorial you linked to is simply very much out of date, and you can no longer follow it exactly, but maybe you can still learn something from it otherwise.

How to build Yeoman Backbone without uglifying?

I'm using Yeoman 1.0 and the latest version of the Backbone generator and everything I've written is working in development, however when I $ grunt build the resulting /dist is throwing the error:
Uncaught ReferenceError: Backbone is not defined
Unfortunately, since the js has been minified, trying to debug is almost pointless. Is there a way to "build" without the minification? I've tried playing around with settings in the gruntfile but can't seem to find the right options for the right task.
Can you share your GruntFile.js to check the configurations?
By the way, you can omit the minification, don't use uglify or whatever task you have been running so, give it a try to grunt-contrib-copy task which copies the files to designated folder (prod/dist).
https://github.com/gruntjs/grunt-contrib-copy
// Configuration goes here
grunt.initConfig({
// Configure the copy task to move files from the development to production folders
copy: {
target: {
files: {
'prod/': ['dev/**']
}
}
},
});

Resources