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
6 changes: 4 additions & 2 deletions infra/jenkins/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ USER root
RUN mkdir -p /var/jenkins_home && chown -R jenkins:jenkins /var/jenkins_home
USER jenkins

COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
COPY infra/jenkins/plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN jenkins-plugin-cli --plugin-file /usr/share/jenkins/ref/plugins.txt

ENV CASC_JENKINS_CONFIG=/usr/share/jenkins/ref/jenkins.yaml
COPY script /usr/share/jenkins/ref/script

COPY --chown=jenkins:jenkins \
./job_dsl.groovy \
./infra/jenkins/job_dsl.groovy \
/usr/share/jenkins/ref/init.groovy.d/job_dsl.groovy
5 changes: 4 additions & 1 deletion infra/jenkins/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
services:
jenkins:
build: .
build:
context: ../../
dockerfile: infra/jenkins/Dockerfile
container_name: jenkins
ports:
- "8080:8080"
- "50000:50000"
volumes:
- ./jenkins_home:/var/jenkins_home
- ./jenkins.yaml:/var/jenkins_home/casc_configs/jenkins.yaml:ro
- ./../..:/workspace
- /var/run/docker.sock:/var/run/docker.sock # Docker in docker
- ../../images:/images
environment:
Expand Down
6 changes: 6 additions & 0 deletions infra/jenkins/jenkins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ appearance:
unclassified:
location:
url: http://localhost:8080/

security:
scriptApproval:
approvedScriptHashes:
- "SHA512:4221429e203439d7010910774f0ef107c56e054eb7001881a02852d108c03fb8d8470c15e2a6d27141b604075db9c09223a7e5c083478dcef770f84b733ceb2a"
forceSandbox: false
55 changes: 55 additions & 0 deletions infra/jenkins/job_dsl.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,58 @@ folder('Projects') {
displayName(name)
description('Available projects in whanos')
}

freeStyleJob('link-project') {
displayName('Link Project')
description('Links a repository to the Whanos infrastructure by creating a corresponding Jenkins project job.')

parameters {
stringParam('PROJECT_NAME', '', 'Name of the project (e.g. my-app)')
stringParam('GIT_URL', '', 'Git repository URL to link')
stringParam('GIT_BRANCH', 'main', 'Branch to monitor (default: main)')
stringParam('ROOT_FOLDER', '', 'Root folder in the repository (if applicable)')
credentialsParam('GIT_CREDENTIALS_ID') {
description('Credentials ID for accessing the Git repository (if private)')
defaultValue('')
}
}

steps {
shell('''
#!/bin/bash
set -e

rm -rf repo
git clone "$GIT_URL" repo
cd repo
git checkout "$GIT_BRANCH"

echo "Detecting language..."
WHANOS_DIR=$PWD
if [ -n "$ROOT_FOLDER" ]; then
cd "$ROOT_FOLDER"

fi

LANG_DETECTED=$("$WHANOS_DIR/script/detect_language")
EXIT_CODE=$?

if [ $EXIT_CODE -ne 0 ]; then
echo "Error: repository is not Whanos-compatible"
exit 1
fi

echo "Detected language: $LANG_DETECTED"
echo "$LANG_DETECTED" > $WHANOS_DIR/detected_language.txt
export LANG_DETECTED=$(cat $WHANOS_DIR/detected_language.txt)

# Copy DSL script to workspace
cp /workspace/infra/jenkins/project_job.groovy $WORKSPACE/project_job.groovy
''')
dsl {
external("project_job.groovy")
additionalClasspath("project_job.groovy/lives")
ignoreExisting(false)
}
}
}
48 changes: 48 additions & 0 deletions infra/jenkins/project_job.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
def String projectName = binding.variables['PROJECT_NAME']
def String gitUrl = binding.variables['GIT_URL']
def String gitBranch = binding.variables['GIT_BRANCH']
def String credentialsId = binding.variables['GIT_CREDENTIALS_ID']
def File languageFile = new File("${WORKSPACE}/repo/detected_language.txt")
def String language = languageFile.text.trim().toLowerCase()
def String baseImage = "whanos-${language}"

folder('Projects') {
displayName('Projects')
description('Available projects in Whanos')
}

freeStyleJob("Projects/${projectName}") {
displayName("${projectName}")
description('Linked project managed by Whanos')

scm {
git {
remote {
url("${gitUrl}")
if (credentialsId) {
credentials(credentialsId)
}
}
branch("*/${gitBranch}")
}
}

triggers {
scm('* * * * *') // every minute
}

steps {
shell("echo Building ${projectName} using ${baseImage}")
shell("docker build -t ${projectName}:${language} --build-arg BASE_IMAGE=${baseImage} .")
shell("""
#!/bin/bash
set -e
// if [ -f whanos.yml ]; then
// echo "Deploying ${projectName} to Kubernetes..."
// kubectl apply -f whanos.yml
// else
// echo "No whanos.yml found, skipping deployment."
// fi
""")
}
}