Skip to content

Commit fb0886b

Browse files
committed
ci: trigger Jenkins from release tooling commits
1 parent 7ddfddf commit fb0886b

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎Jenkinsfile‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@ pipeline {
77
timeout(time: 45, unit: 'MINUTES')
88
}
99

10+
parameters {
11+
choice(name: 'TRIGGER_SOURCE', choices: ['manual', 'post-commit'], description: 'Build origin')
12+
}
13+
1014
stages {
15+
stage('Trigger contract') {
16+
steps {
17+
script {
18+
if (!(params.TRIGGER_SOURCE in ['manual', 'post-commit'])) {
19+
error("unsupported trigger source: ${params.TRIGGER_SOURCE}")
20+
}
21+
}
22+
}
23+
}
1124
stage('Build') {
1225
steps { sh 'scripts/build.sh' }
1326
}

‎scripts/jenkins-local-job.sh‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ cli() {
2323
fi
2424
}
2525

26+
install_post_commit_hook() {
27+
local hooks hook temporary
28+
hooks="$root/.git/hooks"
29+
hook="$hooks/post-commit"
30+
mkdir -p "$hooks"
31+
temporary="$(mktemp "$hooks/.post-commit.XXXXXX")"
32+
cat > "$temporary" <<'EOF'
33+
#!/usr/bin/env bash
34+
set -u
35+
root="$(git rev-parse --show-toplevel 2>/dev/null)" || exit 0
36+
[[ "$(git -C "$root" branch --show-current 2>/dev/null)" == "release-tooling" ]] || exit 0
37+
nohup "$root/scripts/jenkins-local-job.sh" trigger-hook >/dev/null 2>&1 &
38+
EOF
39+
chmod 700 "$temporary"
40+
mv "$temporary" "$hook"
41+
}
42+
2643
case "${1:-}" in
2744
configure)
2845
mkdir -p "$(dirname "$cli")"
@@ -34,14 +51,18 @@ case "${1:-}" in
3451
else
3552
cli create-job "$job_name" < "$root/scripts/jenkins-local-job.xml"
3653
fi
54+
install_post_commit_hook
3755
echo "configured $job_name"
3856
;;
3957
inspect)
4058
test -r "$config" && rg -n '<triggers|pollSCM|release-tooling|file://' "$config"
4159
;;
4260
trigger)
43-
cli build "$job_name" -s
61+
cli build "$job_name" -p TRIGGER_SOURCE=manual -s
4462
echo "triggered $jenkins_url/job/$job_name/"
4563
;;
46-
*) echo "usage: $0 configure|inspect|trigger" >&2; exit 2 ;;
64+
trigger-hook)
65+
cli build "$job_name" -p TRIGGER_SOURCE=post-commit
66+
;;
67+
*) echo "usage: $0 configure|inspect|trigger|trigger-hook" >&2; exit 2 ;;
4768
esac

‎scripts/jenkins-local-job.xml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
<flow-definition plugin="workflow-job">
33
<description>Script-triggered codebase-memory-cli release-tooling build and test.</description>
44
<properties>
5+
<hudson.model.ParametersDefinitionProperty>
6+
<parameterDefinitions>
7+
<hudson.model.StringParameterDefinition>
8+
<name>TRIGGER_SOURCE</name>
9+
<defaultValue>manual</defaultValue>
10+
<description>Build origin; post-commit hooks use post-commit.</description>
11+
<trim>true</trim>
12+
</hudson.model.StringParameterDefinition>
13+
</parameterDefinitions>
14+
</hudson.model.ParametersDefinitionProperty>
515
<org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty>
616
<abortPrevious>false</abortPrevious>
717
</org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty>

‎tests/test_jenkins_artifact_contract.sh‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set -euo pipefail
33

44
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
55
pipeline="$root/Jenkinsfile"
6+
job_script="$root/scripts/jenkins-local-job.sh"
67

78
for required in \
89
"Archive Linux CLI artifact" \
@@ -15,3 +16,14 @@ for required in \
1516
exit 1
1617
}
1718
done
19+
20+
for required in "TRIGGER_SOURCE" "post-commit"; do
21+
grep -Fq "$required" "$pipeline" || {
22+
echo "missing Jenkins trigger contract: $required" >&2
23+
exit 1
24+
}
25+
grep -Fq "$required" "$job_script" || {
26+
echo "missing post-commit hook contract: $required" >&2
27+
exit 1
28+
}
29+
done

0 commit comments

Comments
 (0)