This repository was archived by the owner on Mar 14, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathupdate.js
156 lines (136 loc) · 4.48 KB
/
update.js
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
import utils from 'shipit-utils'
import path from 'path2/posix'
import moment from 'moment'
import chalk from 'chalk'
import util from 'util'
import rmfr from 'rmfr'
import _ from 'lodash'
import extendShipit from '../../extendShipit'
/**
* Update task.
* - Set previous release.
* - Set previous revision.
* - Create and define release path.
* - Copy previous release (for faster rsync)
* - Set current revision and write REVISION file.
* - Remote copy project.
* - Remove workspace.
*/
const updateTask = shipit => {
utils.registerTask(shipit, 'deploy:update', async () => {
extendShipit(shipit)
/**
* Copy previous release to release dir.
*/
async function copyPreviousRelease() {
const copyParameter = shipit.config.copy || '-a'
if (!shipit.previousRelease || shipit.config.copy === false) return
shipit.log('Copy previous release to "%s"', shipit.releasePath)
await shipit.remote(
util.format(
'cp %s %s/. %s',
copyParameter,
path.join(shipit.releasesPath, shipit.previousRelease),
shipit.releasePath,
),
)
}
/**
* Create and define release path.
*/
async function createReleasePath() {
/* eslint-disable no-param-reassign */
shipit.releaseDirname = moment.utc().format('YYYYMMDDHHmmss')
shipit.releasePath = path.join(shipit.releasesPath, shipit.releaseDirname)
/* eslint-enable no-param-reassign */
shipit.log('Create release path "%s"', shipit.releasePath)
await shipit.remote(`mkdir -p ${shipit.releasePath}`)
shipit.log(chalk.green('Release path created.'))
}
/**
* Remote copy project.
*/
async function remoteCopy() {
const options = _.get(shipit.config, 'deploy.remoteCopy') || {
rsync: '--del',
}
const rsyncFrom = shipit.config.rsyncFrom || shipit.workspace
const uploadDirPath = path.resolve(
rsyncFrom,
shipit.config.dirToCopy || '',
)
shipit.log('Copy project to remote servers.')
await shipit.remoteCopy(`${uploadDirPath}/`, shipit.releasePath, options)
shipit.log(chalk.green('Finished copy.'))
}
/**
* Set shipit.previousRevision from remote REVISION file.
*/
async function setPreviousRevision() {
/* eslint-disable no-param-reassign */
shipit.previousRevision = null
/* eslint-enable no-param-reassign */
if (!shipit.previousRelease) return
const revision = await shipit.getRevision(shipit.previousRelease)
if (revision) {
shipit.log(chalk.green('Previous revision found.'))
/* eslint-disable no-param-reassign */
shipit.previousRevision = revision
/* eslint-enable no-param-reassign */
}
}
/**
* Set shipit.previousRelease.
*/
async function setPreviousRelease() {
/* eslint-disable no-param-reassign */
shipit.previousRelease = null
/* eslint-enable no-param-reassign */
const currentReleaseDirname = await shipit.getCurrentReleaseDirname()
if (currentReleaseDirname) {
shipit.log(chalk.green('Previous release found.'))
/* eslint-disable no-param-reassign */
shipit.previousRelease = currentReleaseDirname
/* eslint-enable no-param-reassign */
}
}
/**
* Set shipit.currentRevision and write it to REVISION file.
*/
async function setCurrentRevision() {
shipit.log('Setting current revision and creating revision file.')
const response = await shipit.local(
`git rev-parse ${shipit.config.branch}`,
{
cwd: shipit.workspace,
},
)
/* eslint-disable no-param-reassign */
shipit.currentRevision = response.stdout.trim()
/* eslint-enable no-param-reassign */
await shipit.remote(
`echo "${shipit.currentRevision}" > ${path.join(
shipit.releasePath,
'REVISION',
)}`,
)
shipit.log(chalk.green('Revision file created.'))
}
async function removeWorkspace() {
if (shipit.config.shallowClone) {
shipit.log(`Removing workspace "${shipit.workspace}"`)
await rmfr(shipit.workspace)
shipit.log(chalk.green('Workspace removed.'))
}
}
await setPreviousRelease()
await setPreviousRevision()
await createReleasePath()
await copyPreviousRelease()
await remoteCopy()
await setCurrentRevision()
await removeWorkspace()
shipit.emit('updated')
})
}
export default updateTask