@docupace/pipeline 1.0.0-alpha.35 → 1.0.0-alpha.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/next/deploy.sh CHANGED
@@ -1,40 +1,40 @@
1
- #!/bin/bash
1
+ #!/bin/bash
2
2
 
3
- set -e # Enable command tracing
4
- # Variables
5
- REPO_NAME="gitops-dev"
6
- REPO_URL="https://x-token-auth:$GITOPS_REP_TOKEN@bitbucket.org/dp-cloud/${REPO_NAME}.git"
7
- FILE_PATH="dp/$2/$1.values.yaml"
8
- BUILD_NUMBER=$BITBUCKET_BUILD_NUMBER
9
- GITOPS_BRANCH=master
10
- if [ -n "$VERSION_TAG" ]; then
11
- DEPLOY_TAG=$VERSION_TAG
12
- else
13
- DEPLOY_TAG="build-$BUILD_NUMBER"
14
- fi
3
+ set -e # Enable command tracing
4
+ # Variables
5
+ REPO_NAME="gitops-dev"
6
+ REPO_URL="https://x-token-auth:$GITOPS_REP_TOKEN@bitbucket.org/dp-cloud/${REPO_NAME}.git"
7
+ FILE_PATH="dp/$2/$1.values.yaml"
8
+ BUILD_NUMBER=$BITBUCKET_BUILD_NUMBER
9
+ GITOPS_BRANCH=master
10
+ if [ -n "$VERSION_TAG" ]; then
11
+ DEPLOY_TAG=$VERSION_TAG
12
+ else
13
+ DEPLOY_TAG="build-$BUILD_NUMBER"
14
+ fi
15
15
 
16
16
 
17
17
 
18
- # Clone the repository
18
+ # Clone the repository
19
19
 
20
- git config user.email qxebpukjnzljnwty1knte7ym53aogk@bots.bitbucket.org
21
- git clone $REPO_URL
22
- cd ${REPO_NAME} || exit
20
+ git config user.email qxebpukjnzljnwty1knte7ym53aogk@bots.bitbucket.org
21
+ git clone $REPO_URL
22
+ cd ${REPO_NAME} || exit
23
23
 
24
- # Checkout the branch
24
+ # Checkout the branch
25
25
 
26
- # Modify the YAML file
27
- sed -i "s/tag: .*/tag: $DEPLOY_TAG/" $FILE_PATH
26
+ # Modify the YAML file
27
+ sed -i "s/tag: .*/tag: $DEPLOY_TAG/" $FILE_PATH
28
28
 
29
- # Add changes to git
30
- git add $FILE_PATH
29
+ # Add changes to git
30
+ git add $FILE_PATH
31
31
 
32
- # Commit changes
33
- git commit -m "Update image tag to $DEPLOY_TAG"
32
+ # Commit changes
33
+ git commit -m "Update image tag to $DEPLOY_TAG"
34
34
 
35
- # Push changes
36
- git push origin $GITOPS_BRANCH
35
+ # Push changes
36
+ git push origin $GITOPS_BRANCH
37
37
 
38
- # Clean up
39
- cd ..
40
- rm -rf ${REPO_NAME}
38
+ # Clean up
39
+ cd ..
40
+ rm -rf ${REPO_NAME}
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@docupace/pipeline",
3
- "version": "1.0.0-alpha.35",
3
+ "version": "1.0.0-alpha.37",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "bin": {
8
- "dp-changeset-cli": "src/changeset/cli.js"
8
+ "dp-changeset-cli": "src/changeset/cli.js",
9
+ "dp-next-cli": "src/next/cli.js"
9
10
  },
10
11
  "keywords": [
11
12
  "bash"
@@ -14,6 +15,7 @@
14
15
  "license": "ISC",
15
16
  "dependencies": {
16
17
  "commander": "^12.1.0",
18
+ "js-yaml": "^4.1.0",
17
19
  "zx": "^8.2.4"
18
20
  },
19
21
  "devDependencies": {
@@ -12,8 +12,7 @@ export async function validateChangesets(directoryPath) {
12
12
  let preJson;
13
13
 
14
14
  if (!fs.existsSync(preJsonPath) && mdFiles.length === 0) {
15
- console.error("Error: No pre.json and no md files found.");
16
- process.exit(1);
15
+ throw new Error("Error: No pre.json and no md files found.")
17
16
  }
18
17
 
19
18
  if (fs.existsSync(preJsonPath)) {
@@ -29,16 +28,15 @@ export async function validateChangesets(directoryPath) {
29
28
  const unmatchedFiles = mdFileNamesWithoutExt.filter(fileName => !changesetNames.includes(fileName));
30
29
 
31
30
  if (unmatchedFiles.length == 0) {
32
- console.error("No new MD files found:", unmatchedFiles);
33
- process.exit(1);
31
+ throw new Error("Error: No new MD files found:");
34
32
  }
35
33
  }
36
34
  }
37
35
 
38
36
  console.log("Validation successful.");
39
37
  } catch (error) {
40
- console.error("Error while processing:", error);
41
- process.exit(1);
38
+ console.log(error.message);
39
+ throw(error);
42
40
  }
43
41
  }
44
42
 
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { Command } from 'commander';
4
+ import { pull } from './pull.js';
5
+ import { sonar } from './sonar.js';
6
+ import { publish } from './publish.js';
7
+ import { deploy } from './deploy.js';
8
+ import { copy } from './copy.js';
9
+
10
+ const program = new Command();
11
+
12
+ // GIT SHA computation
13
+ import { $ } from 'zx';
14
+ const GIT_SHA = (await $`git log --pretty=format:'%h' -n 1`).stdout.trim();
15
+
16
+ program
17
+ .command('pull <imageName>')
18
+ .description('Pull a Docker image from the registry')
19
+ .action(async (imageName) => {
20
+ await pull(imageName);
21
+ });
22
+
23
+ program
24
+ .command('sonar')
25
+ .description('Run Sonar analysis')
26
+ .action(async () => {
27
+ await sonar(GIT_SHA);
28
+ });
29
+
30
+ program
31
+ .command('publish')
32
+ .description('Publish a Docker image')
33
+ .action(async () => {
34
+ await publish(GIT_SHA);
35
+ });
36
+
37
+ program
38
+ .command('deploy <application> <site>')
39
+ .description('Deploy an application for a specific site')
40
+ .action(async (application, site) => {
41
+ await deploy(application, site);
42
+ });
43
+
44
+ program
45
+ .command('deployTemplate <site>')
46
+ .description('Deploy an application for a specific site')
47
+ .action(async (site) => {
48
+ await deployTemplate(site);
49
+ });
50
+
51
+ program
52
+ .command('copy')
53
+ .description('Copy a Docker image between registries')
54
+ .action(async () => {
55
+ await copy();
56
+ });
57
+
58
+ program.parse(process.argv);
@@ -0,0 +1,21 @@
1
+ import { $ } from 'zx';
2
+
3
+ export async function copy() {
4
+ const JF_REGISTRY_URL = 'registry.paperout.com';
5
+ const SOURCE_REGISTRY = `${JF_REGISTRY_URL}/${process.env.BITBUCKET_REPO_SLUG}-dev-docker-local`;
6
+ const TARGET_REGISTRY = `${JF_REGISTRY_URL}/${process.env.BITBUCKET_REPO_SLUG}-${process.env.TARGET_ENV}-docker-local`;
7
+ const IMAGE_NAME = `${process.env.BITBUCKET_REPO_SLUG}:${process.env.PUBLISH_TAG}`;
8
+ const SOURCE_FULL_IMAGE_NAME = `${SOURCE_REGISTRY}/${IMAGE_NAME}`;
9
+ const TARGET_FULL_IMAGE_NAME = `${TARGET_REGISTRY}/${IMAGE_NAME}`;
10
+
11
+ console.log(`Copying Docker image: ${SOURCE_FULL_IMAGE_NAME} -> ${TARGET_FULL_IMAGE_NAME}`);
12
+
13
+ // Login to source registry
14
+ await $`echo ${process.env.JF_REGISTRY_PASSWORD_DEV} | docker login --username ${process.env.JF_REGISTRY_USER_DEV} --password-stdin ${JF_REGISTRY_URL}`;
15
+ await $`docker pull ${SOURCE_FULL_IMAGE_NAME}`;
16
+ await $`docker tag ${SOURCE_FULL_IMAGE_NAME} ${TARGET_FULL_IMAGE_NAME}`;
17
+
18
+ // Login to target registry
19
+ await $`echo ${process.env.JF_REGISTRY_PASSWORD_QA} | docker login --username ${process.env.JF_REGISTRY_USER_QA} --password-stdin ${JF_REGISTRY_URL}`;
20
+ await $`docker push ${TARGET_FULL_IMAGE_NAME}`;
21
+ }
@@ -0,0 +1,118 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import yaml from 'js-yaml';
4
+ import { $ } from 'zx';
5
+
6
+ const REPO_NAME = 'gitops-dev';
7
+ const REPO_URL = `https://x-token-auth:${process.env.GITOPS_REP_TOKEN}@bitbucket.org/dp-cloud/${REPO_NAME}.git`;
8
+ const GITOPS_BRANCH = 'master';
9
+
10
+ /**
11
+ * Clone the repository, read the YAML file, and return the parsed data and file paths.
12
+ * @param {string} application - The application name.
13
+ * @param {string} site - The site name.
14
+ * @returns {object} - The repository path, file path, and YAML data.
15
+ */
16
+ export async function readDeployConfig(application, site) {
17
+ const repoPath = path.join(process.cwd(), REPO_NAME);
18
+ const deployConfigFilePath = path.join(repoPath, `dp/${site}/${application}.values.yaml`);
19
+
20
+ console.log(`Cloning repository: ${REPO_NAME}`);
21
+ await $`git clone ${REPO_URL}`;
22
+ console.log(`Repository cloned to: ${repoPath}`);
23
+
24
+ let deployConfig;
25
+ try {
26
+ deployConfig = yaml.load(fs.readFileSync(deployConfigFilePath, 'utf8'));
27
+ console.log(`YAML file loaded from: ${deployConfigFilePath}`);
28
+ } catch (error) {
29
+ console.error(`Error reading YAML file: ${error.message}`);
30
+ process.exit(1);
31
+ }
32
+
33
+ return { repoPath, deployConfigFilePath, deployConfig };
34
+ }
35
+
36
+ /**
37
+ * Update the `tag` field in the YAML data.
38
+ * @param {object} deployConfig - The parsed YAML data.
39
+ * @param {string} tag - The new tag to set.
40
+ * @returns {object} - The updated YAML data.
41
+ */
42
+ export function setTag(deployConfig, tag) {
43
+ if (deployConfig.image && deployConfig.image.tag) {
44
+ console.log(`Current image tag: ${deployConfig.image.tag}`);
45
+ deployConfig.image.tag = tag;
46
+ console.log(`Updated image tag to: ${deployConfig.image.tag}`);
47
+ } else {
48
+ console.error('No "image.tag" field found in the YAML file.');
49
+ process.exit(1);
50
+ }
51
+ return deployConfig;
52
+ }
53
+
54
+ /**
55
+ * Write the updated YAML data back to the file, commit the changes, and push to the remote branch.
56
+ * @param {string} repoPath - The path to the cloned repository.
57
+ * @param {string} deployConfigFilePath - The path to the YAML file.
58
+ * @param {object} deployConfig - The updated YAML data.
59
+ */
60
+ export async function writeDeployConfig(repoPath, deployConfigFilePath, deployConfig) {
61
+ try {
62
+ fs.writeFileSync(deployConfigFilePath, yaml.dump(deployConfig), 'utf8');
63
+ console.log(`Updated YAML file saved at: ${deployConfigFilePath}`);
64
+ } catch (error) {
65
+ console.error(`Error writing YAML file: ${error.message}`);
66
+ process.exit(1);
67
+ }
68
+
69
+ console.log('Committing changes to the repository...');
70
+ await $`cd ${repoPath} && git config user.email "gitops@docupace.com"`;
71
+ await $`cd ${repoPath} && git add ${deployConfigFilePath}`;
72
+ await $`cd ${repoPath} && git commit -m "Update image tag to ${deployConfig.image.tag}"`;
73
+ await $`cd ${repoPath} && git push origin ${GITOPS_BRANCH}`;
74
+ console.log('Changes pushed to the remote repository.');
75
+
76
+ console.log('Cleaning up...');
77
+ await $`rm -rf ${repoPath}`;
78
+ console.log('Cleanup complete.');
79
+ }
80
+
81
+ /**
82
+ * Main deploy function that orchestrates reading, updating, and writing the deployment config.
83
+ * @param {string} application - The application name.
84
+ * @param {string} site - The site name.
85
+ */
86
+
87
+ function getDeployTag() {
88
+ const gitTag = $`git tag --points-at HEAD | grep '^v' | head -n 1)`.stdout.trim()
89
+ const deployTag = gitTag || `build-${process.env.BITBUCKET_BUILD_NUMBER}`;
90
+ return deployTag;
91
+ }
92
+
93
+ export async function deployTemplate(site) {
94
+ const deployTag = getDeployTag();
95
+ console.log(`Deploying standard template for site: ${site}`);
96
+ console.log(`Using deployment tag: ${deployTag}`);
97
+ const { repoPath, deployConfigFilePath, deployConfig } = await readDeployConfig('graphql', site);
98
+
99
+ deployConfig.graphqlServer.standardConfigRemoteBranch = deployTag;
100
+
101
+ await writeDeployConfig(repoPath, deployConfigFilePath, deployConfig);
102
+ console.log('Deployment completed successfully.');
103
+
104
+ }
105
+
106
+
107
+ export async function deploy(application, site) {
108
+ const deployTag = getDeployTag();
109
+
110
+ console.log(`Deploying application: ${application} for site: ${site}`);
111
+ console.log(`Using deployment tag: ${deployTag}`);
112
+
113
+ const { repoPath, deployConfigFilePath, deployConfig } = await readDeployConfig(application, site);
114
+ const updatedConfigData = setTag(deployConfig, deployTag);
115
+ await writeDeployConfig(repoPath, deployConfigFilePath, updatedConfigData);
116
+
117
+ console.log('Deployment completed successfully.');
118
+ }
@@ -0,0 +1,46 @@
1
+ import { $ } from 'zx';
2
+
3
+ export async function publish(GIT_SHA) {
4
+ const BITBUCKET_REPO_SLUG = process.env.BITBUCKET_REPO_SLUG;
5
+ const BUILD_IMAGE_NAME = `${BITBUCKET_REPO_SLUG}:build-${process.env.BITBUCKET_BUILD_NUMBER}`;
6
+ const IMAGE_NAME = `${BITBUCKET_REPO_SLUG}:sha-${GIT_SHA}`;
7
+ const JF_REGISTRY_FULL_URL = `${process.env.JF_REGISTRY_URL}/${process.env.JF_REGISTRY_REPO_NAME}`;
8
+
9
+ console.log(`Publishing Docker image: ${IMAGE_NAME}`);
10
+
11
+ // Docker login
12
+ await $`echo ${process.env.JF_REGISTRY_PASSWORD_DEV} | docker login --username ${process.env.JF_REGISTRY_USER_DEV} --password-stdin ${process.env.JF_REGISTRY_URL}`;
13
+
14
+ // Check if the image already exists
15
+ try {
16
+ await $`docker manifest inspect ${JF_REGISTRY_FULL_URL}/${IMAGE_NAME}`;
17
+ console.log(`Tag sha-${GIT_SHA} already exists in the remote repository. Skipping push.`);
18
+ } catch {
19
+ await $`docker tag ${IMAGE_NAME} ${JF_REGISTRY_FULL_URL}/${IMAGE_NAME}`;
20
+ await $`docker push ${JF_REGISTRY_FULL_URL}/${IMAGE_NAME}`;
21
+ }
22
+
23
+ // Tag and push build image
24
+ await $`docker tag ${IMAGE_NAME} ${JF_REGISTRY_FULL_URL}/${BUILD_IMAGE_NAME}`;
25
+ await $`docker push ${JF_REGISTRY_FULL_URL}/${BUILD_IMAGE_NAME}`;
26
+
27
+ // Fetch Git tags pointing to the current HEAD
28
+ const gitTagsOutput = (await $`git tag --points-at HEAD`).stdout.trim();
29
+ const gitTags = gitTagsOutput.split('\n').filter((tag) => tag);
30
+
31
+ // Tag and push each Git tag as a Docker tag
32
+ if (gitTags.length > 0) {
33
+ console.log(`Found Git tags pointing to HEAD: ${gitTags.join(', ')}`);
34
+ for (const gitTag of gitTags) {
35
+ const dockerTag = `${JF_REGISTRY_FULL_URL}/${BITBUCKET_REPO_SLUG}:${gitTag}`;
36
+ console.log(`Tagging and pushing Docker image for Git tag: ${gitTag}`);
37
+ await $`docker tag ${IMAGE_NAME} ${dockerTag}`;
38
+ await $`docker push ${dockerTag}`;
39
+ }
40
+ } else {
41
+ console.log('No Git tags found pointing to the current HEAD.');
42
+ }
43
+
44
+ // Docker logout
45
+ await $`docker logout ${process.env.JF_REGISTRY_URL}`;
46
+ }
@@ -0,0 +1,23 @@
1
+ import { $ } from 'zx';
2
+
3
+ export async function sonar(GIT_SHA) {
4
+ const IMAGE_NAME = `${process.env.BITBUCKET_REPO_SLUG}:sha-${GIT_SHA}`;
5
+ const SONAR_CONTAINER_NAME = `${process.env.BITBUCKET_REPO_SLUG}-${GIT_SHA}`;
6
+
7
+ console.log(`Running Sonar analysis on Docker image: ${IMAGE_NAME}`);
8
+
9
+ // Remove existing container
10
+ const existingContainer = (
11
+ await $`docker ps -a --filter "name=${SONAR_CONTAINER_NAME}" --format "{{.Names}}"`
12
+ ).stdout.trim();
13
+ if (existingContainer) {
14
+ await $`docker rm -f ${SONAR_CONTAINER_NAME}`;
15
+ }
16
+
17
+ await $`docker run --rm \
18
+ -e BRANCH=${process.env.BITBUCKET_BRANCH} \
19
+ -e PR_ID=${process.env.BITBUCKET_PR_ID} \
20
+ -e PR_DESTINATION_BRANCH=${process.env.BITBUCKET_PR_DESTINATION_BRANCH} \
21
+ -e SONAR_TOKEN=${process.env.SONAR_TOKEN} \
22
+ --name ${SONAR_CONTAINER_NAME} ${IMAGE_NAME} pnpm sonar`;
23
+ }