-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.cjs
More file actions
159 lines (131 loc) · 4.15 KB
/
script.cjs
File metadata and controls
159 lines (131 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const https = require('https');
const githubToken = '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'; // Replace with your GitHub Personal Access Token
const owner = 'SS2199';
const repo = 'CICD';
const branch = 'main'; // Change to your branch name
const pathToFile = '.github/workflows/workflow1.yml'; // Path to your YAML file
const content = `
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install Dependencies
run: |
npm install
- name: SonarQube Analysis
run: |
curl -o sonar-scanner.zip -L https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-5.0.1.3006-windows.zip
echo "C:\\Users\\sindhu\\sonar-scanner-5.0.1.3006-windows\\bin\\sonar-scanner.bat"
- name: Test using Jest
run: npm test
- name: Run JMeter Tests
uses: QAInsights/[email protected]
with:
test-plan-path: ./CSVSample.jmx
- name: Build Docker image
run: |
docker build -t vipv/kuber1:\${{ github.run_number }} .
- name: Push Docker image
run: |
docker login -u vipv -p 9092897730
docker push vipv/kuber1:\${{ github.run_number }}
- name: Run Docker Container
run: |
docker run -d -p 8000:8000 vipv/kuber1:\${{ github.run_number }}
`; // Replace with the content of your updated YAML file
// Get the current SHA of the file from GitHub
function getCurrentFileSHA() {
return new Promise((resolve, reject) => {
const auth = `token ${githubToken}`;
// Define the request options
const options = {
hostname: 'api.github.com',
path: `/repos/${owner}/${repo}/contents/${pathToFile}?ref=${branch}`,
method: 'GET',
headers: {
'User-Agent': 'Node.js',
'Authorization': auth,
},
};
// Send the request to GitHub
const req = https.request(options, (res) => {
let response = '';
res.on('data', (chunk) => {
response += chunk;
});
res.on('end', () => {
if (res.statusCode === 200) {
const fileData = JSON.parse(response);
resolve(fileData.sha);
} else {
reject(`Failed to fetch current file SHA. Status code: ${res.statusCode}`);
}
});
});
req.on('error', (error) => {
reject(`Error fetching current file SHA: ${error.message}`);
});
req.end();
});
}
// Create or update the file on GitHub
async function createOrUpdateFile() {
try {
// Fetch the current SHA of the file
const currentSHA = await getCurrentFileSHA();
// Prepare the data to send in the request
const auth = `token ${githubToken}`;
const encodedContent = Buffer.from(content).toString('base64');
const data = {
message: 'Update CI/CD YAML file', // Change this message for each commit
content: encodedContent,
branch: branch,
sha: currentSHA, // Include the current SHA
};
// Define the request options
const options = {
hostname: 'api.github.com',
path: `/repos/${owner}/${repo}/contents/${pathToFile}`,
method: 'PUT',
headers: {
'User-Agent': 'Node.js',
'Authorization': auth,
'Content-Type': 'application/json',
},
};
// Send the request to GitHub
const req = https.request(options, (res) => {
let response = '';
res.on('data', (chunk) => {
response += chunk;
});
res.on('end', () => {
if (res.statusCode === 200 || res.statusCode === 201) {
console.log('YAML file pushed successfully.');
} else {
console.error('Error pushing YAML file:', response);
}
});
});
req.on('error', (error) => {
console.error('Error pushing YAML file:', error);
});
req.write(JSON.stringify(data));
req.end();
} catch (error) {
console.error('Error:', error);
}
}
// Call the function to create or update the file
createOrUpdateFile();