Skip to content

Commit

Permalink
chore(server): inject version at build time (#1491)
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna authored Jan 31, 2024
1 parent 4d1c4ca commit 5272b34
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/shadowbox/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ COPY tsconfig.json ./
COPY third_party third_party

ARG ARCH
ARG VERSION

RUN ARCH=${ARCH} ROOT_DIR=/ npm run action shadowbox/server/build
RUN ARCH=${ARCH} ROOT_DIR=/ SB_VERSION=${VERSION} npm run action shadowbox/server/build

# shadowbox image
FROM ${NODE_IMAGE}

# Save metadata on the software versions we are using.
LABEL shadowbox.node_version=16.18.0

ARG GITHUB_RELEASE
LABEL shadowbox.github.release="${GITHUB_RELEASE}"
LABEL shadowbox.github.release=${VERSION}

# The user management service doesn't quit with SIGTERM.
STOPSIGNAL SIGKILL
Expand Down
9 changes: 8 additions & 1 deletion src/shadowbox/docker/build.action.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Environment inputs:
# - SB_VERSION
# - SB_IMAGE
# - ARCH
# - NODE_IMAGE
# - ROOT_DIR

export DOCKER_CONTENT_TRUST="${DOCKER_CONTENT_TRUST:-1}"
# Enable Docker BuildKit (https://docs.docker.com/develop/develop-images/build_enhancements)
export DOCKER_BUILDKIT=1
Expand All @@ -38,7 +45,7 @@ readonly NODE_IMAGE=$(
docker build --force-rm \
--build-arg ARCH="${ARCH}" \
--build-arg NODE_IMAGE="${NODE_IMAGE}" \
--build-arg GITHUB_RELEASE="${TRAVIS_TAG:-none}" \
--build-arg VERSION="${SB_VERSION:-dev}" \
-f src/shadowbox/docker/Dockerfile \
-t "${SB_IMAGE:-localhost/outline/shadowbox}" \
"${ROOT_DIR}"
1 change: 0 additions & 1 deletion src/shadowbox/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "outline-server",
"private": true,
"version": "1.7.2",
"description": "Outline server",
"main": "build/server/main.js",
"author": "Outline",
Expand Down
4 changes: 2 additions & 2 deletions src/shadowbox/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import * as logging from '../infrastructure/logging';
import {PrometheusClient, startPrometheus} from '../infrastructure/prometheus_scraper';
import {RolloutTracker} from '../infrastructure/rollout';
import {AccessKeyId} from '../model/access_key';
import {version} from '../package.json';
import * as version from './version';

import {PrometheusManagerMetrics} from './manager_metrics';
import {bindService, ShadowsocksManagerService} from './manager_service';
Expand Down Expand Up @@ -82,7 +82,7 @@ function createRolloutTracker(
async function main() {
const verbose = process.env.LOG_LEVEL === 'debug';
logging.info('======== Outline Server main() ========');
logging.info(`Version is ${version}`);
logging.info(`Version is ${version.getPackageVersion()}`);

const portProvider = new PortProvider();
const accessKeyConfig = json_config.loadFileConfig<AccessKeyConfigJson>(
Expand Down
4 changes: 2 additions & 2 deletions src/shadowbox/server/manager_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {JsonConfig} from '../infrastructure/json_config';
import * as logging from '../infrastructure/logging';
import {AccessKey, AccessKeyRepository, DataLimit} from '../model/access_key';
import * as errors from '../model/errors';
import {version} from '../package.json';
import * as version from './version';

import {ManagerMetrics} from './manager_metrics';
import {ServerConfigJson} from './server_config';
Expand Down Expand Up @@ -258,7 +258,7 @@ export class ShadowsocksManagerService {
serverId: this.serverConfig.data().serverId,
metricsEnabled: this.serverConfig.data().metricsEnabled || false,
createdTimestampMs: this.serverConfig.data().createdTimestampMs,
version,
version: version.getPackageVersion(),
accessKeyDataLimit: this.serverConfig.data().accessKeyDataLimit,
portForNewAccessKeys: this.serverConfig.data().portForNewAccessKeys,
hostnameForAccessKeys: this.serverConfig.data().hostname,
Expand Down
6 changes: 3 additions & 3 deletions src/shadowbox/server/shared_metrics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import {ManualClock} from '../infrastructure/clock';
import {InMemoryConfig} from '../infrastructure/json_config';
import {AccessKeyId, DataLimit} from '../model/access_key';
import {version} from '../package.json';
import * as version from './version';
import {AccessKeyConfigJson} from './server_access_key';

import {ServerConfigJson} from './server_config';
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('OutlineSharedMetricsPublisher', () => {
await clock.runCallbacks();
expect(metricsCollector.collectedFeatureMetricsReport).toEqual({
serverId: 'server-id',
serverVersion: version,
serverVersion: version.getPackageVersion(),
timestampUtcMs: timestamp,
dataLimit: {
enabled: true,
Expand All @@ -238,7 +238,7 @@ describe('OutlineSharedMetricsPublisher', () => {
await clock.runCallbacks();
expect(metricsCollector.collectedFeatureMetricsReport).toEqual({
serverId: 'server-id',
serverVersion: version,
serverVersion: version.getPackageVersion(),
timestampUtcMs: timestamp,
dataLimit: {
enabled: false,
Expand Down
4 changes: 2 additions & 2 deletions src/shadowbox/server/shared_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {JsonConfig} from '../infrastructure/json_config';
import * as logging from '../infrastructure/logging';
import {PrometheusClient} from '../infrastructure/prometheus_scraper';
import {AccessKeyId, AccessKeyMetricsId} from '../model/access_key';
import {version} from '../package.json';
import * as version from './version';
import {AccessKeyConfigJson} from './server_access_key';

import {ServerConfigJson} from './server_config';
Expand Down Expand Up @@ -280,7 +280,7 @@ export class OutlineSharedMetricsPublisher implements SharedMetricsPublisher {
const keys = this.keyConfig.data().accessKeys;
const featureMetricsReport = {
serverId: this.serverConfig.data().serverId,
serverVersion: version,
serverVersion: version.getPackageVersion(),
timestampUtcMs: this.clock.now(),
dataLimit: {
enabled: !!this.serverConfig.data().accessKeyDataLimit,
Expand Down
25 changes: 25 additions & 0 deletions src/shadowbox/server/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Injected by WebPack with webpack.DefinePlugin.
declare const __VERSION__: string;

export function getPackageVersion(): string {
try {
return __VERSION__;
} catch {
// Catch the ReferenceError if __VERSION__ is not injected.
return "dev"
}
}
2 changes: 2 additions & 0 deletions src/shadowbox/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const config = {
__dirname: false,
},
plugins: [
// Used by server/version.ts.
process.env.SB_VERSION ? new webpack.DefinePlugin({'__VERSION__': JSON.stringify(process.env.SB_VERSION)}): undefined,
// WORKAROUND: some of our (transitive) dependencies use node-gently, which hijacks `require`.
// Setting global.GENTLY to false makes these dependencies use standard require.
new webpack.DefinePlugin({'global.GENTLY': false}),
Expand Down

0 comments on commit 5272b34

Please sign in to comment.