Skip to content

Commit 46f7413

Browse files
Merge pull request #2 from ServiceNow/scratch/STRY56337138
Scratch/STRY56337138: GitLab Docker - ChangeCustomTask changes
2 parents 59c8ff7 + 27c7e66 commit 46f7413

File tree

15 files changed

+998
-1
lines changed

15 files changed

+998
-1
lines changed

Dockerfile

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## ===========================================================> The common stage
2+
FROM node:20 AS base
3+
ENV NODE_ENV=production
4+
5+
WORKDIR /app
6+
7+
COPY package*.json ./
8+
RUN npm ci --only=production
9+
10+
## Remove unnecessary files from `node_modules` directory
11+
RUN ( wget -q -O /dev/stdout https://gobinaries.com/tj/node-prune | sh ) \
12+
&& node-prune
13+
14+
15+
## ======================================================> The build image stage
16+
FROM base AS build
17+
ENV NODE_ENV=development
18+
19+
COPY . .
20+
## This step could install only the missing dependencies (ie., development deps ones)
21+
## but there's no way to do that with this NPM version
22+
RUN npm ci --only=production
23+
## Compile the TypeScript source code
24+
# RUN npm link
25+
26+
27+
## =================================================> The production image stage
28+
FROM node:20-alpine AS prod
29+
ENV NODE_ENV=production
30+
31+
WORKDIR /app
32+
## Copy required file to run the production application
33+
COPY --from=base --chown=node:node /app/node_modules ./node_modules
34+
COPY --from=base --chown=node:node /app/*.json ./
35+
COPY --from=build --chown=node:node /app/src /app/src
36+
# COPY --from=build --chown=node:node /app/sndevopscli.js /app/
37+
RUN npm link
38+
# COPY --from=build --chown=node:node /usr/local/lib/node_modules/sndevopscli /usr/local/bin/sndevopscli
39+
40+
41+
42+
## https://engineeringblog.yelp.com/2016/01/dumb-init-an-init-for-docker.html
43+
# RUN apk add --no-cache dumb-init
44+
# RUN npm link
45+
# RUN chmod +x sndevopscli.js
46+
## Dropping privileges
47+
USER node
48+
## Running the app wrapped by the init system for helping on graceful shutdowns
49+
CMD [ "sndevopscli" ]
50+
51+
# FROM node:20-alpine as builder
52+
53+
# # Create app directory
54+
# WORKDIR /app
55+
56+
# ENV NODE_ENV=production
57+
58+
# COPY sndevopscli.js package*.json ./
59+
60+
# COPY . /app
61+
62+
# RUN npm ci --only=production
63+
64+
# FROM node:20-alpine
65+
66+
67+
# WORKDIR /app
68+
69+
# COPY . .
70+
# RUN npm install .
71+
# RUN npm link
72+
# RUN chmod +x sndevopscli.js
73+
# RUN ln sndevopscli.js sndevopscli
74+
# # USER node
75+
# CMD [ "echo Get a command" ]

README.md

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,118 @@
1-
# app-devops-gitlab
1+
# CLI example using npm modules
2+
3+
## Build and install
4+
5+
```sh
6+
npm install .
7+
```
8+
9+
## Running the cli commands
10+
11+
```sh
12+
node src/index.js
13+
```
14+
15+
Follow the prompts to create a package or type --help to get more informaiton
16+
17+
```sh
18+
node src/index.js --help
19+
```
20+
21+
## Creating a bin executable
22+
23+
To create a symlink sndevopscli, run the following commmand
24+
25+
```sh
26+
27+
npm link
28+
29+
```
30+
OR
31+
32+
```sh
33+
npm install -g .
34+
```
35+
36+
This will create a executable sndevopscli in bin folder ( /usr/loca/bin/)
37+
38+
**Running CLI**
39+
40+
```sh
41+
sndevopscli create package --help
42+
```
43+
### Removing symlink
44+
45+
```sh
46+
npm unlink .
47+
```
48+
49+
## Creating Docker image
50+
51+
### Building docker image
52+
53+
```sh
54+
docker build -t servicenowdocker/sndevops-cli:0.01 .
55+
```
56+
57+
```sh
58+
docker push servicenowdocker/sndevops-cli:0.01
59+
```
60+
61+
## Integrating with gitlab
62+
63+
[Example gitlab project](https://gitlab.xxxx.xxxxx.xyz/devops-admin/helloworld/-/blob/main/.gitlab-ci.yml?ref_type=heads)
64+
65+
66+
**Example with passing all ServiceNow info via commandline**
67+
68+
```yaml
69+
70+
stages:
71+
- package
72+
73+
package:
74+
stage: package
75+
image: servicenowdocker/sndevops-cli:0.35
76+
script:
77+
- sndevopscli create artifact -a '[{"name":"artifact-name-$CI_JOB_ID","repositoryName":"artifact-repo-name" ,"version":"1.3.0"}]'
78+
- sndevopscli create package -n "package-mame" -a '[{"name":"artifact-name-$CI_JOB_ID","repositoryName":"artifact-repo-name" ,"version":"1.3.0"}]
79+
80+
```
81+
82+
### Env variables
83+
84+
```
85+
SNOW_URL = <servicenow-instance-url>
86+
SNOW_TOKEN = <servicenow-tool-token>
87+
SNOW_TOOLID = <servicenow-tool-id>
88+
```
89+
90+
**Example with passing all ServiceNow info via commandline**
91+
```yaml
92+
93+
stages:
94+
- package
95+
96+
package:
97+
stage: package
98+
image: servicenowdocker/sndevops-cli:0.35
99+
script:
100+
- sndevopscli create artifact -u <serviceno-url> -t <tool-id> --token <tool-token> -a '[{"name":"artifact-name-$CI_JOB_ID","repositoryName":"artifact-repo-name" ,"version":"1.3.0"}]'
101+
- sndevopscli create package -u <serviceno-url> -t <tool-id> --token <tool-token> -n "package-mame" -a '[{"name":"artifact-name-$CI_JOB_ID","repositoryName":"artifact-repo-name" ,"version":"1.3.0"}]
102+
103+
```
104+
105+
**Example of change creation for ServiceNow via commandline**
106+
```yaml
107+
108+
stages:
109+
- DevOpsChangeApproval
110+
111+
ServiceNow DevOps Change:
112+
stage: DevOpsChangeApproval
113+
image: servicenowdocker/sndevops-cli:0.35
114+
script:
115+
- sndevopscli create change -p '{"changeStepDetails":{"timeout":3600,"interval":100},"attributes":{"short_description":"Automated Software Deployment","description":"Automated Software Deployment.","assignment_group":"XXXXXXX","implementation_plan":"Software update is tested and results can be found in Test Summaries Tab.","backout_plan":"When software fails in production, the previous software release will be re-deployed.","test_plan":"Testing if the software was successfully deployed or not"}}'
116+
117+
```
118+

package-lock.json

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "sndevopscli",
3+
"version": "1.0.0",
4+
"description": "ServiceNow DevOps CLI interface",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"sndevopscli": "node src/index.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"bin": {
11+
"sndevopscli": "src/index.js"
12+
},
13+
"author": "",
14+
"license": "ISC",
15+
"dependencies": {
16+
"axios": "^1.6.0",
17+
"commander": "^11.0.0",
18+
"dotenv": "^16.3.1"
19+
}
20+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const SnDevopsApi = require('../base/sndevopsApi.js')
2+
3+
const API_PACKAGE_PATH = 'api/sn_devops/devops/package/registration';
4+
const API_ARTIFACT_PATH = 'api/sn_devops/devops/artifact/registration';
5+
const BaseEnv = require('../../common/baseEnv.js')
6+
7+
8+
class ArtifactManager extends SnDevopsApi {
9+
10+
createPackage(packageName, artifacts) {
11+
12+
let url = new URL(API_PACKAGE_PATH, this.url);
13+
url.searchParams.append("orchestrationToolId", this.toolId)
14+
15+
console.log("Computed ServiceNow url " + url.toString())
16+
17+
let artifactJson = JSON.parse(artifacts);
18+
let payload = this._getRequestBodyForPackageRegistration(packageName, artifactJson)
19+
20+
this.post(url.toString(), payload, this._getAuthHeaderWithToken());
21+
22+
}
23+
24+
25+
createArtifacts(artifacts) {
26+
27+
let url = new URL(API_ARTIFACT_PATH, this.url);
28+
url.searchParams.append("orchestrationToolId", this.toolId)
29+
console.log("Computed path ServiceNow url " + url.toString())
30+
31+
32+
let artifactJson = JSON.parse(artifacts);
33+
let payload = this._getRequestBodyForArtifactRegistration(artifactJson)
34+
35+
let response = this.post(url.toString(), payload, this._getAuthHeaderWithToken());
36+
console.log("Response for Package Creation" + response);
37+
38+
}
39+
40+
_getRequestBodyForPackageRegistration(packageName, artifacts) {
41+
let requestPayload = {
42+
"name": packageName,
43+
"pipelineName": BaseEnv.CI_PROJECT_PATH,
44+
"stageName": BaseEnv.CI_JOB_NAME,
45+
"taskExecutionNumber": BaseEnv.CI_JOB_ID,
46+
"orchestrationTask": {
47+
"orchestrationTaskURL": BaseEnv.CI_PIPELINE_URL,
48+
"orchestrationTaskName": BaseEnv.CI_JOB_NAME,
49+
"branchName": BaseEnv.CI_COMMIT_BRANCH,
50+
"toolId": this.toolId,
51+
},
52+
"artifacts": [
53+
{
54+
"name": artifacts[0].name,
55+
"pipelineName": BaseEnv.CI_PROJECT_PATH,
56+
"repositoryName": artifacts[0].repositoryName,
57+
"stageName": BaseEnv.CI_JOB_NAME,
58+
"taskExecutionNumber": BaseEnv.CI_JOB_ID,
59+
"semanticVersion": artifacts[0].semanticVersion,
60+
"version": artifacts[0].version
61+
}
62+
]
63+
}
64+
return requestPayload;
65+
66+
}
67+
68+
_getRequestBodyForArtifactRegistration(artifacts) {
69+
70+
let requestPayload = {
71+
"taskExecutionNumber": BaseEnv.CI_JOB_ID,
72+
"pipelineName": BaseEnv.CI_PROJECT_PATH,
73+
"stageName": BaseEnv.CI_JOB_NAME,
74+
"artifacts": artifacts
75+
};
76+
return requestPayload;
77+
78+
}
79+
80+
81+
}
82+
83+
module.exports = ArtifactManager;

0 commit comments

Comments
 (0)