javamavengoogle-app-enginejava-11app.yaml

Variable substitution in Java App Engine 11 project's app.yaml


We've got several Java App Engine Standard projects that I am migrating from App Engine 8 to App Engine 11 on Spring Boot. A number of them use a vpc-access-connector in their appengine-web.xml file, which we inject using resource filtering during the maven-war-plugin. The new way to configure the app engine is to use a src/main/appengine/app.yaml file and a jar instead of a war as the artifact, however I have been unable to determine how to do variable substitution into the app.yaml file. I have tried including this is my maven section, but it did not work:

<resources>
  <resource>
    <directory>src/main/appengine</directory>
    <filtering>true</filtering>
  </resource>
</resources>

I need to inject different values based on the maven profile being run. We currently define vpc access connector values in maven properties sections within profiles aligned with our spring profiles for each of our deployment environments, like test, ea, and prod.

What is the solution here?

Is there some way to use different app.yaml files based on which profile is being invoked? (This is not the best solution, as it would mean most of the file content would be static copies, though the common stuff could be moved to an included yaml.)

Or is there some syntax I can't locate online that will allow me to set an environment variable or maven property and refer to it from within the src/main/appengine/app.yaml file?

Thanks for any assistance!

Tom Harris


Solution

  • This functionality doesn't exist, but there's a way to achieve your desired output.

    YAML is a standard format with many libraries for both reading and writing. It is also a text file, so a simple solution could simply copy a template and replace any placeholder the you want with the appropriate value.

    Having been said that, I tried creating a sample app to inject my defined values on the maven profile.

    1. I added a profiles in my pom.xml file after I added your "resources" code snippet under my build in the pom.xml:
    <profiles>
        <profile>
            <id>Test_ID</id>
            <properties>
                <appengine.vpc-access-connector>
                    projects/PROJECT_ID/locations/REGION/connectors/CONNECTOR_NAME
                </appengine.vpc-access-connector>
            </properties>
        </profile>
    </profiles> 
    
    1. Under my app.yaml file under "/src/main/appengine/app.yaml", here's what I did:
    vpc_access_connector: 
      name: "${appengine.vpc-access-connector}"
      egress_setting: all-traffic
    
    1. Lastly, I tried running it using the Maven Filtering command: $ mvn resources:resources -P Test_ID, and got a new app.yaml file under "/target/classes/app.yaml" that has this output:
    vpc_access_connector: 
      name: "projects/PROJECT_ID/locations/REGION/connectors/CONNECTOR_NAME"
      egress_setting: all-traffic
    

    For deploying your generated app.yaml file, run this command that was also in the documentation: gcloud app deploy --appyaml=target/classes/app.yaml

    There's also an available guide to further understand your migrating options to Java App Engine 11.