Skip to content

Instantly share code, notes, and snippets.

@Jeansen
Last active February 14, 2024 18:10
Show Gist options
  • Save Jeansen/952eac5055870fc2847237b1e5facf0a to your computer and use it in GitHub Desktop.
Save Jeansen/952eac5055870fc2847237b1e5facf0a to your computer and use it in GitHub Desktop.
Gitea Jenkins Ambassador
#Create this file in /etc/systemd/system/gitea_jenkins_ambassador.service
#Enable service with: sudo systemctl enable gitea_jenkins_ambassador.service
#Start service with: sudo systemctl start gitea_jenkins_ambassador.service
#Check all ist fine: sudo sytemctl status gitea_jenkins_ambassador.service
[Unit]
Description=Wrapper for GET hooks from systemd to Jenkins
[Service]
ExecStart=/bin/bash /usr/local/bin/gitea_jenkins_ambassador.sh
[Install]
WantedBy=multi-user.target
#!/bin/bash
#Simple script that listens for any incomming GET request from a Gitea Webhook, cuts off the payload and calls
#the real webhook to a Jenkins Job. This is a workaround because there is - as of Feb. 2024 - no webhook support
#for Jenkins in Gitea itself.
#Install with e.g.: sudo install ./gitea_jenkins_ambassador.sh /usr/local/bin/gitea_jenkins_ambassador.sh
#Also see the simple Systemd Service, so this script is started automatically.
#With this setup, create or change a Gitea webhook to a Jenkins job the same way, as you would when calling Jenkins
#directly, with all the parameters etc. Only the host and port needs to be to where this script is running.
#It would be best to have this script running on the same server, where Jenkins is installed. This way the webhook URLs
#would only change with respect to their prots.
#Finally, make sure you review the curl command and the end of this scipt. This is the part, where you put your real
#Jenkins server host.
### Create the response FIFO
rm -f response
mkfifo response
function handleRequest() {
while read line; do
echo $line
trline=$(echo $line | tr -d '[\r\n]')
[ -z "$trline" ] && break
HEADLINE_REGEX='(.*?)\s(.*?)\sHTTP.*?'
[[ "$trline" =~ $HEADLINE_REGEX ]] &&
REQUEST=$(echo $trline | sed -E "s/$HEADLINE_REGEX/\2/")
done
# echo -e 'HTTP/1.1 200\r\n\r\n\r\n</h1>PONG</h1>' > response
get=$(echo "$REQUEST" | sed 's/payload=.*&//')
##Chang to your server!
curl https://build:8443$get && echo -e "HTTP/1.1 200\r\n" > response
}
echo 'Listening on 3000...'
while true; do
cat response | nc -ln 3000 | handleRequest
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment