summaryrefslogtreecommitdiff
path: root/commit-repo-tag.sh
blob: 84998521f41ead70e580eab591d5b5b9f0116b3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash

# Script for GStreamer Jenkins CI

# start        : default branch
# build branch : jenkins-build-branch

# Create a new repo manifest file based on the default one, but containing
# the current git revision of all used projects. The new manifest is meant
# to be used for tracking the code used during each Jenkins build.
# The new manifest is tagged with the build number and pushed to a git branch.

# param: <jenkins build number> - Jenkins build number for the current job

# Important: repo sync should have been done before

# Build branch where new manifest will be stored
if [ -z $BUILD_BRANCH ];
then
    BUILD_BRANCH=jenkins-build-branch
fi

if [ -z $BUILD_PREFIX ];
then
    BUILD_PREFIX=
else
    BUILD_PREFIX+="_"
fi

# The default manifest file name
MANIFEST_FILENAME=default.xml

# Temporary file used to store the new manifest
TMP_M_FILENAME=`mktemp --suffix=.xml`

if [ -z $WORKSPACE ];
then
    echo "Out-of-jenkins build"
    if [ $# = 0 ];
    then
	echo "Usage: $0 <jenkins build number>"
	exit 42
    fi
    BUILD_ID=$1
fi

if [ ! -d ".repo" ];
then
    echo "Cannot find a repo directory. Aborting..."
    exit
fi

echo
echo "======================================================"
echo

# Gets the current repo root dir
ROOT_DIR=`pwd`

echo "Generating new manifest file for BUILD_ID $BUILD_ID [$BUILD_PREFIX]"

# Create new manifest with all revisions
repo manifest -r -o $TMP_M_FILENAME

# Change to build branch and add the new manifest
cd $ROOT_DIR/.repo/manifests
git checkout $BUILD_BRANCH
cp $TMP_M_FILENAME $MANIFEST_FILENAME

# Remove temporary file
rm -fr $TMP_M_FILENAME

# Temporary file used to store the new changes for commiting
TMP_C_FILENAME=`mktemp --suffix=.msg`

# Store changes for commiting
git diff --exit-code $MANIFEST_FILENAME > $TMP_C_FILENAME

# Check if there's something new to commit
if [ $? = 1 ];
then
    # Commit new manifest file
    git commit -F $TMP_C_FILENAME -- $MANIFEST_FILENAME || true

    echo "Tagging manifest for storage."

    # Tag the new manifest version
    CUR_DATE=`date +%Y%m%d`
    git tag -a "build_${BUILD_PREFIX}$BUILD_ID" \
     -m "Tagging manifest build $BUILD_ID"

    echo "Pushing new manifest to the branch $BUILD_BRANCH"

    # Push new manifest to the build branch
    git push -v --tags origin $BUILD_BRANCH
else
    echo "No changes to push"
fi

# Remove temporary file
rm -fr $TMP_C_FILENAME

# Move back to the default branch
git checkout default

# Go back to the root directory
cd $ROOT_DIR

echo
echo "Done."
echo "======================================================"