Skip to content

Commit

Permalink
Merge pull request #4 from CIAT-DAPA/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
victor-993 authored Jun 1, 2023
2 parents 09429c7 + 1200984 commit 0a1d541
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test_pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ jobs:
commit_message: "Update version to ${{ steps.taggerDryRun.outputs.new_tag }}"
# API Zip
- name: Zip artifact for deployment
run: zip releaseAPI.zip ./src/* -r
run: cd ./src && zip -r ../releaseAPI.zip ./* && cd ..
# Upload Artifacts
- name: Upload ORM artifact for deployment job
uses: actions/upload-artifact@v3
Expand Down
185 changes: 185 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Define an empty map for storing remote SSH connection parameters
def remote = [:]

pipeline {
agent any

environment {
server_name = credentials('name_spcat')
server_host = credentials('host_spcat')
ssh_key = credentials('spcat_key')
}

stages {
stage('Connection to AWS server') {
steps {
script {
// Set up remote SSH connection parameters
remote.allowAnyHosts = true
remote.identityFile = ssh_key
remote.user = ssh_key_USR
remote.name = server_name
remote.host = server_host

}
}
}

stage('Verify Api folder and environment') {
steps {
script {
sshCommand remote: remote, command: '''
# Verify and create the api_SPCAT folder if it does not exist and the virtual environment
if [ ! -d api_SPCAT ]; then
mkdir ./api_SPCAT
cd ./api_SPCAT
python3 -m venv env
fi
'''

}
}
}

stage('Stop previous API') {
steps {
script {
sshCommand remote: remote, command: '''
# Stop the API if it is running
cd ./api_SPCAT
if [ -f pid.txt ]; then
PID_API_SPCAT=$(cat pid.txt)
if kill -0 "$PID_API_SPCAT" 2>/dev/null; then
echo "The process exists, stopping it..."
kill "$PID_API_SPCAT"
fi
fi
'''
}
}
}

stage('Backup previous files') {
steps {
script {
sshCommand remote: remote, command: '''
# Saving old API files
cd ./api_SPCAT
rm -rf api_antiguo
if [ -d api_actual ]; then
mv api_actual api_antiguo
fi
'''
}
}
}

stage('Download latest release') {
steps {
script {
sshCommand remote: remote, command: '''
# Download the latest release from GitHub
cd ./api_SPCAT
rm -rf releaseApi.zip
curl -LOk https://github.com/CIAT-DAPA/spcat_webapi/releases/latest/download/releaseApi.zip
rm -rf api_actual
unzip releaseApi.zip -d api_actual
'''
}
}
}

stage('Update dependencies') {
steps {
script {
sshCommand remote: remote, command: '''
cd ./api_SPCAT
# Activate the virtual environment
source env/bin/activate
# Updating the dependencies
pip install --upgrade setuptools wheel
pip install -r api_actual/requirements.txt
'''
}
}
}

stage('Start API') {
steps {
script {
sshCommand remote: remote, command: '''
# Configure variables for deployment
while IFS= read -r line; do
export "$line"
done < variables.txt
# Activate the virtual environment
cd ./api_SPCAT
source env/bin/activate
# Start API
cd ./api_actual
nohup python3 api.py > api_spcat.log 2>&1 &
# Get the new PID and save it to a file
PID_API_SPCAT=$!
echo $PID_API_SPCAT > ../pid.txt
'''
}
}
}

stage('Verify API') {
steps {
script {
def apiUrl = "http://127.0.0.1:5000"

//def response = sh(script: "curl -sL -w \"%{http_code}\" -o /dev/null ${apiUrl}", returnStdout: true)

def response = sshCommand remote: remote, command: "curl -sL -w \"%{http_code}\" -o /dev/null ${apiUrl}"

if (response.trim() == '200') {
echo "API is running correctly."
} else {
error "API is not running correctly. Rolling back..."
}
}
}
}
}

post {
failure {
script {
sshCommand remote: remote, command: '''
# Configure variables for deployment
while IFS= read -r line; do
export "$line"
done < variables.txt
# Rollback to the previous API if any step fails
cd ./api_SPCAT
rm -rf api_actual
mv api_antiguo api_actual
# Activate the virtual environment
source env/bin/activate
# Start API
cd ./api_actual
nohup python3 api.py > api_spcat.log 2>&1 &
# Get the new PID and save it to a file
PID_API_SPCAT=$!
echo $PID_API_SPCAT > ../pid.txt
'''
}
}
}
}
6 changes: 1 addition & 5 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,4 @@ def send_static(path):
connect(host=config['CONNECTION_DB'])
print("Connected DB")

if config['DEBUG']:
app.run(threaded=True, port=config['PORT'], debug=config['DEBUG'])
else:
app.run(host=config['HOST'], port=config['PORT'],
debug=config['DEBUG'])
app.run(threaded=True, host=config['HOST'], port=config['PORT'], debug=config['DEBUG'])
4 changes: 2 additions & 2 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
config['DEBUG'] = True
config['HOST'] = 'localhost'
config['PORT'] = 5000
config['CONNECTION_DB']='mongodb://root:s3cr3t@localhost:27017/dbgap?authSource=admin'
config['CONNECTION_DB']='mongodb://localhost:27017/dbgap'
else:
config['DEBUG'] = False
config['HOST'] = '0.0.0.0'
config['PORT'] = os.getenv('PORT')
config['PORT'] = os.getenv('API_SPCAT_PORT')
config['CONNECTION_DB']=os.getenv('CONNECTION_DB')
1 change: 1 addition & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Flask==2.2.3
Flask-Cors==3.0.10
Flask-RESTful==0.3.9
flask-swagger-ui==4.11.1
gunicorn==20.1.0
itsdangerous==2.1.2
Jinja2==3.1.2
jsonschema==4.17.3
Expand Down

0 comments on commit 0a1d541

Please sign in to comment.