diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0833653a5ece5526c872b09c0f0af39134a82827..f7130082efe296a1402cdd253b296ab6ce68ac7d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,6 +3,7 @@ stages: - frontend build - backend test - frontend test + - deploy cache: key: ${CI_COMMIT_REF_SLUG} @@ -16,13 +17,13 @@ cache: variables: # Specify the SDK tools version and build tools version to use - ANDROID_COMPILE_SDK: 31 - ANDROID_BUILD_TOOLS: 31.0.0 + ANDROID_COMPILE_SDK: 33 + ANDROID_BUILD_TOOLS: 33.0.0 ANDROID_SDK_TOOLS: 7583922 ANDROID_HOME: "/usr/local/android-sdk" -Prettier check: +Prettier: image: node:16.3.0 stage: backend test needs: [] @@ -32,27 +33,42 @@ Prettier check: - yarn prettier --check . retry: 1 -Backend build: - image: node:16.3.0 - stage: backend test - needs: [] - script: - - cd backend - - yarn - - yarn tsc - -Frontend build: - image: gradle:7.5.0-jdk11 - stage: backend test - needs: [] - script: - # Restore Android SDK from cache - - if [ -d android-sdk ]; then mv android-sdk/* $ANDROID_HOME/; fi - # Download and install Android SDK - - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip - - unzip -q android-sdk.zip -d android-sdk - - echo y | android-sdk/cmdline-tools/bin/sdkmanager --sdk_root=$ANDROID_HOME "platforms;android-${ANDROID_COMPILE_SDK}" "build-tools;${ANDROID_BUILD_TOOLS}" - - cd frontend - - ./gradlew clean - - ./gradlew build --refresh-dependencies - - gradle build +Typescript-compile: + image: node:16.3.0 + stage: backend test + needs: [] + script: + - cd backend + - yarn + - yarn tsc + +Gradle-build: + image: gradle:7.5.0-jdk11 + stage: frontend test + needs: [] + script: + # Restore Android SDK from cache + #- if [ -d android-sdk ]; then mv android-sdk/* $ANDROID_HOME/; fi + # Download and install Android SDK + - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip + - unzip -q -o android-sdk.zip -d android-sdk + - rm -rf $ANDROID_HOME/platforms/android-33 + - echo y | android-sdk/cmdline-tools/bin/sdkmanager --sdk_root=$ANDROID_HOME "platforms;android-${ANDROID_COMPILE_SDK}" "build-tools;${ANDROID_BUILD_TOOLS}" + - cd frontend + - ./gradlew clean + - ./gradlew build --refresh-dependencies + - gradle build + +Backend (NTNU-VM): + image: node:16.3.0 + stage: deploy + needs: [Typescript-compile] + before_script: + - mkdir -p ~/.ssh + - echo "$NTNU_VM_SSH_KEY" | tr -d '\r' > ~/.ssh/id_rsa + - chmod 600 ~/.ssh/id_rsa + - ssh-keyscan 10.212.26.72 >> ~/.ssh/known_hosts + - ssh-keygen -p -f ~/.ssh/id_rsa + + script: + - ssh -v -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ubuntu@10.212.26.72 -p 22 "bash /home/git/tdt4240-tank-wars/backend/vm.sh </dev/null >/dev/null 2>&1" diff --git a/backend/README.md b/backend/README.md index 875e43cc639073dd67ca4ad448e91c9545f1b61e..cd331285191c1decb654caddf54498a197610306 100644 --- a/backend/README.md +++ b/backend/README.md @@ -25,3 +25,11 @@ Run the following commands in your terminal: Formatere koden: `yarn prettier` Kjøre tester: `yarn test` + +## VM Deployment + +The backend runs on NTNU VM. +IP: 10.212.26.72 +Port: 80 + +`vm.sh` defines the code that is needed to boot the vm (after CI connectes to the VM with SSH) diff --git a/backend/src/index.ts b/backend/src/index.ts index fb9a0a995cc84a948ccf88a77b7fb19fbe85470e..840d77bf1c69d05f05a1c49ef9f3fa77da6f67b2 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -4,7 +4,7 @@ import { User } from '../types/User'; import { GameHandler } from './gameHandler'; const app = express(); -const port = 3000; +const port = 80; const gameHandler = new GameHandler(); // singleton ;) @@ -22,10 +22,11 @@ app.get('/', (req, res) => { res.send('Hello, World!'); }); -app.listen(port, () => { +app.listen(port, '0.0.0.0', () => { console.log(`Server started on port ${port}`); }); + // establish connection to firebase const admin = require('firebase-admin'); const serviceAccount = path.join(__dirname, '../..', 'keys', 'fb-key.json'); diff --git a/backend/vm.sh b/backend/vm.sh new file mode 100644 index 0000000000000000000000000000000000000000..3811113f273f168fe74af99be454540c2b5d0b4c --- /dev/null +++ b/backend/vm.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# Kill the service running on port 80, if any +sudo kill $(sudo lsof -t -i:80) + +# Start the Express.js app +cd /home/git/tdt4240-tank-wars/backend && sudo git pull && sudo npx yarn && sudo npx yarn start >/dev/null 2>&1 & + +# Wait for the app to start and check the status +BACKEND_PID=$! +sleep 10 +if ps -p $BACKEND_PID > /dev/null; then + echo "Backend started successfully" + exit 0 +else + echo "Backend failed to start" + exit 1 +fi +