Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions .github/workflows/deploy-ghcr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ on:
branches:
- dev
- feature/issue-203/fix-CD-runner
- bugfix/issue-232/implement-openai-secret

jobs:
build-and-deploy:
runs-on: ubuntu-latest
env:
BACKEND_URL: ailearningtool.ddns.net
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
steps:
# Step 1: Checkout the repository
- name: Checkout repository
Expand All @@ -24,7 +26,12 @@ jobs:
java-version: '21'
distribution: 'temurin'

# Step 3: Adjust the backend application parameters
# Step 3: Adjust OpenAI API key.
- name: Adjust the OpenAI API key
run: (envsubst < OpenAIAuthenticator.java) > OpenAIAuthenticatorUPD.java && cp OpenAIAuthenticatorUPD.java OpenAIAuthenticator.java && rm OpenAIAuthenticatorUPD.java
working-directory: backend/src/main/java/com/UoB/AILearningTool

# Step 4: Adjust the backend application parameters
- name: Adjust the backend URL
run: |
echo -e "spring.application.name=AILearningTool\n\
Expand All @@ -39,22 +46,22 @@ jobs:
server.ssl.key-store-password=ailearntool" > application.properties
working-directory: backend/src/main/resources

# Step 4: Adjust the frontend BACKEND_URL constant
# Step 5: Adjust the frontend BACKEND_URL constant
- name: Update frontend BACKEND_URL constant
run : echo "const BACKEND_URL = 'http://$BACKEND_URL';
export { BACKEND_URL };" > globalConstants.js
working-directory: frontend/src/assets

# Step 5: Build the JAR file with Maven
# Step 6: Build the JAR file with Maven
- name: Build JAR
run: mvn clean package
working-directory: backend

# Step 6: Log in to GitHub Container Registry
# Step 7: Log in to GitHub Container Registry
- name: Log in to GHCR
run: echo "${{ secrets.CONTAINER_REGISTRY_PAT }}" | docker login ghcr.io -u GerardChabaBristol --password-stdin

# Step 7: Build and push the Docker image
# Step 8: Build and push the Docker image
- name: Publish Docker image to GHCR
uses: docker/build-push-action@v3
with:
Expand All @@ -63,7 +70,7 @@ jobs:
push: true
tags: ghcr.io/spe-uob/2024-ailearningtool:latest

# Step 8: Pull the image and deploy on the server
# Step 9: Pull the image and deploy on the server
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
Expand All @@ -74,5 +81,5 @@ jobs:
docker pull ghcr.io/spe-uob/2024-ailearningtool:latest
docker stop ailearningtool || true
docker rm ailearningtool || true
docker run -d --name ailearningtool -p 443:443 ghcr.io/spe-uob/2024-ailearningtool:latest -v keystore.p12:/keystore.p12
sudo docker run -d --name ailearningtool -p 443:443 ghcr.io/spe-uob/2024-ailearningtool:latest -v /keystore.p12:/keystore.p12

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
backend/target
backend/.idea
backend/src/main/resources/keystore.p12
backend/src/main/java/com/UoB/AILearningTool/OpenAIAuthenticator.java
backend/database.db
backend/src/main/resources/static/*

frontend/dist
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.UoB.AILearningTool;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
Expand All @@ -10,21 +9,21 @@

public class OpenAIAuthenticator {
private final Logger log = LoggerFactory.getLogger(OpenAIAuthenticator.class);

private String apiKey;

// Constructor that initializes the API key
public OpenAIAuthenticator() {
this.apiKey = "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";
log.info("OpenAI API key set.");
this.apiKey = "$OPENAI_API_KEY";
log.info("OpenAI API key set.");
}

// Returns the API key as a "Bearer token" (used in authorization headers)
public String getBearerToken() {
if (this.apiKey == null || this.apiKey.isEmpty()) {
log.error("API Key not set or is empty.");
throw new IllegalStateException("API Key for OpenAI is not set.");
}
return this.apiKey;
if (this.apiKey == null || this.apiKey.equals("OPENAI_API_KEY") || this.apiKey.isEmpty()) {
log.error("API Key not set or is empty.");
throw new IllegalStateException("API Key for OpenAI is not set.");
}
return this.apiKey;
}
}
54 changes: 47 additions & 7 deletions localExecute.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
#!/bin/bash
# Initialising the server outside of Docker container, eg. for development
# and debugging purposes. No SSL is used.
# First argument - openAI API key
# Second argument - preferred port number (optional)
# Preferred port number can be provided as an integer number
# in range 0-65535, for example
# ./localExecute.sh 9000
# ./localExecute.sh 65535
# If no argument is provided or the range is incorrect, a standard port 8080 is used.
# in range 0-65535.
# Usage examples:
# ./localExecute.sh somerandomOpenAIAPIKey1 9000
# ./localExecute.sh somestrangeunreadableopenaiapikey 9000
# If no port argument is provided or the range is incorrect, a standard port 8080 is used.

# Processes the argument (if provided)
if [ "$1" != "" ] && [ "$1" -ge 0 ] && [ "$1" -le 65535 ]
# Process the OpenAI API key argument (if provided)
if [ "$1" != "" ]
then
PORT=$1
echo "package com.UoB.AILearningTool;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OpenAIAuthenticator {
private final Logger log = LoggerFactory.getLogger(OpenAIAuthenticator.class);

private String apiKey;

// Constructor that initializes the API key
public OpenAIAuthenticator() {
this.apiKey = \"$1\";
log.info(\"OpenAI API key set.\");
}

// Returns the API key as a \"Bearer token\" (used in authorization headers)
public String getBearerToken() {
if (this.apiKey == null || this.apiKey.equals(\"OPENAI_API_KEY\") || this.apiKey.isEmpty()) {
log.error(\"API Key not set or is empty.\");
throw new IllegalStateException(\"API Key for OpenAI is not set.\");
}
return this.apiKey;
}
}" > backend/src/main/java/com/UoB/AILearningTool/OpenAIAuthenticator.java
else
echo "OpenAI API key not provided!"
exit 1
fi

# Processes the port argument (if provided)
if [ "$2" != "" ] && [ "$2" -ge 0 ] && [ "$2" -le 65535 ]
then
PORT=$2
else
PORT=8080
fi
Expand Down