Skip to content

Commit

Permalink
AG-38264 Add .crx files to artifacts
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit e971af7
Author: Kurbanali Ruslan <[email protected]>
Date:   Wed Jan 29 11:13:23 2025 +0500

    updated README

commit c473a5b
Author: Kurbanali Ruslan <[email protected]>
Date:   Wed Jan 29 10:36:58 2025 +0500

    updated readme

commit ab0cc32
Author: Kurbanali Ruslan <[email protected]>
Date:   Wed Jan 29 10:27:43 2025 +0500

    added test certificate

commit 9209065
Merge: 8bf9d0e cb3f1ab
Author: Kurbanali Ruslan <[email protected]>
Date:   Wed Jan 29 10:08:25 2025 +0500

    Merge branch 'master' into feature/AG-38264

commit 8bf9d0e
Author: Kurbanali Ruslan <[email protected]>
Date:   Wed Jan 29 10:07:47 2025 +0500

    reverted some changes

commit d980bce
Author: Kurbanali Ruslan <[email protected]>
Date:   Tue Jan 28 14:27:07 2025 +0500

    updated readme

commit d2eaf28
Author: Kurbanali Ruslan <[email protected]>
Date:   Tue Jan 28 12:07:32 2025 +0500

    review changes

commit 89d6040
Author: Kurbanali Ruslan <[email protected]>
Date:   Mon Jan 27 16:54:07 2025 +0500

    fixed not created folder bug

commit 71bc1b4
Author: Kurbanali Ruslan <[email protected]>
Date:   Mon Jan 27 16:25:40 2025 +0500

    added comment

commit 49d8ff3
Author: Kurbanali Ruslan <[email protected]>
Date:   Mon Jan 27 16:24:48 2025 +0500

    added generating key for dev builds

commit edda857
Author: Kurbanali Ruslan <[email protected]>
Date:   Mon Jan 27 13:33:32 2025 +0500

    updated readme

commit 1b3b518
Author: Kurbanali Ruslan <[email protected]>
Date:   Mon Jan 27 13:25:43 2025 +0500

    updated readme

commit ffca8c1
Author: Kurbanali Ruslan <[email protected]>
Date:   Mon Jan 27 13:15:21 2025 +0500

    fix tests spec stage env

commit 902d228
Author: Kurbanali Ruslan <[email protected]>
Date:   Mon Jan 27 13:01:34 2025 +0500

    minor changes

commit 1cb6e5e
Author: Kurbanali Ruslan <[email protected]>
Date:   Fri Jan 24 19:03:20 2025 +0500

    minor fixes

commit 8473dfa
Author: Kurbanali Ruslan <[email protected]>
Date:   Fri Jan 24 18:43:10 2025 +0500

    updated readme

commit 4019c4c
Author: Kurbanali Ruslan <[email protected]>
Date:   Fri Jan 24 18:43:03 2025 +0500

    updated build plans

commit c0d848c
Author: Kurbanali Ruslan <[email protected]>
Date:   Fri Jan 24 18:18:29 2025 +0500

    updated crx build
  • Loading branch information
kurrx committed Jan 30, 2025
1 parent cb3f1ab commit ce96328
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 57 deletions.
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ You can specify browser in arguments. See examples below:

```bash
yarn dev chrome
yarn beta chrome-mv3
yarn beta chrome-mv2
yarn release opera
```

Expand All @@ -148,16 +148,44 @@ Before releasing new versions do not forget to update exclusions-services data,

### Artifact builds

- `CREDENTIALS_PASSWORD=<password> artifacts:beta`
- `CREDENTIALS_PASSWORD=<password> artifacts:release`
Before building artifacts make sure you have [build](#build)
the extension and also make sure you have added credentials:

Make sure you have added credentials
- `certificate-beta.pem` - chrome crx beta certificate
- `certificate-release.pem` - chrome crx release certificate
- `mozilla_credentials.json` - encrypted credentials - temporary not needed,

- certificate-beta.pem - chrome crx beta certificate
- certificate-release.pem - chrome crx release certificate
- mozilla_credentials.json - encrypted credentials,
to the directory `./private/AdguardVPN`.

to the directory `./private/AdguardVPN`
For testing purposes for `artifacts:dev` command credentials taken from `./tests/certificate-test.pem` file.

WARNING: DO NOT USE TEST CREDENTIALS FOR PRODUCTION BUILDS, BECAUSE THEY ARE AVAILABLE IN PUBLIC.

If you want to generate your own credentials you can go to
[How to generate credentials for `crx` builds](#how-to-generate-credentials-for-crx-builds) for more details

To build artifacts, run:

- `CREDENTIALS_PASSWORD=<password> yarn artifacts:dev`
- `CREDENTIALS_PASSWORD=<password> yarn artifacts:beta`
- `CREDENTIALS_PASSWORD=<password> yarn artifacts:release`

**Builds will be located in the `build` directory**

By default, you will have builds for:

- Chrome — manifest versions **3** (`chrome.crx`)
- Firefox — manifest version **3** (`firefox.xpi`) - temporarily disabled

#### How to generate credentials for `crx` builds

You can use [Crx CLI `keygen`](https://github.com/thom4parisot/crx#crx-keygen-directory)
to generate credentials for `crx` builds, see the example below:

```bash
# Command will generate `key.pem` credential in the `./private/AdguardVPN` directory
yarn crx keygen ./private/AdguardVPN
```

#### <a name="build-firefox-review"></a> Special building instructions for Firefox reviewers

Expand Down
4 changes: 2 additions & 2 deletions bamboo-specs/build-beta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ Build:
# build with prod API endpoints edge.zip, chrome.zip, firefox.zip, opera.zip
yarn beta
# build artifacts chrome.crx
# firefox.xpi - temporary disabled
# build artifacts chrome.crx and update.xml
# firefox.xpi and update.json - temporary disabled
yarn artifacts:beta
# build source code for uploading to amo
Expand Down
15 changes: 15 additions & 0 deletions bamboo-specs/build-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ Build:
export VPN_API_URL="${bamboo.prodVpnApiUrl}"
export AUTH_API_URL="${bamboo.prodAuthApiUrl}"
export FORWARDER_DOMAIN="${bamboo.prodVpnForwarderDomain}"
export CREDENTIALS_PASSWORD="${bamboo.extensionsPassphrase}"
yarn release
# build artifacts chrome.crx and update.xml
# firefox.xpi and update.json - temporary disabled
yarn artifacts:release
# build source code for uploading to amo
git ls-files | zip -@ build/release/source.zip
- inject-variables:
Expand Down Expand Up @@ -102,6 +107,16 @@ Build:
pattern: firefox.zip
shared: true
required: true
- name: chrome.crx
location: build/beta
pattern: chrome.crx
shared: true
required: true
- name: update.xml
location: build/beta
pattern: update.xml
shared: true
required: true
- name: build.txt
location: build/release
pattern: build.txt
Expand Down
20 changes: 20 additions & 0 deletions bamboo-specs/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,22 @@ Test:
FORWARDER_DOMAIN="${bamboo.devVpnForwarderDomain}" \
yarn dev
# build artifacts chrome.crx
# firefox.xpi - temporary disabled
STAGE_ENV=test \
yarn artifacts:dev
# build with prod API endpoints
STAGE_ENV=prod \
VPN_API_URL="${bamboo.prodVpnApiUrl}" \
AUTH_API_URL="${bamboo.prodAuthApiUrl}" \
FORWARDER_DOMAIN="${bamboo.prodVpnForwarderDomain}" \
yarn dev
# build artifacts chrome-prod.crx
# firefox-prod.xpi - temporary disabled
STAGE_ENV=prod \
yarn artifacts:dev
final-tasks:
- script:
interpreter: SHELL
Expand Down Expand Up @@ -102,6 +112,11 @@ Test:
pattern: firefox.zip
shared: true
required: true
- name: chrome.crx
location: build/dev
pattern: chrome.crx
shared: true
required: true
- name: build.txt
location: build/dev
pattern: build.txt
Expand All @@ -127,6 +142,11 @@ Test:
pattern: firefox-prod.zip
shared: true
required: true
- name: chrome-prod.crx
location: build/dev
pattern: chrome-prod.crx
shared: true
required: true
requirements:
- adg-docker: 'true'

Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
"locales": "babel-node tasks/translations",
"compile-proto": "./tasks/compile-protobuf.sh",
"increment": "yarn version --patch --no-git-tag-version",
"crx:beta": "export BUILD_ENV=beta; yarn beta chrome && ts-node tasks/crx.ts",
"xpi:beta": "export BUILD_ENV=beta; yarn beta firefox && ts-node tasks/xpi.ts",
"crx:release": "export BUILD_ENV=release; yarn release chrome && ts-node tasks/crx.ts",
"xpi:release": "export BUILD_ENV=release; yarn release firefox && ts-node tasks/xpi.ts",
"crx:dev": "export BUILD_ENV=dev; ts-node tasks/crx.ts",
"crx:beta": "export BUILD_ENV=beta; ts-node tasks/crx.ts",
"crx:release": "export BUILD_ENV=release; ts-node tasks/crx.ts",
"xpi:beta": "export BUILD_ENV=beta; ts-node tasks/xpi.ts",
"xpi:release": "export BUILD_ENV=release; ts-node tasks/xpi.ts",
"artifacts:dev": "yarn crx:dev",
"artifacts:beta": "yarn crx:beta",
"artifacts:release": "yarn crx:release && yarn xpi:release",
"artifacts:release": "yarn crx:release",
"resources": "ts-node tasks/resources"
},
"devDependencies": {
Expand Down
21 changes: 11 additions & 10 deletions tasks/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,31 @@ export const IS_BETA = BUILD_ENV ? BUILD_ENV === Env.Beta : false;
// Build output path
export const BUILD_PATH = '../build';
export const CRX_NAME = 'chrome.crx';
export const CRX_MV3_NAME = 'chrome-mv3.crx';
export const CRX_PROD_NAME = 'chrome-prod.crx';
export const XPI_NAME = 'firefox.xpi';
export const CHROME_UPDATER_FILENAME = 'update.xml';
export const FIREFOX_UPDATER_FILENAME = 'update.json';
export const MANIFEST_NAME = 'manifest.json';

export const MV3 = 'mv3';

// Chrome CRX certificate paths
export const CERTIFICATE_PATHS = {
beta: './private/AdguardVPN/certificate-beta.pem',
release: './private/AdguardVPN/certificate-release.pem',
[Env.Dev]: './tests/certificate-test.pem',
[Env.Beta]: './private/AdguardVPN/certificate-beta.pem',
[Env.Release]: './private/AdguardVPN/certificate-release.pem',
};

export const deployPath = process.env.BUILD_ENV || Env.Dev;
export const DEPLOY_PATH = process.env.BUILD_ENV || Env.Dev;

export const UPDATE_BASE_URL = 'https://static.adtidy.net/extensions/adguardvpn';

// Update manifest URL for the Chrome extension
export const CHROME_UPDATE_URL = `https://static.adtidy.net/extensions/adguardvpn/${deployPath}/${CHROME_UPDATER_FILENAME}`;
export const CHROME_UPDATE_URL = `${UPDATE_BASE_URL}/${DEPLOY_PATH}/${CHROME_UPDATER_FILENAME}`;

// Update manifest URL for the Firefox add-on
export const FIREFOX_UPDATE_URL = `https://static.adtidy.net/extensions/adguardvpn/${deployPath}/${FIREFOX_UPDATER_FILENAME}`;
export const FIREFOX_UPDATE_URL = `${UPDATE_BASE_URL}/${DEPLOY_PATH}/${FIREFOX_UPDATER_FILENAME}`;

// Path to the Chrome CRX (that we'll add to the update manifest)
export const CHROME_UPDATE_CRX = `https://static.adtidy.net/extensions/adguardvpn/${deployPath}/${CRX_NAME}`;
export const CHROME_UPDATE_CRX = `${UPDATE_BASE_URL}/${DEPLOY_PATH}/${CRX_NAME}`;

// Path to the Firefox XPI (that we'll add to the update manifest)
export const FIREFOX_UPDATE_XPI = `https://static.adtidy.net/extensions/adguardvpn/${deployPath}/${XPI_NAME}`;
export const FIREFOX_UPDATE_XPI = `${UPDATE_BASE_URL}/${DEPLOY_PATH}/${XPI_NAME}`;
116 changes: 84 additions & 32 deletions tasks/crx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import Crx from 'crx';
import chalk from 'chalk';
import { program } from 'commander';

const { log, error } = console;

const packageJson = require('../package.json');
import packageJson from '../package.json';

const {
import { updateManifest } from './helpers';
import {
CHROME_UPDATE_URL,
MANIFEST_NAME,
Browser,
Expand All @@ -20,26 +19,48 @@ const {
CHROME_UPDATE_CRX,
CHROME_UPDATER_FILENAME,
CRX_NAME,
} = require('./consts');
const { updateManifest } = require('./helpers');
CRX_PROD_NAME,
IS_DEV,
STAGE_ENV,
StageEnv,
type Env,
} from './consts';

const { log, error } = console;

const { BUILD_ENV } = process.env;
const { outputPath } = BUILD_ENV_MAP[BUILD_ENV as string];

const WRITE_PATH = path.resolve(__dirname, BUILD_PATH, outputPath);

const getPrivateKey = async () => {
const certificatePath = CERTIFICATE_PATHS[BUILD_ENV as string];
/**
* Retrieves certificate private key depending on the environment
*
* @param crxName Name of the crx file (used for logging)
* @returns Buffer of the private key
*
* @throws Error if the certificate is not found
*/
const getPrivateKey = async (crxName: string) => {
const certificatePath = CERTIFICATE_PATHS[BUILD_ENV as Env];
try {
const privateKey = await fs.readFile(certificatePath);
log(chalk.greenBright(`\nThe certificate is read from ${certificatePath}\n`));
return privateKey;
} catch (e: any) {
error(chalk.redBright(`Can not create ${CRX_NAME} - the valid certificate is not found in ${certificatePath} - ${e.message}\n`));
error(chalk.redBright(`Can not create ${crxName} - the valid certificate is not found in ${certificatePath} - ${e.message}\n`));
throw e;
}
};

/**
* Creates crx file from the loaded file
*
* @param loadedFile Loaded crx file
* @param crxName Name of the crx file
*
* @throws Error if the crx file can not be created
*/
// TODO: remove any when crx will support types
const createCrx = async (loadedFile: any, crxName: string) => {
try {
Expand All @@ -53,40 +74,73 @@ const createCrx = async (loadedFile: any, crxName: string) => {
}
};

/**
* Creates update XML file for the extension manifest
*
* @param crx Instance of the Crx package
* @param updateName Name of the update file
*
* @throws Error if the update file can not be created
*/
// TODO: remove any when crx will support types
const createXml = async (crx: any) => {
const xmlBuffer = await crx.generateUpdateXML();
const writeXmlPath = path.resolve(WRITE_PATH, CHROME_UPDATER_FILENAME);
await fs.writeFile(writeXmlPath, xmlBuffer);
log(chalk.greenBright(`${CHROME_UPDATER_FILENAME} saved to ${WRITE_PATH}\n`));
const createXml = async (crx: any, updateName: string) => {
try {
const xmlBuffer = await crx.generateUpdateXML();
const writeXmlPath = path.resolve(WRITE_PATH, updateName);
await fs.writeFile(writeXmlPath, xmlBuffer);
log(chalk.greenBright(`${updateName} saved to ${WRITE_PATH}\n`));
} catch (e: any) {
error(chalk.redBright(`Error: Can not create ${updateName} - ${e.message}\n`));
throw e;
}
};

/**
* Packs extension to crx file for chrome browser with manifest version 3.
*
* Output files:
* - chrome.crx and updated.xml - beta and release environments
* - chrome.crx - dev environment with test stage environment
* - chrome-prod.crx - dev environment with prod stage environment
*/
const generateChromeFiles = async () => {
const loadPath = path.resolve(WRITE_PATH, Browser.Chrome);
const manifestPath = path.resolve(loadPath, MANIFEST_NAME);

let crxFileName = CRX_NAME;
if (IS_DEV && STAGE_ENV === StageEnv.Prod) {
crxFileName = CRX_PROD_NAME;
}

try {
const chromeManifest = await fs.readFile(manifestPath);
const PRIVATE_KEY = await getPrivateKey();
const privateKey = await getPrivateKey(crxFileName);

const crx = new Crx({
codebase: CHROME_UPDATE_CRX,
privateKey: PRIVATE_KEY,
privateKey,
publicKey: packageJson.name,
});

// Add to the chrome manifest `update_url` property
// which is to be present while creating the crx file
const updatedManifest = updateManifest(chromeManifest, { update_url: CHROME_UPDATE_URL });
await fs.writeFile(manifestPath, updatedManifest);

const loadedFile = await crx.load(loadPath);
await createCrx(loadedFile, CRX_NAME);
await createXml(crx);

// Delete from the chrome manifest `update_url` property
// after the crx file has been created - reset the manifest
await fs.writeFile(manifestPath, chromeManifest);
if (IS_DEV) {
// for dev environment we just create crx file
const loadedFile = await crx.load(loadPath);
await createCrx(loadedFile, crxFileName);
} else {
// for beta and release environments we add the update_url property to the manifest
// Add to the chrome manifest `update_url` property
// which is to be present while creating the crx file
const updatedManifest = updateManifest(chromeManifest, { update_url: CHROME_UPDATE_URL });
await fs.writeFile(manifestPath, updatedManifest);

const loadedFile = await crx.load(loadPath);
await createCrx(loadedFile, crxFileName);
await createXml(crx, CHROME_UPDATER_FILENAME);

// Delete from the chrome manifest `update_url` property
// after the crx file has been created - reset the manifest
await fs.writeFile(manifestPath, chromeManifest);
}
} catch (e: any) {
error(chalk.redBright(e.message));
error(e);
Expand All @@ -97,9 +151,7 @@ const generateChromeFiles = async () => {
};

program
.description('Builds for manifest version 3')
.action(() => {});
.description('Packs extension to crx file for chrome browser with manifest version 3')
.action(generateChromeFiles);

program.parse(process.argv);

generateChromeFiles();
Loading

0 comments on commit ce96328

Please sign in to comment.