FROM jenkins/jenkins:lts

USER root

# Install necessary packages
RUN apt-get update && apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release \
    bzr \
    ant \
    gradle \
    git

# Install Docker
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg && \
    echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
    $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null && \
    apt-get update && \
    apt-get install -y docker-ce docker-ce-cli containerd.io

# Add jenkins user to docker group
RUN usermod -aG docker jenkins

# Copy configuration scripts
COPY jenkins_init/ /usr/share/jenkins/ref/init.groovy.d/

# Copy plugins list
COPY jenkins_cfg/plugins.txt /usr/share/jenkins/ref/plugins.txt

# Install plugins
RUN jenkins-plugin-cli -f /usr/share/jenkins/ref/plugins.txt

# Copy job configurations
COPY jenkins_cfg/jobs/ /usr/share/jenkins/ref/jobs/

# Add the custom entrypoint script
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

USER jenkins

ARG JENKINS_ADMIN_ID
ARG JENKINS_ADMIN_PASSWORD

ENV JENKINS_ADMIN_ID=${JENKINS_ADMIN_ID}
ENV JENKINS_ADMIN_PASSWORD=${JENKINS_ADMIN_PASSWORD}

# Override the entrypoint to use the custom script
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]