-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: draft 2nd blog post project automation (#4)
- Loading branch information
Showing
11 changed files
with
272 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
NODE_ENV=development | ||
BASE_URL=http://localhost:3000 | ||
DISCORD_CLIENT_ID=12345678 | ||
DISCORD_SECRET=XXXXXXXXXXXXXXXXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: cd | ||
|
||
on: | ||
push: | ||
branches: | ||
- release | ||
|
||
jobs: | ||
release: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Version Bump | ||
uses: phips28/gh-action-bump-version@master | ||
with: | ||
skip-tag: true | ||
minor-wording: add:,minor,feat | ||
|
||
- name: Build Changelog message | ||
id: changelog | ||
uses: scottbrenner/generate-changelog-action@master | ||
env: | ||
REPO: ${{ github.repository }} | ||
|
||
- name: Create Github Release | ||
id: create_release | ||
uses: actions/create-release@latest | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
tag_name: v${{ github.ref }} | ||
release_name: Release ${{ github.ref }} | ||
body: | | ||
${{ steps.changelog.outputs.changelog }} | ||
draft: false | ||
prerelease: false | ||
|
||
- name: Deploy to Heroku | ||
uses: akhileshns/heroku-deploy@v3.4.6 | ||
with: | ||
heroku_api_key: ${{ secrets.HEROKU_API_KEY }} | ||
heroku_app_name: ${{ secrets.HEROKU_APP_NAME }} | ||
heroku_email: ${{ secrets.HEROKU_EMAIL }} | ||
healthcheck: "https://${{ secrets.HEROKU_APP_NAME }}.herokuapp.com/health" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: ci | ||
|
||
on: | ||
pull_request: | ||
|
||
env: | ||
CI: true | ||
|
||
jobs: | ||
test: | ||
runs-on: ${{ matrix.os }} | ||
|
||
strategy: | ||
matrix: | ||
node-version: [10.x, 12.x, 14.x] | ||
os: [ubuntu-latest] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Use Node.js | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
|
||
- name: Install | ||
run: npm install --ignore-scripts | ||
- name: Run tests | ||
run: npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
import Fastify from 'fastify' | ||
import app from './app.mjs' | ||
|
||
const server = Fastify({ | ||
logger: true, | ||
pluginTimeout: 10000 | ||
}) | ||
|
||
server.register(app) | ||
|
||
server.listen(process.env.PORT || 3000, '0.0.0.0', (err) => { | ||
if (err) { | ||
server.log.error(err) | ||
process.exit(1) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# Project Automation | ||
|
||
This tutorial is the following of the "[A Discord app with Fastify!](https://dev.to/eomm/a-discord-app-with-fastify-3h8c)". | ||
|
||
## Road to ES Import | ||
|
||
Node.js used the CommonJS module system (CJS) since the beginning and recently it has added support to ECMAScript Modules (ESM) | ||
([without the `--experimental-modules` flag](https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V12.md#ecmascript-modules-----experimental-modules-flag-removal)). | ||
|
||
So to update this project to the ESM module there are many possibilities, described in [this article](https://medium.com/@nodejs/announcing-core-node-js-support-for-ecmascript-modules-c5d6dc29b663) by Node.js Module Team. | ||
|
||
I will follow the one that makes sense to me to a project written in CJS as first the implementation: | ||
|
||
- add `"type": "module"` in the `package.json` | ||
- rename the `js` files to the `mjs` extension | ||
- fix the `__dirname` usage since it is not supported in ESM | ||
- remove all the `require` in favour of `import` | ||
|
||
Note that it is mandatory adding the file extension to local files import: | ||
|
||
```js | ||
import authRoutes from './auth.js' | ||
``` | ||
|
||
- remove `'use strict'` since it is the default behaviour with ESM | ||
- update the `module.exports` to `export default function app (fastify, opts, next) {..` | ||
- fix the start script since `fastify-cli` doesn't support the ESM loading right now | ||
|
||
|
||
## CI/CD | ||
|
||
Adding CI/CD to the project is quite simple thanks to [GitHub Actions](https://github.com/features/actions) | ||
and the great community around them! | ||
|
||
### Continuous Integration | ||
|
||
We want to run the tests automatically whenever there is a Pull Request, so the actions to take are: | ||
|
||
```yml | ||
#... | ||
steps: | ||
# checkout the project | ||
- uses: actions/checkout@v2 | ||
|
||
# install nodejs on the Virtual Machine | ||
- name: Use Node.js | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
|
||
# install the project | ||
- name: Install | ||
run: npm install --ignore-scripts | ||
|
||
# run the test on the project itself | ||
- name: Run tests | ||
run: npm test | ||
``` | ||
|
||
### Continuous Delivery | ||
|
||
The delivery of our application needs just to push new commits to the Heroku remote git server. | ||
Moreover, it would be useful for our customers to see what version of the application is running and an | ||
updated CHANGELOG file. | ||
|
||
To automate these steps, it is necessary to define a good workflow in the first place. | ||
|
||
For examples, the process should answer questions like: | ||
- When releasing the application? | ||
- What semver is the new version? | ||
- What changes should be written in the changelog? | ||
- Must any scripts be executed? - like a database update | ||
- Should notification be sent? | ||
- a lot of other headaches!! | ||
|
||
This application will adopt a process like this: | ||
- at every merge in the `release` branch | ||
- using a commit messages format like [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) | ||
- bump a new semver version based on the commit messages | ||
- tag the version | ||
- generate a changelog description, grouping the commit messages | ||
- freeze the release on GitHub with the changelog text | ||
- deploy to Heroku | ||
|
||
This list can be easily transformed into a GitHub Action where every step will accomplish one of these tasks and the result will be like this (omitting parameters): | ||
|
||
```yml | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Version Bump | ||
uses: phips28/gh-action-bump-version@master | ||
... | ||
|
||
- name: Build Changelog message | ||
uses: scottbrenner/generate-changelog-action@master | ||
... | ||
|
||
- name: Create Github Release | ||
uses: actions/create-release@latest | ||
... | ||
|
||
- name: Deploy to Heroku | ||
uses: akhileshns/heroku-deploy@v3.4.6 | ||
... | ||
``` | ||
|
||
Check out [the source code](https://github.com/Eomm/fastify-discord-bot-demo/tree/master/.github/workflows/cd.yml) to see the complete file. | ||
|
||
## End | ||
|
||
In the next posts we will: | ||
|
||
+ add new features to the application: | ||
+ store the token in cookies | ||
+ add some `/api` endpoints | ||
|
||
|
||
## Side effects | ||
|
||
To write this article: | ||
- I created an [issue](https://github.com/fastify/fastify-cli/issues/267) to `fastify-cli` to support ESM | ||
- I added to the `gh-action-bump-version` GitHub Action: | ||
- support new pattern matching strings [#36](https://github.com/phips28/gh-action-bump-version/pull/36) | ||
- skip the tagging phase [#37](https://github.com/phips28/gh-action-bump-version/pull/37) | ||
- I fixed a VSCode icon pack extension [#178](https://github.com/EmmanuelBeziat/vscode-great-icons/pull/178) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters