Skip to content
This repository was archived by the owner on Mar 14, 2023. It is now read-only.

Commit 2a4a8cb

Browse files
author
Bernhard Grünewaldt
committed
doc
1 parent 09a3ec8 commit 2a4a8cb

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,32 @@ magically by convention over configuration.
7575

7676
<p align="center"><img src="https://codeclou.github.io/jenkins-github-webhook-build-trigger-plugin/img/jenkins-job-config---with-overlays.png" width="80%"></p>
7777

78+
&nsbp;
7879

80+
**Available Environment Variables from Webhook**
81+
82+
| Variable | Description | Example |
83+
|----------|-------------|---------|
84+
| `$GWBT_COMMIT_BEFORE` | `before` commit id as sha1 hash from Webhook Payload, specifying the commit revision the repository was in before the event happened. | `3be1cb4b6b86533b5dab2b0083fa9fb8b401b430` or <br> `0000000000000000000` if push event was a tag |
85+
| `$GWBT_COMMIT_AFTER` | `after` commit id as sha1 hash from Webhook Payload, specifying the commit revision the repository is now in. Meaning the current revision. | `2c9522c9618864808eaaede8353dbeafb996c605` |
86+
| `$GWBT_REF` | `ref` from Webhook Payload representing the branch or tag that was pushed | `refs/heads/{branchname}` or <br> `refs/tags/{tagname}` |
87+
| `$GWBT_TAG` | short tag name derived from `ref` and stripped of clutter. | When `ref` is `refs/tags/1.0.0` then it is `1.0.0`. <br>When `ref` is not a tag, it is empty! |
88+
| `$GWBT_BRANCH` | short branch name derived from `ref` and stripped of clutter. | When `ref` is `refs/heads/master` then it is `master`. <br>When `ref` is not a branch, it is empty! |
89+
| `$GWBT_REPO_CLONE_URL` | GitHub repository clone url. | `https://github.com/{repoOwner}/{repoName}.git` <br> e.g. `https://github.com/codeclou/jenkins-github-webhook-build-trigger-plugin.git` |
90+
| `$GWBT_REPO_HTML_URL` | GitHub repository browser url. | `https://github.com/{repoOwner}/{repoName}` <br> e.g. `https://github.com/codeclou/jenkins-github-webhook-build-trigger-plugin` |
91+
| `$GWBT_REPO_FULL_NAME` | GitHub repository full name | `{repoOwner}/{repoName}` <br> e.g. `codeclou/jenkins-github-webhook-build-trigger-plugin` |
92+
| `$GWBT_REPO_NAME` | GitHub repository full name | `{repoName}` <br> e.g. `jenkins-github-webhook-build-trigger-plugin` |
93+
94+
95+
&nsbp;
96+
97+
**Example Build Script Snippet**
7998

80-
Available variables:
8199
```bash
82100
#!/bin/bash
83101

102+
set -e
103+
84104
echo "GWBT_COMMIT_BEFORE: $GWBT_COMMIT_BEFORE"
85105
echo "GWBT_COMMIT_AFTER: $GWBT_COMMIT_AFTER"
86106
echo "GWBT_REF: $GWBT_REF"
@@ -90,7 +110,74 @@ echo "GWBT_REPO_CLONE_URL: $GWBT_REPO_CLONE_URL"
90110
echo "GWBT_REPO_HTML_URL: $GWBT_REPO_HTML_URL"
91111
echo "GWBT_REPO_FULL_NAME: $GWBT_REPO_FULL_NAME"
92112
echo "GWBT_REPO_NAME: $GWBT_REPO_NAME"
113+
114+
#
115+
# Cleanup before run
116+
#
117+
rm -rf $WORKSPACE/\.git || true
118+
rm -rf $WORKSPACE/* || true
119+
cd $WORKSPACE
120+
121+
#
122+
# Prevent manual Job starts
123+
#
124+
if [[ -z "$GWBT_COMMIT_AFTER" ]]
125+
then
126+
echo "I DON'T WANT JOBS STARTED MANUALLY! ONLY VIA GITHUB WEBHOOK!"
127+
exit 1
128+
fi
129+
130+
131+
#
132+
# Only Build Branches
133+
#
134+
if [ -z "$GWBT_BRANCH" ]
135+
then
136+
echo "THIS PUSH IS NOT INSIDE A BRANCH. I DON'T LIKE IT!"
137+
exit 1
138+
fi
139+
140+
#
141+
# Clone specific branch
142+
#
143+
git clone --single-branch \
144+
--branch $GWBT_BRANCH \
145+
https://github.com/${GWBT_REPO_FULL_NAME}.git \
146+
source
147+
148+
#
149+
# Switch to specific revision
150+
#
151+
cd source
152+
git reset --hard $GWBT_COMMIT_AFTER
153+
154+
#
155+
# Trigger build script inside cloned repository
156+
#
157+
bash jenkins.sh
158+
```
159+
160+
161+
&nsbp;
162+
163+
**Example Build Script Snippet for Cloning Private Repositories**
164+
165+
It is best to use [Personal Access Tokens](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/).
166+
Put a Global Environment Variable named `GITHUB_AUTH_TOKEN` in your Jenkins Configuration or specify at Job level.
167+
168+
Then you can clone a private repository like this:
169+
170+
```bash
171+
#
172+
# Clone specific branch
173+
#
174+
git clone --single-branch \
175+
--branch $GWBT_BRANCH \
176+
https://${GITHUB_AUTH_TOKEN}@github.com/${GWBT_REPO_FULL_NAME}.git \
177+
source
93178
```
179+
180+
94181
-----
95182

96183
&nbsp;

0 commit comments

Comments
 (0)