Skip to content

Commit b0abbfa

Browse files
authored
Fix Docker build versioning (#20077)
* Centralise version scripts and fix Docker version * Refactor generation of a git-hash-based version into get-version-from-git * Refactor normalization of versions (stripping leading v) into normalize-version.sh * Call get-version-from-git from ci_package.sh, call normalize-version from package.sh * Refactor docker-write-version.sh into docker-package.sh, which both writes the version file and invokes yarn build passing VERSION * Normalize the version received from the server
1 parent b3c5bb8 commit b0abbfa

File tree

7 files changed

+36
-36
lines changed

7 files changed

+36
-36
lines changed

Dockerfile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ WORKDIR /src
1616
COPY . /src
1717
RUN dos2unix /src/scripts/docker-link-repos.sh && bash /src/scripts/docker-link-repos.sh
1818
RUN yarn --network-timeout=100000 install
19-
RUN yarn build
19+
20+
RUN dos2unix /src/scripts/docker-package.sh && bash /src/scripts/docker-package.sh
2021

2122
# Copy the config now so that we don't create another layer in the app image
2223
RUN cp /src/config.sample.json /src/webapp/config.json
2324

24-
# Ensure we populate the version file
25-
RUN dos2unix /src/scripts/docker-write-version.sh && bash /src/scripts/docker-write-version.sh
26-
27-
2825
# App
2926
FROM nginx:alpine
3027

scripts/ci_package.sh

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
#!/bin/bash
22

3-
# Runs package.sh setting the version to git hashes of the element-web,
4-
# react-sdk & js-sdk checkouts, for the case where these dependencies
5-
# are git checkouts.
3+
# Runs package.sh, passing DIST_VERSION determined by git
64

75
set -ex
86

97
rm dist/element-*.tar.gz || true # rm previous artifacts without failing if it doesn't exist
108

11-
# Since the deps are fetched from git, we can rev-parse
12-
REACT_SHA=$(cd node_modules/matrix-react-sdk; git rev-parse --short=12 HEAD)
13-
JSSDK_SHA=$(cd node_modules/matrix-js-sdk; git rev-parse --short=12 HEAD)
9+
DIST_VERSION=`$(dirname $0)/get-version-from-git.sh`
1410

15-
VECTOR_SHA=$(git rev-parse --short=12 HEAD) # use the ACTUAL SHA rather than assume develop
16-
17-
CI_PACKAGE=true DIST_VERSION=$VECTOR_SHA-react-$REACT_SHA-js-$JSSDK_SHA scripts/package.sh
11+
CI_PACKAGE=true DIST_VERSION=$DIST_VERSION scripts/package.sh

scripts/docker-write-version.sh renamed to scripts/docker-package.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ DIST_VERSION=$TAG
1313
# for an appropriately tagged branch as well (heads/v1.2.3).
1414
if [[ $BRANCH != HEAD && ! $BRANCH =~ heads/v.+ ]]
1515
then
16-
REACT_SHA=$(cd node_modules/matrix-react-sdk; git rev-parse --short=12 HEAD)
17-
JSSDK_SHA=$(cd node_modules/matrix-js-sdk; git rev-parse --short=12 HEAD)
18-
VECTOR_SHA=$(git rev-parse --short=12 HEAD) # use the ACTUAL SHA rather than assume develop
19-
DIST_VERSION=$VECTOR_SHA-react-$REACT_SHA-js-$JSSDK_SHA
16+
DIST_VERSION=`$(dirname $0)/get-version-from-git.sh`
2017
fi
2118

19+
DIST_VERSION=`$(dirname $0)/normalize-version.sh ${DIST_VERSION}`
20+
VERSION=$DIST_VERSION yarn build
2221
echo $DIST_VERSION > /src/webapp/version

scripts/get-version-from-git.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
# Echoes a version based on the git hashes of the element-web, react-sdk & js-sdk checkouts, for the case where
4+
# these dependencies are git checkouts.
5+
6+
# Since the deps are fetched from git, we can rev-parse
7+
REACT_SHA=$(cd node_modules/matrix-react-sdk; git rev-parse --short=12 HEAD)
8+
JSSDK_SHA=$(cd node_modules/matrix-js-sdk; git rev-parse --short=12 HEAD)
9+
VECTOR_SHA=$(git rev-parse --short=12 HEAD) # use the ACTUAL SHA rather than assume develop
10+
echo $VECTOR_SHA-react-$REACT_SHA-js-$JSSDK_SHA

scripts/normalize-version.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
# If $1 looks like v1.2.3 or v1.2.3-foo, strip the leading v, then print it to stdout
4+
if [[ $1 =~ ^v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+(-.+)?$ ]]; then
5+
echo ${1:1}
6+
else
7+
echo $1
8+
fi

scripts/package.sh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ cp -r webapp element-$version
2121
# Just in case you have a local config, remove it before packaging
2222
rm element-$version/config.json || true
2323

24-
# if $version looks like semver with leading v, strip it before writing to file
25-
if [[ ${version} =~ ^v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+(-.+)?$ ]]; then
26-
echo ${version:1} > element-$version/version
27-
else
28-
echo ${version} > element-$version/version
29-
fi
24+
$(dirname $0)/normalize-version.sh ${version} > element-$version/version
3025

3126
tar chvzf dist/element-$version.tar.gz element-$version
3227
rm -r element-$version

src/vector/platform/WebPlatform.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export default class WebPlatform extends VectorBasePlatform {
107107
// presence of intermediate caching proxies), but still: we're trying
108108
// to tell the user that there is a new version.
109109

110-
return new Promise(function(resolve, reject) {
110+
return new Promise((resolve, reject) => {
111111
request(
112112
{
113113
method: "GET",
@@ -121,27 +121,24 @@ export default class WebPlatform extends VectorBasePlatform {
121121
return;
122122
}
123123

124-
const ver = body.trim();
125-
resolve(ver);
124+
resolve(this.getNormalizedAppVersion(body.trim()));
126125
},
127126
);
128127
});
129128
}
130129

131-
getNormalizedAppVersion(): string {
132-
let ver = process.env.VERSION;
133-
130+
getNormalizedAppVersion(version: string): string {
134131
// if version looks like semver with leading v, strip it
135-
// (matches scripts/package.sh)
132+
// (matches scripts/normalize-version.sh)
136133
const semVerRegex = new RegExp("^v[0-9]+.[0-9]+.[0-9]+(-.+)?$");
137-
if (semVerRegex.test(process.env.VERSION)) {
138-
ver = process.env.VERSION.substr(1);
134+
if (semVerRegex.test(version)) {
135+
return version.substr(1);
139136
}
140-
return ver;
137+
return version;
141138
}
142139

143140
getAppVersion(): Promise<string> {
144-
return Promise.resolve(this.getNormalizedAppVersion());
141+
return Promise.resolve(this.getNormalizedAppVersion(process.env.VERSION));
145142
}
146143

147144
startUpdater() {
@@ -155,7 +152,7 @@ export default class WebPlatform extends VectorBasePlatform {
155152

156153
pollForUpdate = () => {
157154
return this.getMostRecentVersion().then((mostRecentVersion) => {
158-
const currentVersion = this.getNormalizedAppVersion();
155+
const currentVersion = this.getNormalizedAppVersion(process.env.VERSION);
159156

160157
if (currentVersion !== mostRecentVersion) {
161158
if (this.shouldShowUpdate(mostRecentVersion)) {

0 commit comments

Comments
 (0)