Skip to content

Commit

Permalink
Update demo to include gitea for multibranch support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlatombe committed Jan 16, 2023
1 parent 6451c6d commit 12b97db
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 58 deletions.
25 changes: 12 additions & 13 deletions demo/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
FROM jenkins/jenkins:2.319.3
FROM jenkins/jenkins:2.375.2

USER root

ENV MAVEN_VERSION=3.8.4
ENV MAVEN_VERSION=3.8.7
RUN curl -s https://archive.apache.org/dist/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz | tar xvfCz - /opt && \
ln -sv /opt/apache-maven-$MAVEN_VERSION/bin/mvn /usr/local/bin/mvn

ADD repo /tmp/repo
COPY gen.sh /tmp/
ADD lib /tmp/lib
COPY plugins /usr/share/jenkins/ref/plugins
RUN chown -R jenkins.jenkins /tmp/repo /tmp/lib /usr/share/jenkins/ref/plugins
RUN mkdir -p /m2repo

RUN chown -R jenkins.jenkins /tmp/lib /m2repo

USER jenkins

RUN cd /tmp/repo && \
bash ../gen.sh && \
git init && \
git add . && \
git -c [email protected] -c user.name="Parallel Test Executor Demo" commit -m 'demo' && \
cd /tmp/lib && \
COPY target/test-classes/test-dependencies/*.hpi /usr/share/jenkins/ref/plugins/

RUN cd /tmp/lib && \
git init && \
git add . && \
git -c [email protected] -c user.name="Parallel Test Executor Demo" commit -m 'demo'

# TODO without this JENKINS-24752 workaround, it takes too long to provision.
# (Do not add hudson.model.LoadStatistics.decay=0.1; in that case we overprovision slaves which never get used, and OnceRetentionStrategy.check disconnects them after an idle timeout.)
ENV JAVA_OPTS -Dhudson.model.LoadStatistics.clock=1000 -Dhudson.Main.development=true
ENV JAVA_OPTS -Dhudson.model.LoadStatistics.clock=1000 -Dhudson.Main.development=true -Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true

ADD JENKINS_HOME /usr/share/jenkins/ref

HEALTHCHECK --interval=5m --timeout=3s \
CMD curl -f http://localhost:8080/ || exit 1
20 changes: 20 additions & 0 deletions demo/JENKINS_HOME/credentials.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version='1.1' encoding='UTF-8'?>
<com.cloudbees.plugins.credentials.SystemCredentialsProvider plugin="[email protected]">
<domainCredentialsMap class="hudson.util.CopyOnWriteMap$Hash">
<entry>
<com.cloudbees.plugins.credentials.domains.Domain>
<specifications/>
</com.cloudbees.plugins.credentials.domains.Domain>
<java.util.concurrent.CopyOnWriteArrayList>
<com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl plugin="[email protected]">
<scope>GLOBAL</scope>
<id>gitea</id>
<description/>
<username>jenkins</username>
<password>SECRET</password>
<usernameSecret>false</usernameSecret>
</com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
</java.util.concurrent.CopyOnWriteArrayList>
</entry>
</domainCredentialsMap>
</com.cloudbees.plugins.credentials.SystemCredentialsProvider>
26 changes: 19 additions & 7 deletions demo/JENKINS_HOME/jobs/pipeline/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@
<actions/>
<description></description>
<sources>
<jenkins.branch.BranchSource>
<source class="jenkins.plugins.git.GitSCMSource">
<remote>/tmp/repo</remote>
<credentialsId></credentialsId>
<includes>*</includes>
<excludes></excludes>
<ignoreOnPushNotifications>false</ignoreOnPushNotifications>
<jenkins.branch.BranchSource plugin="[email protected]_f6cc">
<source class="org.jenkinsci.plugin.gitea.GiteaSCMSource" plugin="[email protected]">
<id>6126b51e-8b62-413c-9e82-333d0cf58bdb</id>
<serverUrl>http://gitea:3000</serverUrl>
<repoOwner>jenkins</repoOwner>
<repository>demo</repository>
<credentialsId>gitea</credentialsId>
<traits>
<org.jenkinsci.plugin.gitea.BranchDiscoveryTrait>
<strategyId>1</strategyId>
</org.jenkinsci.plugin.gitea.BranchDiscoveryTrait>
<org.jenkinsci.plugin.gitea.OriginPullRequestDiscoveryTrait>
<strategyId>1</strategyId>
</org.jenkinsci.plugin.gitea.OriginPullRequestDiscoveryTrait>
<org.jenkinsci.plugin.gitea.ForkPullRequestDiscoveryTrait>
<strategyId>1</strategyId>
<trust class="org.jenkinsci.plugin.gitea.ForkPullRequestDiscoveryTrait$TrustContributors"/>
</org.jenkinsci.plugin.gitea.ForkPullRequestDiscoveryTrait>
</traits>
</source>
<strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
<properties class="empty-list"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version='1.1' encoding='UTF-8'?>
<org.jenkinsci.plugin.gitea.servers.GiteaServers plugin="[email protected]">
<servers>
<org.jenkinsci.plugin.gitea.servers.GiteaServer>
<displayName>gitea</displayName>
<serverUrl>http://gitea:3000</serverUrl>
<manageHooks>false</manageHooks>
<credentialsId>gitea</credentialsId>
</org.jenkinsci.plugin.gitea.servers.GiteaServer>
</servers>
</org.jenkinsci.plugin.gitea.servers.GiteaServers>
27 changes: 11 additions & 16 deletions demo/Makefile
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
IMAGE=parallel-test-executor-demo
DOCKER_RUN=docker run --rm -p 127.0.0.1:8080:8080 -v m2repo:/m2repo --group-add=$(shell stat -c %g /var/run/docker.sock) -ti

PHONY: validate
validate:
mvn -Dtest=InjectedTest clean test

PHONY: copy-plugins
copy-plugins:
if [ \! -f ../target/parallel-test-executor.hpi ]; then mvn -f .. -DskipTests install; fi
if [ \! -f ../target/parallel-test-executor.hpi ]; then mvn -f .. -Pquick-build install; fi
if [ \! -f target/test-classes/test-dependencies/index -o \
pom.xml -nt target/test-classes/test-dependencies/index -o \
../target/parallel-test-executor.hpi -nt target/test-classes/test-dependencies/parallel-test-executor.hpi ]; then \
mvn clean validate hpi:resolve-test-dependencies; fi
@# TODO would be more efficient to move Dockerfile and all it references into a subdirectory, or use .dockerignore
rm -rf plugins
mkdir plugins
cp -v target/test-classes/test-dependencies/*.hpi plugins

build: copy-plugins
docker build -t $(IMAGE) .

# http://stackoverflow.com/q/23544282/12916 unclear how best to ensure that the jenkins user can write to this volume
volume:
docker volume create --name=m2repo
docker run --rm -v m2repo:/m2repo ubuntu chmod -v a+rw /m2repo
PHONY: clean
clean:
rm -rf target
docker-compose down -v
docker image rm demo-jenkins || true

run: build volume
$(DOCKER_RUN) $(IMAGE)
PHONY: run
run: copy-plugins
./run.sh
21 changes: 21 additions & 0 deletions demo/create-pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -euxo pipefail
export TOKEN=$(cat target/gitea_token.txt)
pushd target/repo > /dev/null
export BRANCH_NAME=experiment-$(openssl rand -hex 6)
export TARGET_BRANCH=main
git checkout -b "$BRANCH_NAME"
git -c [email protected] -c user.name="Parallel Test Executor Demo" commit --allow-empty -m "Empty commit"
git push
curl -X 'POST' \
'http://localhost:3000/api/v1/repos/jenkins/demo/pulls' \
-H "Authorization: token $TOKEN" \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d "{
\"base\": \"$TARGET_BRANCH\",
\"title\": \"A pull request from $BRANCH_NAME\",
\"body\": \"Some description\",
\"head\": \"$BRANCH_NAME\"
}"
popd > /dev/null
8 changes: 8 additions & 0 deletions demo/credentials.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl plugin="[email protected]">
<scope>GLOBAL</scope>
<id>gitea</id>
<description/>
<username>jenkins</username>
<password>SECRET</password>
<usernameSecret>false</usernameSecret>
</com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
45 changes: 45 additions & 0 deletions demo/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: "3.9"
networks:
demo:
external: false
volumes:
m2repo:
gitea:
services:
gitea:
image: gitea/gitea:1.18.0
container_name: gitea
environment:
- USER_UID=1000
- USER_GID=1000
- GITEA__server__HTTP_PORT=3000
- GITEA__server__SSH_PORT=22
- GITEA__server__ROOT_URL=http://localhost:3000/
- GITEA__server__DOMAIN=localhost
- GITEA__database__DB_TYPE=sqlite3
- GITEA__security__INSTALL_LOCK=true
restart: always
networks:
- demo
volumes:
- gitea:/data
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "3000:3000"
- "222:22"
jenkins:
container_name: jenkins
networks:
- demo
build:
context: .
ports:
- "8080:8080"
volumes:
- m2repo:/m2repo:rw
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:8080" ]
interval: 5s
timeout: 3s

33 changes: 11 additions & 22 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.34</version>
<version>4.53</version>
<relativePath />
</parent>
<artifactId>parallel-test-executor-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<properties>
<jenkins.version>2.319.3</jenkins.version>
<java.level>8</java.level>
<jenkins.version>2.361.4</jenkins.version>
</properties>
<name>Parallel Test Executor Demo</name>
<repositories>
Expand All @@ -32,7 +31,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>parallel-test-executor</artifactId>
<version>1.13</version>
<version>999999-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<!-- cf. https://stackoverflow.com/q/45761521/12916 -->
Expand Down Expand Up @@ -111,32 +110,22 @@
<version>82.vc2a_3f0f7a_291</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>gitea</artifactId>
<version>1.4.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.319.x</artifactId>
<version>1148.v7261f385f859</version>
<artifactId>bom-2.361.x</artifactId>
<version>1775.vf0577a_a_01778</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>parameterized-trigger</artifactId>
<version>2.43</version>
<exclusions>
<exclusion>
<groupId>org.jenkins-ci</groupId>
<artifactId>symbol-annotation</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>maven-plugin</artifactId>
<version>3.16</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
39 changes: 39 additions & 0 deletions demo/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
set -euo pipefail

docker compose up -d --wait
mkdir -p target
docker-compose exec -u 1000:1000 gitea gitea admin user create --admin --username jenkins --random-password --email [email protected] > target/gitea_output.txt || true
docker-compose exec -u 1000:1000 gitea gitea admin user generate-access-token --username jenkins --raw > target/gitea_token.txt
export TOKEN=$(cat target/gitea_token.txt)
wget -O target/jenkins-cli.jar -o /dev/null http://localhost:8080/jnlpJars/jenkins-cli.jar
sed -e "s/SECRET/$TOKEN/" credentials.xml | java -jar target/jenkins-cli.jar -s http://localhost:8080/ update-credentials-by-xml "SystemCredentialsProvider::SystemContextResolver::jenkins" "(global)" gitea

curl -X 'POST' \
'http://localhost:3000/api/v1/user/repos' \
-H "Authorization: token $TOKEN" \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"auto_init": true,
"default_branch": "main",
"name": "demo"
}'

git clone http://jenkins:$TOKEN@localhost:3000/jenkins/demo.git target/repo
cp -R repo target/
pushd target/repo
bash ../../gen.sh
git add .
git -c [email protected] -c user.name="Parallel Test Executor Demo" commit -m "Initial commit"
git push origin -u
popd

GITEA_USERNAME=$(grep "New user" < target/gitea_output.txt | cut -f 2 -d "'")
GITEA_PASSWORD=$(grep password < target/gitea_output.txt | cut -f 2 -d "'")

echo "Demo initialized"
echo "Gitea is available on http://localhost:3000 using $GITEA_USERNAME $GITEA_PASSWORD"
echo "Jenkins is available on http://localhost:8080"
echo "The demo git repo is available in target/repo and can be accessed at http://localhost:3000/jenkins/demo"
open http://localhost:8080/

0 comments on commit 12b97db

Please sign in to comment.