Skip to content

Commit

Permalink
Fix Docker build versioning (#20077)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
novocaine authored Dec 7, 2021
1 parent b3c5bb8 commit b0abbfa
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 36 deletions.
7 changes: 2 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ WORKDIR /src
COPY . /src
RUN dos2unix /src/scripts/docker-link-repos.sh && bash /src/scripts/docker-link-repos.sh
RUN yarn --network-timeout=100000 install
RUN yarn build

RUN dos2unix /src/scripts/docker-package.sh && bash /src/scripts/docker-package.sh

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

# Ensure we populate the version file
RUN dos2unix /src/scripts/docker-write-version.sh && bash /src/scripts/docker-write-version.sh


# App
FROM nginx:alpine

Expand Down
12 changes: 3 additions & 9 deletions scripts/ci_package.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#!/bin/bash

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

set -ex

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

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

VECTOR_SHA=$(git rev-parse --short=12 HEAD) # use the ACTUAL SHA rather than assume develop

CI_PACKAGE=true DIST_VERSION=$VECTOR_SHA-react-$REACT_SHA-js-$JSSDK_SHA scripts/package.sh
CI_PACKAGE=true DIST_VERSION=$DIST_VERSION scripts/package.sh
7 changes: 3 additions & 4 deletions scripts/docker-write-version.sh → scripts/docker-package.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ DIST_VERSION=$TAG
# for an appropriately tagged branch as well (heads/v1.2.3).
if [[ $BRANCH != HEAD && ! $BRANCH =~ heads/v.+ ]]
then
REACT_SHA=$(cd node_modules/matrix-react-sdk; git rev-parse --short=12 HEAD)
JSSDK_SHA=$(cd node_modules/matrix-js-sdk; git rev-parse --short=12 HEAD)
VECTOR_SHA=$(git rev-parse --short=12 HEAD) # use the ACTUAL SHA rather than assume develop
DIST_VERSION=$VECTOR_SHA-react-$REACT_SHA-js-$JSSDK_SHA
DIST_VERSION=`$(dirname $0)/get-version-from-git.sh`
fi

DIST_VERSION=`$(dirname $0)/normalize-version.sh ${DIST_VERSION}`
VERSION=$DIST_VERSION yarn build
echo $DIST_VERSION > /src/webapp/version
10 changes: 10 additions & 0 deletions scripts/get-version-from-git.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Echoes a version based on the git hashes of the element-web, react-sdk & js-sdk checkouts, for the case where
# these dependencies are git checkouts.

# Since the deps are fetched from git, we can rev-parse
REACT_SHA=$(cd node_modules/matrix-react-sdk; git rev-parse --short=12 HEAD)
JSSDK_SHA=$(cd node_modules/matrix-js-sdk; git rev-parse --short=12 HEAD)
VECTOR_SHA=$(git rev-parse --short=12 HEAD) # use the ACTUAL SHA rather than assume develop
echo $VECTOR_SHA-react-$REACT_SHA-js-$JSSDK_SHA
8 changes: 8 additions & 0 deletions scripts/normalize-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# If $1 looks like v1.2.3 or v1.2.3-foo, strip the leading v, then print it to stdout
if [[ $1 =~ ^v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+(-.+)?$ ]]; then
echo ${1:1}
else
echo $1
fi
7 changes: 1 addition & 6 deletions scripts/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ cp -r webapp element-$version
# Just in case you have a local config, remove it before packaging
rm element-$version/config.json || true

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

tar chvzf dist/element-$version.tar.gz element-$version
rm -r element-$version
Expand Down
21 changes: 9 additions & 12 deletions src/vector/platform/WebPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class WebPlatform extends VectorBasePlatform {
// presence of intermediate caching proxies), but still: we're trying
// to tell the user that there is a new version.

return new Promise(function(resolve, reject) {
return new Promise((resolve, reject) => {
request(
{
method: "GET",
Expand All @@ -121,27 +121,24 @@ export default class WebPlatform extends VectorBasePlatform {
return;
}

const ver = body.trim();
resolve(ver);
resolve(this.getNormalizedAppVersion(body.trim()));
},
);
});
}

getNormalizedAppVersion(): string {
let ver = process.env.VERSION;

getNormalizedAppVersion(version: string): string {
// if version looks like semver with leading v, strip it
// (matches scripts/package.sh)
// (matches scripts/normalize-version.sh)
const semVerRegex = new RegExp("^v[0-9]+.[0-9]+.[0-9]+(-.+)?$");
if (semVerRegex.test(process.env.VERSION)) {
ver = process.env.VERSION.substr(1);
if (semVerRegex.test(version)) {
return version.substr(1);
}
return ver;
return version;
}

getAppVersion(): Promise<string> {
return Promise.resolve(this.getNormalizedAppVersion());
return Promise.resolve(this.getNormalizedAppVersion(process.env.VERSION));
}

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

pollForUpdate = () => {
return this.getMostRecentVersion().then((mostRecentVersion) => {
const currentVersion = this.getNormalizedAppVersion();
const currentVersion = this.getNormalizedAppVersion(process.env.VERSION);

if (currentVersion !== mostRecentVersion) {
if (this.shouldShowUpdate(mostRecentVersion)) {
Expand Down

0 comments on commit b0abbfa

Please sign in to comment.