Skip to content

Commit

Permalink
Decouple behaviours to main script && Fix paths && update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolman-Freecss committed Oct 7, 2024
1 parent 182d946 commit 0cd33c2
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 178 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-devops.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
run: sudo apt-get update && sudo apt-get install docker-compose -y

- name: Run Docker Compose
working-directory: src/env.local
working-directory: src/local
run: docker-compose up -d

- name: Wait for services to be ready
Expand All @@ -48,5 +48,5 @@ jobs:
- name: Tear down Docker Compose
if: always()
working-directory: src/env.local
working-directory: src/local
run: docker-compose down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

# Ignore .env/env.local.env files
.env/env.local
.env/env.docker-local

# Ignore RSA temp files
*_rsa*
*_rsa*

# Python temp files
*.pyc
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
python-jenkins~=1.8.2
setuptools~=68.2.0
requests~=2.32.3
urllib3~=2.2.3
urllib3~=2.2.3
python-dotenv~=1.0.1
File renamed without changes.
8 changes: 4 additions & 4 deletions src/local/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ RUN /app/venv/bin/pip install --no-cache-dir -r /app/requirements.txt

# ---------- JENKINS DEPS ----------

COPY jenkins.yaml /var/jenkins_home/casc.yaml
COPY jenkins_plugins/plugins.yaml /var/jenkins_home/casc.yaml
ENV CASC_JENKINS_CONFIG=/var/jenkins_home/casc.yaml

# Copy the shell script that installs dependencies
COPY jenkins_install_deps.sh /usr/local/bin/jenkins_install_deps.sh

# Ensure the shell scripts are executable
RUN chmod +x /usr/env.local/bin/jenkins_install_deps.helpers
RUN chmod +x /usr/local/bin/jenkins_install_deps.sh

# Set the virtual environment as the default for Python
ENV PATH="/app/venv/bin:$PATH"

RUN echo "Before executing the shell script"

# Execute the dependency installation script
RUN /usr/env.local/bin/jenkins_install_deps.helpers
RUN /usr/local/bin/jenkins_install_deps.sh

# ----------------------------------------

Expand All @@ -53,6 +53,6 @@ RUN echo "After executing the shell script and before executing the Python scrip
RUN ls -l /app

# Execute the copied shell script
#RUN /app/build.helpers
#RUN /app/build.sh

RUN echo "After executing the Python script"
Empty file added src/local/__init__.py
Empty file.
23 changes: 8 additions & 15 deletions src/local/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ services:
- jenkins_home:/var/jenkins_home # Volume for Jenkins data
- ./init-scripts:/var/jenkins_init_scripts # We mount the init scripts
- ./jenkins_plugins:/var/jenkins_home/plugins # We mount the plugins
entrypoint: ["/bin/bash", "-c", "/usr/bin/tini -- /usr/env.local/bin/jenkins.helpers && dockerd > /var/log/dockerd.log 2>&1 && tail -f /dev/null"]
entrypoint: ["/bin/bash", "-c", "/usr/bin/tini -- /usr/local/bin/jenkins.sh && dockerd > /var/log/dockerd.log 2>&1 && tail -f /dev/null"]
healthcheck:
test: ["CMD-SHELL", "curl -sS http://localhost:8080/login || exit 1"]
interval: 30s
Expand All @@ -31,25 +31,18 @@ services:
image: python:3.10
container_name: kf-jenkins-init
volumes:
- ./main:/usr/src/app
- ./.data:/usr/src/app/data
- ./requirements.txt:/usr/src/app/requirements.txt
working_dir: /usr/src/app
- ./main:/app/src # Between all the src we have a needed requirements.txt for this implantation
- ./requirements.txt:/app/requirements.txt
- ../../.env/env.docker-local:/app/.env/env.docker-local
working_dir: /app
environment:
- JENKINS_URL=http://kf-jenkins:8080
- JENKINS_USER=admin
- JENKINS_PASS=admin
- GITHUB_CREDENTIALS_ID=github-credentials
- GITHUB_USER=Kolman_Freecss
# TODO: This is a temporary PAT, it should be replaced by a secret (in prod environment) or not synch with the repository
- PAT_JENKINS=
- EXECUTION_ENVIRONMENT=docker-env.local
- ENV=docker-local
command: >
bash -c "pip install -r requirements.txt && python init_jenkins.py && python build.py"
bash -c "ls -la . && ls -la src && pip install -r requirements.txt && python src/main.py"
depends_on:
jenkins:
condition: service_healthy

volumes:
jenkins_home:
driver: env.local
driver: local
Empty file added src/local/main/__init__.py
Empty file.
89 changes: 50 additions & 39 deletions src/local/main/build.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,65 @@
import os

import src.local.main.config as config_module
import src.local.main.services as services
from src.local.main.services import CredentialsType
import config as config_module
import services as services
from services import CredentialsType

try:
print(f"JENKINS INFO -> Jenkins URL: {config_module.get(config_module.ConfigKeys.JENKINS_URL)}, Username: {config_module.get(config_module.ConfigKeys.JENKINS_USER)}, API Token: {config_module.get(config_module.ConfigKeys.JENKINS_PASS)}")
def fetch():
try:
print(f"JENKINS INFO -> Jenkins URL: {config_module.get(config_module.ConfigKeys.JENKINS_URL)}, Username: {config_module.get(config_module.ConfigKeys.JENKINS_USER)}, API Token: {config_module.get(config_module.ConfigKeys.JENKINS_PASS)}")

# Jenkins version
user = services.jenkins_service.get_whoami()
version = services.jenkins_service.get_version()
print('JENKINS INFO -> Hello %s from Jenkins %s' % (user['fullName'], version))

# Print all files in the current directory
print(os.listdir('.'))

# Configured on docker-compose (check volumes)
if config_module.ENV == 'local' or config_module.ENV == 'docker-env':
path_job = '../.data/Jenkinsfile_spring.xml'
else:
path_job = './data/Jenkinsfile_spring.xml'

# Jenkins version
user = services.jenkins_service.get_whoami()
version = services.jenkins_service.get_version()
print('JENKINS INFO -> Hello %s from Jenkins %s' % (user['fullName'], version))
# List all content dir
if config_module.ENV != 'local' or config_module.ENV != 'docker-env':
try:
content = os.listdir('../')
print(content)
except Exception as e:
print(e)

# Print all files in the current directory
print(os.listdir('.'))
# Read XML job
job_config = open(path_job).read()

# Configured on docker-compose (check volumes)
if config_module.ENV == 'env.local':
path_job = '../.data/Jenkinsfile_spring.xml'
else:
path_job = './data/Jenkinsfile_spring.xml'
# Create credentials
services.build_credentials(CredentialsType.USER)

job_name = "spring-pipeline"

# List all content dir
if config_module.ENV != 'env.local':
try:
content = os.listdir('../')
print(content)
if services.jenkins_service.job_exists(job_name):
services.jenkins_service.delete_job(job_name)
print(f'Job {job_name} already exists and was deleted')
else:
print(f'Job {job_name} does not exist')
services.jenkins_service.create_job(job_name, job_config)
print('Job created successfully')
except Exception as e:
print(e)

# Read XML job
job_config = open(path_job).read()

# Create credentials
services.build_credentials(CredentialsType.USER)

job_name = "spring-pipeline"
services.jenkins_service.build_job(job_name)
print('Job execute successfully')
except Exception as e:
print("Error: ", e)

def start():
print("Build:: ...")
try:
if services.jenkins_service.job_exists(job_name):
services.jenkins_service.delete_job(job_name)
print(f'Job {job_name} already exists and was deleted')
else:
print(f'Job {job_name} does not exist')
services.jenkins_service.create_job(job_name, job_config)
print('Job created successfully')
fetch()
except Exception as e:
print(e)
print("Error: ", e)

services.jenkins_service.build_job(job_name)
print('Job execute successfully')
except Exception as e:
print("Error: ", e)
if config_module.ENV == 'local':
start()
14 changes: 13 additions & 1 deletion src/local/main/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ class ConfigKeys(str, Enum):
ENV = os.getenv('ENV') # Provided by Github Actions or Environment Variables
if ENV == 'prod':
env_files_path = '<cloud-config>'
elif ENV == 'docker-local':
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '../')) # .. This avoid to duplicate our .env in src folder "app/.env"
if project_root.endswith(('/')):
project_root = project_root[:-1]
env_files_path = os.path.join(project_root, '.env', f'env.{ENV}')
if ENV == '' or ENV is None:
ENV = 'local'
else:
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..'))
if project_root.endswith(('/')):
project_root = project_root[:-1]
env_files_path = os.path.join(project_root, '.env', f'env.{ENV}')
ENV = 'local'
if ENV == '' or ENV is None:
ENV = 'local'

print (f"ENV: {ENV}, PROJECT_ROOT: {project_root}, ENV_FILES_PATH: {env_files_path}")

# Sensitive data not injected through pipelines
config = dotenv_values(env_files_path)
Expand Down
Loading

0 comments on commit 0cd33c2

Please sign in to comment.