Scripts: Populating database, autocreating .env file. CI/CD: Removed Deployement for now.

This commit is contained in:
Yohan Boujon 2024-12-21 14:12:53 +01:00
parent 5e5c599338
commit 97ede71cf8
5 changed files with 73 additions and 69 deletions

View file

@ -29,35 +29,4 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: java-app
path: '${{ github.workspace }}/helpapp-backend/*'
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
permissions:
id-token: write #This is required for requesting the JWT
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v4
with:
name: java-app
- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_88F0F141C7084A8789D648390DA913D7 }}
tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_A84251CAC0104DC8A6709EE1E898E464 }}
subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_11555886AA084155873A3C256628D2F2 }}
- name: Run Maven Spring Boot
uses: azure/webapps-deploy@v3
with:
app-name: 'helpapp'
slot-name: 'Production'
package: '${{ github.workspace }}/helpapp-backend/user-service/target/user-service-1.0-SNAPSHOT.jar'
custom-command: 'mvn spring-boot:run -pl user-service'
path: '${{ github.workspace }}/helpapp-backend/*'

View file

@ -1,9 +1,20 @@
# Database Initialization
First create a user in the mariadb:
### Install mariadb:
#### On Arch-Linux
```
pacman -S mariadb
sudo mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
sudo chown -R mysql:mysql /var/lib/mysql
sudo systemctl enable mariadb
sudo systemctl start mariadb
```
### Create a user
```bash
sudo mariab -u root -p
sudo mariadb -u root -p
```
```sql
CREATE USER '<user>'@'localhost' IDENTIFIED BY '<password>';
@ -11,13 +22,6 @@ GRANT ALL PRIVILEGES ON *.* TO '<user>'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
```
Then modify the `.env` file:
```bash
SERVICE_ARCHITECTURE_USER=user
SERVICE_ARCHITECTURE_PASSWORD=password
```
**And finally run the `init.sh` to create the database.**
### Run the `init.sh` to create the database.
> If you want to update the database, it is recommended to run `remove.sh` before hand.

40
db/db.sh Normal file
View file

@ -0,0 +1,40 @@
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RESET='\033[0m' # No Color
source_env_file()
{
source .env
user="$SERVICE_ARCHITECTURE_USER"
pwd="$SERVICE_ARCHITECTURE_PASSWORD"
}
create_env_file()
{
echo -e "SERVICE_ARCHITECTURE_USER=$1\nSERVICE_ARCHITECTURE_PASSWORD=$2" > .env
}
check_env()
{
if [ -f .env ]; then
source_env_file
else
echo -e $YELLOW"Warning: No '.env' file found."$RESET
echo "Enter the db username:"
read db_user
echo "Enter the db password:"
read -s db_pwd
if ! mariadb -u"$db_user" -p"$db_pwd" -e "exit" > /dev/null 2>&1
then
echo -e $RED"Error: Could not connect to MYSQL with the following ids."$RESET
exit 1
else
create_env_file $db_user $db_pwd
source_env_file
fi
fi
}

View file

@ -1,28 +1,27 @@
#!/bin/bash
# Checks the .env file
if [ -f .env ]; then
source .env
user="$SERVICE_ARCHITECTURE_USER"
pwd="$SERVICE_ARCHITECTURE_PASSWORD"
else
echo ".env file not found. Please create one with USER and PASSWORD variables."
exit 1
fi
source ./db.sh
echo "Creating database 'service-architecture'..."
check_env
echo "> Creating database 'service-architecture'..."
if ! mariadb -u "$user" -p"$pwd" -e "CREATE DATABASE IF NOT EXISTS \`service-architecture\`;"
then
echo "Error: Failed to create database, check your .env"
echo -e $RED"Error: Failed to create database, check your .env"$RESET
exit 1
fi
echo "Uploading database 'service-architecture'..."
echo "> Uploading database 'service-architecture'..."
if ! mariadb -u "$user" -p"$pwd" "service-architecture" < db.sql
then
echo "Error: Failed to execute db.sql, contact the maintainers."
echo -e $RED"Error: Failed to execute db.sql, contact the maintainers."$RESET
exit 1
fi
echo "Database setup successful!"
echo "> Populating database 'service-architecture'..."
if ! mariadb -u "$user" -p"$pwd" "service-architecture" < init.sql
then
echo -e $RED"Error: Failed to execute init.sql, contact the maintainers."$RESET
exit 1
fi
echo -e $GREEN"Database setup successful!"$RESET

View file

@ -1,21 +1,13 @@
#!/bin/bash
# Checks the .env file
if [ -f .env ]; then
source .env
user="$SERVICE_ARCHITECTURE_USER"
pwd="$SERVICE_ARCHITECTURE_PASSWORD"
else
echo ".env file not found. Please create one with USER and PASSWORD variables."
exit 1
fi
source ./db.sh
check_env
echo "Removing database 'service-architecture'..."
if ! mariadb -u "$user" -p"$pwd" -e "DROP DATABASE \`service-architecture\`;"
then
echo "Error: Failed to create database, check your .env"
echo -e $RED"Error: Failed to remove database:\n\t- Does the database exists ?\n\t- Have you checked your credentials ?"$RESET
exit 1
else
echo "Database removed successfully!"
echo -e $GREEN"Database removed successfully!"$RESET
fi