-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathJenkinsfile
160 lines (148 loc) · 6.38 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
def GITHUB_REPO = '[email protected]:zeroc0d3/deploy-laravel.git'
def workspace = env.WORKSPACE
pipeline {
agent any
stages {
stage('Testing Application') {
failFast true
parallel {
stage('Initialize Database') {
steps {
sh 'docker-compose -f /opt/mariadb/docker-compose.yml up -d'
sh "echo '>> Starting Container MariaDB... DONE !'"
sh '''
docker inspect jenkins_mariadb | grep "IP"
"echo '>> Checking for MariaDB services... DONE !'"
'''
}
}
stage('Unit Test') {
agent {
docker {
image 'edbizarro/gitlab-ci-pipeline-php:7.4-alpine'
args '-u root -v /var/run/docker.sock:/var/run/docker.sock'
}
}
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/dev-master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'MYAPP_SSH_PRIVATE_KEY', url: GITHUB_REPO]]])
sh "echo '>> Checkout Repository... DONE !'"
sh '''
cd "${workspace}"
cp ./src/.env.pipeline.jenkins ./src/.env
make fixing-cache
make composer-install-cicd
make key-generate
make composer-dumpautoload
make run-migrate-all
make clear-all
'''
}
}
}
}
stage('Deploy Staging') {
when {
anyOf {
branch 'dev-staging'
environment name: 'DEPLOY_TO', value: 'staging'
}
}
agent {
docker {
image 'ruby:2.7.1-slim-buster'
args '-u root -v /var/run/docker.sock:/var/run/docker.sock'
}
}
environment {
MYAPP_SSH_PRIVATE_KEY = credentials('MYAPP_SSH_PRIVATE_KEY')
MYAPP_SSH_PEM_KEY = credentials('MYAPP_SSH_PEM_KEY')
MYAPP_KNOWN_HOSTS = credentials('MYAPP_KNOWN_HOSTS')
}
steps {
withCredentials(bindings: [sshUserPrivateKey(credentialsId: 'MYAPP_SSH_PRIVATE_KEY', \
keyFileVariable: 'MYAPP_SSH_PRIVATE_KEY')]) {
sh '''
#################
### SETUP SSH ###
#################
apt-get update -qq
apt-get install -qq git build-essential
apt-get install -qq openssh-client
mkdir -p ~/.ssh
echo "${MYAPP_SSH_PRIVATE_KEY}" | tr -d '\r' > ~/.ssh/id_rsa
echo "${MYAPP_KNOWN_HOSTS}" | tr -d '\r' > ~/.ssh/known_hosts
chmod 700 ~/.ssh/id_rsa
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
ssh-keyscan -H 'gitlab.com' >> ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hosts
#######################
### INSTALL LIBRARY ###
#######################
cd "${workspace}"
cp "${MYAPP_SSH_PEM_KEY}" > keys/myapp.pem
chmod 400 keys/myapp.pem
gem install bundler
bundle install
echo "--- ---"
make deploy-staging
'''
}
}
}
stage('Deploy Production') {
when {
anyOf {
branch 'dev-master'
environment name: 'DEPLOY_TO', value: 'production'
}
}
agent {
docker {
image 'ruby:2.7.1-slim-buster'
args '-u root -v /var/run/docker.sock:/var/run/docker.sock'
}
}
environment {
MYAPP_SSH_PRIVATE_KEY = credentials('MYAPP_SSH_PRIVATE_KEY')
MYAPP_SSH_PEM_KEY = credentials('MYAPP_SSH_PEM_KEY')
MYAPP_KNOWN_HOSTS = credentials('MYAPP_KNOWN_HOSTS')
}
steps {
withCredentials(bindings: [sshUserPrivateKey(credentialsId: 'MYAPP_SSH_PRIVATE_KEY', \
keyFileVariable: 'MYAPP_SSH_PRIVATE_KEY')]) {
sh '''
#################
### SETUP SSH ###
#################
apt-get update -qq
apt-get install -qq git build-essential
apt-get install -qq openssh-client
mkdir -p ~/.ssh
echo "${MYAPP_SSH_PRIVATE_KEY}" | tr -d '\r' > ~/.ssh/id_rsa
echo "${MYAPP_KNOWN_HOSTS}" | tr -d '\r' > ~/.ssh/known_hosts
chmod 700 ~/.ssh/id_rsa
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
ssh-keyscan -H 'gitlab.com' >> ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hosts
#######################
### INSTALL LIBRARY ###
#######################
cd "${workspace}"
cp "${MYAPP_SSH_PEM_KEY}" > keys/myapp.pem
chmod 400 keys/myapp.pem
gem install bundler
bundle install
echo "--- ---"
make deploy-production
'''
}
}
}
}
options {
// Only keep the 10 most recent builds
buildDiscarder(logRotator(numToKeepStr:'10'))
}
}