added the case if node container is not used #3
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
name: Deployment (Container) | |
on: | |
push: | |
branches: | |
- master | |
- dev | |
env: | |
CACHE_KEY: node-deps | |
MONGODB_DB_NAME: gha-demo | |
jobs: | |
test: | |
# DKDK environment name is the one defined in the corresponding repo > Setting > Environment | |
environment: testing | |
# Runner | |
runs-on: ubuntu-latest | |
# DKDK add container using docker hub's image, node | |
# to have full control for environment | |
# by having container, jobs are running at docker container, not at ubuntu-latest | |
container: | |
image: node:20 | |
env: | |
# DKDK for accessing mongodb cloud | |
# MONGODB_CONNECTION_PROTOCOL: mongodb+srv | |
# MONGODB_CLUSTER_ADDRESS: cluster0.ntrwp.mongodb.net | |
# MONGODB_USERNAME: ${{ secrets.MONGODB_USERNAME }} | |
# MONGODB_PASSWORD: ${{ secrets.MONGODB_PASSWORD }} | |
# DKDK here, to access service container, mongodb in the following | |
# so al env should match with the container's env | |
# Note that this assumes that service container is running under node:20 container | |
MONGODB_CONNECTION_PROTOCOL: mongodb | |
# if not using node:20 container, then MONGODB_CLUSTER_ADDRESS should be changed | |
# e.g., MONGODB_CLUSTER_ADDRESS: 127.0.0.1:27017 | |
MONGODB_CLUSTER_ADDRESS: mongodb | |
MONGODB_USERNAME: root | |
MONGODB_PASSWORD: example | |
PORT: 8080 | |
# DKDK add Service Containers: run a test DB at mongodb | |
services: | |
# DKDK service name, mongodb or anything | |
mongodb: | |
image: mongo | |
# DKDK if service container is not running under container (node:20), then ports should be defined | |
# ports: | |
# - 27017:27017 | |
env: | |
# DKDK this is stated at Docker hub's mongodb document | |
MONTO_INITDB_ROOT_USERNAME: root | |
MONTO_INITDB_ROOT_PASSWORD: example | |
# PORT: 8080 | |
steps: | |
- name: Get Code | |
uses: actions/checkout@v4 | |
- name: Cache dependencies | |
uses: actions/cache@v4 | |
with: | |
path: ~/.npm | |
key: ${{ env.CACHE_KEY }}-${{ hashFiles('**/package-lock.json') }} | |
- name: Install dependencies | |
run: npm ci | |
- name: Run server | |
run: npm start & npx wait-on http://127.0.0.1:$PORT # requires MongoDB Atlas to accept requests from anywhere! | |
- name: Run tests | |
run: npm test | |
- name: Output information | |
run: | | |
echo "MONGODB_USERNAME: $MONGODB_USERNAME" | |
deploy: | |
needs: test | |
runs-on: ubuntu-latest | |
steps: | |
- name: Output information | |
env: | |
PORT: 3000 | |
run: | | |
echo "MONGODB_DB_NAME: $MONGODB_DB_NAME" | |
echo "MONGODB_USERNAME: $MONGODB_USERNAME" | |
echo "${{ env.PORT }}" |