Skip to content

Commit 993bc71

Browse files
authored
Heroku deploy (jhipster#561)
Deploy generated app in Heroku Fix jhipster#389
1 parent 5844d76 commit 993bc71

File tree

28 files changed

+1033
-682
lines changed

28 files changed

+1033
-682
lines changed

docs/Features/heroku.md

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,58 @@
1-
# Heroku Deploy
2-
This sub-generator initializes a Heroku [.Net 5](https://docs.microsoft.com/pt-br/dotnet/core/dotnet-five) app that is ready to push to Heroku.
1+
# Deploying to Heroku
2+
3+
This sub-generator allows the deployment of your JHipster .Net application to Heroku cloud.
4+
5+
The Heroku sub-generator will always use free tiers/options. Nevertheless installing addons needs a properly verified Heroku account.
6+
7+
Therefore to avoid any unexpected build failures, we would recommend verifying your Heroku account before starting this sub-generator.
38

49
## Pre-requisites
10+
511
Before running the sub-generator, you must install the [Heroku CLI](https://cli.heroku.com/).
6-
You must also create a Heroku account and run:
12+
13+
Make sure that you are logged into Heroku.
714
```bash
815
heroku login
916
```
17+
1018
[Git](https://git-scm.com/) is also required to deploy to Heroku.
1119

20+
Also make sure you have a working [Docker](https://docs.docker.com) installation (eg. `docker ps`) if deploying to Heroku Container Registry.
21+
1222
## Deploying to Heroku
13-
To deploy your .Net 5 application to Heroku, run this command:
23+
24+
To deploy your application to Heroku, run the following command:
1425

1526
```bash
1627
jhipster heroku
17-
git push heroku main
1828
```
1929

20-
This will initialize a new Heroku app and git push your application to Heroku.
21-
Free tier dynos and database addons are used by the generator. A verified Heroku account might be needed to add some of the resources.
30+
## Databases
31+
32+
- Mysql ✔
33+
- Postgres ✔
34+
- MSSQL ✔ (requires a manual step described below)
2235

23-
This Heroku sub-generator is similar to its Java counterpart. Access the documentation at (https://www.jhipster.tech/heroku/). It might provide helpful insight. But keep in mind that there are some differences between both implementations of the Heroku sub-generator.
36+
Currently MySql and PostgreSQL database addons are free and automatically added by this sub-generator when deploying to Heroku. Even though those resouces are free, an accound with verified credit card is required to add them.
2437

25-
## Databases
38+
Heroku's [MSSQL addon](https://elements.heroku.com/addons/mssql) is not free of cost and to avoid unexpected costs It will not be provisioned automatically when deploying to Heroku.
39+
40+
Please visit [the MSSQL addon page](https://elements.heroku.com/addons/mssql), review the pricing and add the MSSQL addon to your account with the following command:
41+
```bash
42+
heroku addons:create mssql:REPLACE_PLAN_NAME --as DATABASE --app REPLACE_YOUR_APP_NAME
43+
```
2644

27-
Currently MySql and PostgreSQL are supported and automatically added by the sub-generator when deploying to Heroku. [JawsDB MySQL addon](https://elements.heroku.com/addons/jawsdb) and [Heroku Postgres addon](https://elements.heroku.com/addons/heroku-postgresql) are added to the Heroku app using the free tier.
45+
## Oauth2
2846

29-
Since the [MSSQL addon](https://elements.heroku.com/addons/mssql) is not free it requires manual provisioning. This sub-generator provides instructions on how to manually install the MSSQL addon.
47+
For applications that use Oauth2 the following manual steps are required:
3048

31-
Heroku creates an environment variable named `DATABASE_URL` when using database addons. It contains the following structure: `dbType://user:password@server-url:db-port/db-name`. If `DATABASE_URL` is present the database credentials are parsed at `DatabaseConfiguration.cs` to create a properly formed connection string. And it takes precedence over existing connection strings.
49+
1. Deploy the application to Heroku using `jhipster heroku`.
50+
2. Access your (Heroku Dashboard)[https://dashboard.heroku.com/] and select the app you have just created.
51+
3. Under `Installed add-ons` okta should be already installed.
52+
4. Click on `okta` to open the addon's Dashboard.
53+
5. To configure it follow the documentation (here)[https://jhipsternet.readthedocs.io/en/latest/Features/security.html#okta]
3254

33-
**Notes:**
34-
The [heroku/nodejs buildpack](https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-nodejs) is also added to the Heroku app. It is a pre-requisite to build Angular/React client applications.
3555

36-
A `package.json` is automatically added to the root folder as It's required by the heroku/nodejs buildpack, otherwise the application deploy will fail.
56+
## Limitations
3757

38-
[Jincod's Heroku .NET Core buildpack](https://github.com/jincod/dotnetcore-buildpack) is used to deploy this .Net 5 application to Heroku.
58+
Only monolithic deploys are supported at the moment.

generators/client/prompts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
function askForClient() {
3232
if (this.existingProject) return;
3333

34-
var choices = [
34+
const choices = [
3535
{
3636
value: ANGULAR,
3737
name: 'Angular',
@@ -53,7 +53,7 @@ function askForClient() {
5353
name: 'No client',
5454
},
5555
];
56-
56+
5757
if (this.configOptions.isDebugEnabled) {
5858
choices.push(
5959
{

generators/common/files.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,25 @@ const files = {
2323
{
2424
templates: [{ file: 'Dockerfile-Back', renameTo: () => 'Dockerfile-Back' }],
2525
},
26+
{
27+
templates: [{ file: 'docker-entrypoint-back.sh', renameTo: () => 'docker-entrypoint-back.sh' }],
28+
},
2629
{
2730
condition: generator => generator.clientFramework === constants.BLAZOR,
2831
templates: [{ file: 'Dockerfile-Front', renameTo: () => 'Dockerfile-Front' }],
2932
},
33+
{
34+
condition: generator => generator.clientFramework === constants.BLAZOR,
35+
templates: [{ file: 'docker-entrypoint-front.sh', renameTo: () => 'docker-entrypoint-front.sh' }],
36+
},
3037
{
3138
condition: generator => generator.clientFramework === constants.BLAZOR,
3239
templates: [{ file: 'nginx.conf', renameTo: () => 'nginx.conf' }],
3340
},
41+
{
42+
condition: generator => generator.clientFramework === constants.BLAZOR,
43+
templates: [{ file: 'default.conf', renameTo: () => 'default.conf' }],
44+
},
3445
{
3546
templates: [{ file: 'dockerignore', renameTo: () => '.dockerignore', method: 'copy' }],
3647
},

generators/common/templates/dotnetcore/Dockerfile-Back.ejs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,21 @@ RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - && \
2323
apt-get install -yq nodejs && \
2424
rm -rf /var/lib/apt/lists/*
2525

26+
<%_ if(!skipClient && clientFramework === "Blazor") { _%>
27+
# install blazor build tools only if INCLUDE_BLAZOR is set to true
28+
ARG INCLUDE_BLAZOR=false
29+
RUN if [ "$INCLUDE_BLAZOR" = "true" ]; then dotnet tool install -g Microsoft.Web.LibraryManager.Cli ; fi
30+
RUN if [ "$INCLUDE_BLAZOR" = "true" ]; then dotnet tool install -g Excubo.WebCompiler ; fi
31+
ENV PATH="${PATH}:/root/.dotnet/tools"
32+
<%_ } _%>
2633
# Restore dependencies of .net core projects taking advantage of docker layer caching
2734
COPY src/*/*.csproj ./src/
2835
COPY Directory.Build.targets Directory.Build.targets
2936
RUN for file in $(ls src/*.csproj); do mkdir -p ${file%.*} && mv $file ${file%.*}; done
37+
<%_ if(!skipClient && clientFramework === "Blazor") { _%>
38+
COPY src/client/*/*.csproj ./src/client/
39+
RUN for file in $(ls src/client/*.csproj); do mkdir -p ${file%.*} && mv $file ${file%.*}; done
40+
<%_ } _%>
3041
RUN dotnet restore "src/<%= mainProjectDir %>/<%= pascalizedBaseName %>.csproj"
3142
3243
# Copy everything else and build app
@@ -36,10 +47,16 @@ RUN dotnet publish "<%= pascalizedBaseName %>.csproj" -c Release -o /app/out
3647
3748
# Final stage/image
3849
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS runtime
39-
WORKDIR /app
4050
EXPOSE 80
51+
WORKDIR /app
52+
COPY docker-entrypoint-back.sh .
53+
RUN chmod +x /app/docker-entrypoint-back.sh
4154
COPY --from=build /app/out .
42-
4355
ENV ASPNETCORE_ENVIRONMENT=Production
56+
<%_ if(!skipClient && clientFramework === "Blazor") { _%>
57+
ARG INCLUDE_BLAZOR
58+
ENV INCLUDE_BLAZOR $INCLUDE_BLAZOR
59+
ENV ServerUrl="https://localhost:8080"
60+
<%_ } _%>
4461
45-
ENTRYPOINT ["dotnet", "<%= kebabCasedBaseName %>.dll"]
62+
ENTRYPOINT ["./docker-entrypoint-back.sh"]

generators/common/templates/dotnetcore/Dockerfile-Front.ejs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ RUN dotnet publish "<%= pascalizedBaseName %>.Client.csproj" -c Release -o /app/
2121

2222
FROM nginx:alpine AS final
2323
WORKDIR /usr/share/nginx/html
24+
ENV PORT 80
25+
ENV ServerUrl="https://localhost:8080"
2426
COPY --from=build /app/out/wwwroot .
25-
COPY ./nginx.conf /etc/nginx/nginx.conf
26-
ARG ServerUrl="https://localhost:8080"
27-
RUN sed -i "/ServerUrl/c\ "\"ServerUrl"\" : "\"$ServerUrl"\"," appsettings.json
27+
COPY ./nginx.conf /etc/nginx/
28+
COPY ./default.conf /etc/nginx/conf.d/
29+
COPY ./docker-entrypoint-front.sh /docker-entrypoint.d/

generators/common/templates/dotnetcore/README.md.ejs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ This application was generated using JHipster <%= jhipsterVersion %> and JHipste
2828
2929
Before you can build this project, you need to verify if [libman](https://github.com/aspnet/LibraryManager) and [webcompiler](https://github.com/excubo-ag/WebCompiler) are installed. (If the application is generated, the generator installed this tools for you)
3030
31-
If not, run
31+
If not, run
3232
3333
```bash
3434
dotnet tool install -g Microsoft.Web.LibraryManager.Cli
35-
dotnet tool install -g Excubo.WebCompiler
35+
dotnet tool install -g Excubo.WebCompiler
3636
```
3737
3838
<%_ } _%>
@@ -54,11 +54,11 @@ dotnet run --verbosity normal --project ./src/client/<%= pascalizedBaseName %>.C
5454
5555
You can also use the **hotreload**
5656
57-
For the backend
57+
For the backend
5858
```bash
5959
dotnet watch --project ./src/<%= pascalizedBaseName %>/<%= pascalizedBaseName %>.csproj run --verbosity normal
6060
```
61-
For the frontend
61+
For the frontend
6262
```bash
6363
dotnet watch --project ./src/client/<%= pascalizedBaseName %>.Client/<%= pascalizedBaseName %>.Client.csproj run --verbosity normal
6464
```
@@ -167,9 +167,9 @@ In ClientApp folder run :
167167
168168
<%_ } _%>
169169
170-
### Generate entities
170+
### Generate entities
171171
172-
With cli
172+
With cli
173173
```bash
174174
jhipster entity <entity-name>
175175
```
@@ -226,7 +226,7 @@ docker build -f ./Dockerfile-Back -t <%= baseName.toLowerCase() %> .
226226
<%_ if (!skipClient && clientFramework === "Blazor") { _%>
227227
228228
```bash
229-
docker build -f ./Dockerfile-Front -t <%= baseName.toLowerCase() %>-front . --build-arg ServerUrl=https://localhost:8080
229+
docker build -f ./Dockerfile-Front -t <%= baseName.toLowerCase() %>-front .
230230
```
231231
232232
<%_ } _%>
@@ -240,12 +240,12 @@ docker run -p 8080:80 <%= baseName.toLowerCase() %>
240240
<%_ if (!skipClient && clientFramework === "Blazor") { _%>
241241
242242
```bash
243-
docker run -p 8081:80 <%= baseName.toLowerCase() %>-front
243+
docker run -p 8081:80 -e ServerUrl=https://localhost:8080 <%= baseName.toLowerCase() %>-front
244244
```
245245
246246
<%_ } _%>
247247
248-
Or you can simply run :
248+
Or you can simply run :
249249
250250
```bash
251251
docker-compose -f .\docker\app.yml build
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
server {
2+
listen 80;
3+
4+
location / {
5+
root /usr/share/nginx/html;
6+
try_files $uri $uri/ /index.html =404;
7+
}
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<%#
2+
Copyright 2019-2022 the original author or authors from the JHipster project.
3+
This file is part of the JHipster project, see https://www.jhipster.tech/
4+
for more information.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-%>
15+
#!/bin/bash
16+
17+
echo "Starting the application..."
18+
19+
# if $PORT is set in the environment, set ASPNETCORE_URLS to "http://*:$PORT"
20+
if [ -n "$PORT" ]; then
21+
echo "Setting ASPNETCORE_URLS with the PORT environment variable..."
22+
export ASPNETCORE_URLS=http://*:$PORT
23+
echo "ASPNETCORE_URLS: $ASPNETCORE_URLS"
24+
fi
25+
26+
if [ "$INCLUDE_BLAZOR" = "true" ]; then
27+
# replace blazor server url if INCLUDE_BLAZOR is true
28+
echo "Replacing blazor server url in appsettings.json with the ServerUrl environment variable..."
29+
sed -i "/ServerUrl/c\ "\"ServerUrl"\" : "\"$ServerUrl"\"," wwwroot/appsettings.json
30+
fi
31+
32+
# run the dotnet app
33+
dotnet <%= pascalizedBaseName %>.dll
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<%#
2+
Copyright 2019-2022 the original author or authors from the JHipster project.
3+
This file is part of the JHipster project, see https://www.jhipster.tech/
4+
for more information.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-%>
15+
#!/bin/sh
16+
17+
echo "Replacing the backend/api in appsettings.json with the ServerUrl environment variable..."
18+
sed -i "/ServerUrl/c\ "\"ServerUrl"\" : "\"$ServerUrl"\"," appsettings.json
19+
20+
echo "Replacing the listen port in default.conf with the PORT environment variable..."
21+
sed -i "s/listen.*/listen $PORT;/" /etc/nginx/conf.d/default.conf

generators/common/templates/dotnetcore/dockerignore.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ README.md
1717
sonar-project.properties
1818
**/node_modules
1919
**/Dockerfile
20+
**/ClientApp/target/

0 commit comments

Comments
 (0)