27
loading...
This website collects cookies to deliver better user experience
mkdir node-express
cd node-express
npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (node-express)
version: (1.0.0)
description: A simple Node.js with Express framework application
entry point: (index.js) app.js
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/koyeb/demo/node-express/package.json:
{
"name": "node-express",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes)
npm install express --save
/
. All other routes will respond with a 404 error.app.js
and copy the content below:const express = require('express')
const app = express()
const port = process.env.PORT || 3000
app.get('/', (req, res) => {
res.json({
commit_id: process.env.COMMIT_ID || 'unknown',
port
})
})
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`)
})
node app.js
and request the /
endpoint running:curl http://localhost:3000/
{"commit_id":"unknown","port":3000}
Dockerfile
in your project folder containing the content below.FROM node:lts as runner
WORKDIR /node-express
ENV NODE_ENV production
ARG COMMIT_ID
ENV COMMIT_ID=${COMMIT_ID}
COPY . .
RUN npm ci --only=production
EXPOSE 3000
CMD ["node", "app.js"]
docker build . -t ghcr.io/<YOUR_GITHUB_USERNAME>/node-express:prod
docker run -p 3000:3000 ghcr.io/<YOUR_GITHUB_USERNAME>/node-express:prod
curl http://localhost:3000/
{"commit_id":"unknown","port":3000}
docker push ghcr.io/<YOUR_GITHUB_USERNAME>/node-express:prod
https://github.com/<YOUR_GITHUB_USERNAME>?tab=packages
.<REPLACE_ME_WITH_GH_USERNAME>
with your GitHub username and <REPLACE_ME_WITH_GH_TOKEN>
with a valid GitHub token having registry read/write permissions and execute the command below.echo \
'{ \
"auths": { \
"ghcr.io": { \
"username": "<REPLACE_ME_WITH_GH_USERNAME>", \
"password": "<REPLACE_ME_WITH_GH_TOKEN>" \
} \
} \
}' | koyeb secrets create gh-registry-credentials
koyeb app init node-express --docker "ghcr.io/<REPLACE_ME_WITH_GH_USERNAME>/node-express:prod" --ports 3000:http --routes /:3000 --docker-private-registry-secret gh-registry-credentials
https://node-express-<REPLACE_ME_WITH_GH_USERNAME>.koyeb.app
.main
of your repository using GitHub Actions.mkdir -p .github/workflows
cd .github/workflows
workflow.yaml
inside the directory we created in the previous step and paste the snippet below:name: CI
on:
push:
branches:
- main
env:
GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
jobs:
build-push:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Seed env
run: |
echo $GITHUB_SHA | awk '{ printf "SHORT_SHA1=%.7s\n", $1 }' >> $GITHUB_ENV
basename $GITHUB_REF | awk '{ printf "BRANCH_NAME=%s\n", $1 }' >> $GITHUB_ENV
- name: Docker build
run: docker build --rm=false --build-arg COMMIT_ID=$GITHUB_SHA -t ghcr.io/<YOUR_GITHUB_USERNAME>/node-express:prod . # REPLACE <YOUR_GITHUB_USERNAME> with your GitHub username.
- name: Docker login
run: echo $GHCR_TOKEN | docker login ghcr.io -u <YOUR_GITHUB_USERNAME> --password-stdin # REPLACE <YOUR_GITHUB_USERNAME> with your GitHub username.
- name: Docker push
run: docker push ghcr.io/<YOUR_GITHUB_USERNAME>/node-express:prod # REPLACE <YOUR_GITHUB_USERNAME> with your GitHub username.
- name: Deploy on Koyeb
uses: koyeb-community/koyeb-actions@v2
with:
api_token: "${{ secrets.KOYEB_TOKEN }}"
- run: koyeb services redeploy --app=node-express node-express
GHCR_TOKEN
containing a valid GitHub token having registry read/write permissions to push the image to the registry.KOYEB_TOKEN
containing a valid Koyeb token to redeploy the application.main
branch, a Docker image is built and push to the GitHub registry with the tag prod
. Once the image pushed,