diff --git a/.github/workflows/github-docker-registry-push.yml b/.github/workflows/github-docker-registry-push.yml index 8e015f0a..66a1a53a 100644 --- a/.github/workflows/github-docker-registry-push.yml +++ b/.github/workflows/github-docker-registry-push.yml @@ -204,3 +204,13 @@ jobs: platforms: linux/amd64, linux/arm64 push: true tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:golang_${{ env.BRANCH }} + + - name: Build and push the jenkins agent for cpp tutorial + # This step builds and pushes the Jenkins agent for the C++ tutorial + if: contains(env.files, 'dockerfiles/cpp/Dockerfile') + uses: docker/build-push-action@v6 + with: + context: ./dockerfiles/cpp + platforms: linux/amd64, linux/aarch64 + push: true + tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:cpp_agent_${{ env.BRANCH }} diff --git a/build-docker-compose.yaml b/build-docker-compose.yaml index 525cf16e..cb11f18f 100644 --- a/build-docker-compose.yaml +++ b/build-docker-compose.yaml @@ -205,6 +205,23 @@ services: retries: 5 volumes: - agent-ssh-dir:/home/jenkins/.ssh:ro # Mounts the agent-ssh-dir volume to the /home/jenkins/.ssh path inside the container as read-only + cpp: + build: dockerfiles/cpp/. + container_name: desktop-jenkins_agent-1-cpp + profiles: + - cpp + depends_on: + sidekick_service: + condition: service_completed_successfully + jenkins_controller: + condition: service_started + healthcheck: + test: ["CMD-SHELL", "[ -f /home/jenkins/.ssh/authorized_keys ] || exit 1"] + interval: 5s + timeout: 10s + retries: 5 + volumes: + - agent-ssh-dir:/home/jenkins/.ssh:ro volumes: jenkins_home: null agent-ssh-dir: diff --git a/docker-compose.yaml b/docker-compose.yaml index 3c2601cd..9c1a9724 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -281,6 +281,23 @@ services: retries: 5 volumes: - agent-ssh-dir:/home/jenkins/.ssh:ro # Mounts the agent-ssh-dir volume to the /home/jenkins/.ssh path inside the container as read-only + cpp: + image: ${IMAGE_PREFIX}/${GHCR_USERNAME}/quickstart-tutorials/jenkinsci-tutorials:cpp_agent_${BRANCH_SUFFIX} + container_name: desktop-jenkins_agent-1-cpp + profiles: + - cpp + depends_on: + sidekick_service: + condition: service_completed_successfully + jenkins_controller: + condition: service_started + healthcheck: + test: ["CMD-SHELL", "[ -f /home/jenkins/.ssh/authorized_keys ] || exit 1"] + interval: 5s + timeout: 10s + retries: 5 + volumes: + - agent-ssh-dir:/home/jenkins/.ssh:ro volumes: jenkins_home: null empty_jenkins_home: null diff --git a/dockerfiles/cpp/Dockerfile b/dockerfiles/cpp/Dockerfile new file mode 100644 index 00000000..4c9ab6a6 --- /dev/null +++ b/dockerfiles/cpp/Dockerfile @@ -0,0 +1,19 @@ +FROM jenkins/ssh-agent:6.9.0 + +# Install necessary C++ build tools +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + cmake \ + gcc \ + g++ \ + clang \ + make \ + libstdc++-12-dev \ + pkg-config \ + && rm -rf /var/lib/apt/lists/* + +# Set environment variables +ENV PATH="/usr/local/bin:$PATH" + +# Ensure the ownership of the Jenkins agent home directory is set to the Jenkins user +RUN chown -R jenkins:jenkins "${JENKINS_AGENT_HOME}" diff --git a/extract-profiles.sh b/extract-profiles.sh new file mode 100755 index 00000000..5e09eaa8 --- /dev/null +++ b/extract-profiles.sh @@ -0,0 +1,51 @@ +#!/bin/bash +set -e + +original_file="docker-compose.yaml" + +# Function to add a service and its dependencies to the included_services list +add_service_and_dependencies() { + local service=$1 + # Mark the service as included + included_services["$service"]=1 + # Check if the service has dependencies + if yq e ".services.${service}.depends_on" "$original_file" -e > /dev/null; then + # Read dependencies of the service + local dependencies=($(yq e ".services.${service}.depends_on | keys" "$original_file" -o json | jq -r '.[]')) + # Recursively add dependencies + for dependency in "${dependencies[@]}"; do + if [[ -z "${included_services["$dependency"]}" ]]; then + add_service_and_dependencies "$dependency" + fi + done + fi +} + +# Step 1: Collect all dependencies +declare -A all_dependencies +services=$(yq e '.services | keys' "$original_file" -o json | jq -r '.[]') +for service in $services; do + dependencies=$(yq e ".services.$service.depends_on | keys" "$original_file" -o json | jq -r '.[]') + for dependency in $dependencies; do + all_dependencies["$dependency"]=1 + done +done + +# Step 2: Process each profile and include dependencies +for profile in $(yq e '.services[].profiles[]?' "$original_file" | sort -u); do + echo "Processing profile: $profile" + # Initialize an associative array to track included services + declare -A included_services + # Find and include services matching the profile + matching_services=$(yq e ".services | with_entries(select(.value.profiles[]? == \"$profile\")) | keys" "$original_file" -o json | jq -r '.[]') + for service in $matching_services; do + add_service_and_dependencies "$service" + done + # Correctly format the list of included services for yq query + included_services_keys=$(printf "'%s'," "${!included_services[@]}") + included_services_keys="[${included_services_keys%,}]" # Remove trailing comma and wrap in brackets + + # Generate the docker-compose file for the profile + echo "Generating docker-compose-$profile.yaml" + yq e ".services | with_entries(select(.key as \$k | .key == \"$included_services_list\"))" "$original_file" > "docker-compose-$profile.yaml" +done diff --git a/updatecli/updatecli.d/ssh-agent.yaml b/updatecli/updatecli.d/ssh-agent.yaml index bef372b4..602dd108 100644 --- a/updatecli/updatecli.d/ssh-agent.yaml +++ b/updatecli/updatecli.d/ssh-agent.yaml @@ -85,6 +85,16 @@ targets: matcher: jenkins/ssh-agent sourceid: jenkins/ssh-agent scmid: default + jenkins/cpp-agent: + name: '[jenkins/ssh-agent] Bump Docker image tag in "dockerfiles/cpp/Dockerfile"' + kind: dockerfile + spec: + file: dockerfiles/cpp/Dockerfile + instruction: + keyword: FROM + matcher: jenkins/ssh-agent + sourceid: jenkins/ssh-agent + scmid: default default-agent: name: '[jenkins/ssh-agent] Bump Docker image tag in "docker-compose.yaml"' kind: yaml