-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
187 lines (173 loc) · 6.66 KB
/
Jenkinsfile
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//import groovy.transform.CompileStatic
//@GrailsCompileStatic, @CompileStatic or @CompileDynamic CompileStatic
//@CompileStatic
/* Requires the Docker Pipeline plugin */
//Jenkinsfile (Declarative Pipeline)
pipeline {
//agent { dockerfile true }
//node { label 'Noeud1' }
agent {
docker {
label 'Agent1-cloud1-Noeud1'
image 'node:20.16.0-alpine3.20'
//image 'node:20.16.0-slim'
//args '--user=root -m 512m --cpus=1.5'
args '--user=root -m 512m --cpus=1.5'
///args 'NODE_ENV=node'
// Run the container on the node specified at the
// top-level of the Pipeline, in the same workspace,
// rather than on a new node entirely:
reuseNode true
}
}
environment {
JAVA_HOME = '/usr'
}
options {
ansiColor('xterm')
}
stages {
stage('Clone repository') {
/* Let's make sure we have the repository cloned to our workspace */
steps {
checkout scm
sh 'env'
}
}
stage('demo') {
steps {
// Using xterm
ansiColor('xterm') {
echo '\033[31;1m Executing demo stage: ansicolor xterm \033[0m'
}
// Using vga
ansiColor('vga') {
echo '\033[31;1m command-1 ansicolor vga\033[0m'
}
echo '\033[31;1m command-2 \033[0m'
}
}
stage('Clean') {
steps {
echo '\033[34mClean\033[0m \033[33mStage\033[0m \033[35mPipeline\033[0m'
sh 'npm cache clean --force'
}
}
stage('Build') {
steps {
echo '\033[34mBuild\033[0m \033[33mStage\033[0m \033[35mPipeline\033[0m'
sh 'node --version'
//sh 'npm install jest --loglevel=verbose'
sh 'npm install jest'
//sh 'npm install npm-groovy-lint --loglevel=verbose'
sh 'npm install npm-groovy-lint'
sh 'apk add openjdk17-jre curl openssh-client bash'
sh './install-groovy.sh'
}
}
stage('Groovy-lint Jenkinsfile') {
steps {
echo '\033[34mLint\033[0m \033[33mJenkinsfile\033[0m \033[35mPipeline\033[0m'
echo 'Lint..'
sh 'export PATH="/bin:./node_modules/.bin:$PATH"'
echo "PATH: $PATH"
echo "PWD: $PWD"
sh 'ls -latr'
sh 'ls -latr ./node_modules/.bin'
//sh './node_modules/.bin/npm-groovy-lint --verbose 1 --format --parse Jenkinsfile'
//sh './node_modules/.bin/npm-groovy-lint --format --parse Jenkinsfile'
sh './node_modules/.bin/npm-groovy-lint --verbose --parse --format --nolintafter -s Jenkinsfile'
}
}
stage('Tests') {
steps {
echo '\033[34mTests\033[0m \033[33mStage\033[0m \033[35mPipeline\033[0m'
echo 'Testing..'
sh 'export PATH="/bin/:./node_modules/.bin/:$PATH"'
echo "PATH: $PATH"
sh 'ls -ltr ./node_modules/.bin'
//sh 'npm test'
sh 'node ./node_modules/jest/bin/jest.js'
//sh '~/.bin/jest test.sum.js'
//sh 'jest'
}
}
stage('Release') {
//when { tag pattern: "release-\\d+", comparator: "REGEXP"}
steps {
echo 'Release Staging'
//echo $branch
}
}
/*
stage('Deploy1') {
//agent none
steps {
// plugin credentials inclu
//withCredentials([[$class: 'SSHUserPrivateKeyBinding', credentialsId: 'agent1', keyFileVariable: 'SSH_PRIVATE_KEY', passphraseVariable: '', usernameVariable: 'SSH_USERNAME']]) {
withCredentials([file(credentialsId: 'agent1', variable: 'secretFile')]) {
sh 'cat $secretFile'
sh 'cat $secretFile | base64'
sh 'SSH_PRIVATE_KEY=$(cat $secretFile)'
//sh 'ls -atrlR noeud1/noeud1/workspace/node-test-jenkins-cicd/'
sh 'ssh-agent /bin/bash'
sh '''
eval \$(ssh-agent) && ssh-add "${SSH_PRIVATE_KEY}" && ssh-add -l &&
ENVIRONMENT=${env.ENVIRONMENT} \
PLAYBOOK=${env.PLAYBOOK} \
BASTION_USER=${env.BASTION_USER} \
BASTION_HOST=${env.BASTION_HOST} \
env
'''
//./deploy-ansible.sh
}
}
}
*/
stage('Deploy') {
when {
expression {
currentBuild.result == null || currentBuild.result == 'SUCCESS'
}
}
steps {
echo '\033[34mDeploy\033[0m \033[33mStage\033[0m \033[35mPipeline\033[0m'
//sh 'ls -atrlR /home/agent1_jenkins/noeud1/noeud1/workspace/'
/*
*/
//sshagent(['agent1-ssh)']) {
//sshagent(['ssh-credential-id']) {
//sshagent(credentials: ['agent1']) {
//withCredentials([file(credentialsId: 'agent1_jenkins', variable: 'secretFile')]) {
withCredentials([sshUserPrivateKey(credentialsId: 'agent1', keyFileVariable: 'PK')]) {
sh '''
echo "$PK"
echo "$PK" >ssh_private
cat ssh_private | base64
SSH_PRIVATE_KEY=$(cat ssh_private)
eval `ssh-agent -s`
trap "ssh-agent -k" EXIT
ssh-add ${SSH_PRIVATE_KEY}
ssh -o StrictHostKeyChecking=no agent1_jenkins@192.168.3.84 "mkdir -p ~/deploy"
scp -o StrictHostKeyChecking=no Jenkinsfile agent1_jenkins@192.168.3.84:~/deploy
ssh -o StrictHostKeyChecking=no agent1_jenkins@192.168.3.84 "echo 1 >file"
'''
}
echo 'Success'
echo 'Deploying....'
}
}
}
post {
always {
echo 'Pipeline finished.'
//cleanWs deleteDirs: false, notFailBuild: true
}
success {
echo 'Pipeline succeeded!'
}
failure {
echo 'Pipeline failed!'
}
}
}