Skip to content

Commit d622871

Browse files
committed
#11: Configuring environment variables for Vue frontend. Also documented Heroku automatic deployment and pipeline.
1 parent 72cddbb commit d622871

File tree

7 files changed

+57
-3
lines changed

7 files changed

+57
-3
lines changed

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,50 @@ See some elements, when you go to http://localhost:8080/#/bootstrap/ - this shou
402402
A good discussion about various UI component frameworks: http://vuetips.com/bootstrap
403403

404404

405+
## Heroku Deployment
406+
407+
As you may already read, the app is automatically deployed to Heroku on https://spring-boot-vuejs.herokuapp.com/.
408+
409+
The project makes use of the nice Heroku Pipelines feature, where we do get a full Continuous Delivery pipeline with nearly no effort:
410+
411+
![heroku-pipeline](heroku-pipeline.png)
412+
413+
And with the help of super cool `Automatic deploys`, we have our TravisCI build our app after every push to master - and with the checkbox set to `Wait for CI to pass before deploy` - the app gets also automatically deployed to Heroku - but only, if the TravisCI (and Coveralls...) build succeeded:
414+
415+
![heroku-automatic-deploys](heroku-automatic-deploys.png)
416+
417+
You only have to connect your Heroku app to GitHub, activate Automatic deploys and set the named checkbox. That´s everything!
418+
419+
420+
#### Accessing correct Heroku Port in Vue.js frontend
421+
422+
Frontend needs to know the Port of our Spring Boot backend API, which is [automatically set by Heroku every time, we (re-)start our App](https://stackoverflow.com/a/12023039/4964553).
423+
424+
> You can [try out your Heroku app locally](https://devcenter.heroku.com/articles/heroku-local)! Just create a .env-File with all your Environment variables and run `heroku local`!
425+
426+
To access the Heroku set port, we need to configure the used port inside our Vue.js application dynamically instead of hard-coded. This can be easily achieved with the help of enviroment variables, which can be added to the [dev.env.js](https://github.com/jonashackt/spring-boot-vuejs/blob/master/frontend/config/dev.env.js) and [prod.env.js](https://github.com/jonashackt/spring-boot-vuejs/blob/master/frontend/config/prod.env.js) files. Let´s do this with the variable `API_PORT`:
427+
428+
```
429+
module.exports = {
430+
NODE_ENV: '"production"',
431+
API_PORT: '"8077"'
432+
}
433+
```
434+
435+
Now we can access this variable inside our Vue.js application. Just access the variable inside the `<script>` area inside a method:
436+
437+
```
438+
logPortEnvVariable () {
439+
console.log('Now what´s the port?')
440+
console.log(process.env.API_PORT)
441+
}
442+
```
443+
444+
If you want, have a look at [Service.vue](https://github.com/jonashackt/spring-boot-vuejs/blob/master/frontend/src/components/Service.vue), where this is implemented.
445+
446+
447+
448+
405449
# Links
406450

407451
Nice introdutory video: https://www.youtube.com/watch?v=z6hQqgvGI4Y

frontend/config/dev.env.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ var merge = require('webpack-merge')
22
var prodEnv = require('./prod.env')
33

44
module.exports = merge(prodEnv, {
5-
NODE_ENV: '"development"'
5+
NODE_ENV: '"development"',
6+
API_PORT: '"process.env.PORT"'
67
})

frontend/config/prod.env.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
2-
NODE_ENV: '"production"'
2+
NODE_ENV: '"production"',
3+
API_PORT: '"process.env.PORT"'
34
}

frontend/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
1212
"e2e": "node test/e2e/runner.js",
1313
"test": "npm run unit && npm run e2e",
14-
"lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs"
14+
"lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs",
15+
"postinstall": "npm run build"
1516
},
1617
"dependencies": {
1718
"axios": "^0.16.2",

frontend/src/components/Service.vue

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
errors: []
2525
}
2626
},
27+
created () {
28+
this.logPortEnvVariable()
29+
},
2730
methods: {
2831
// Fetches posts when the component is created.
2932
callRestService () {
@@ -36,6 +39,10 @@
3639
.catch(e => {
3740
this.errors.push(e)
3841
})
42+
},
43+
logPortEnvVariable () {
44+
console.log('Now what´s the port?')
45+
console.log(process.env.API_PORT)
3946
}
4047
}
4148
}

heroku-automatic-deploys.png

367 KB
Loading

heroku-pipeline.png

209 KB
Loading

0 commit comments

Comments
 (0)