-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.ts
103 lines (82 loc) · 3.79 KB
/
index.ts
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
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as github from '@actions/github'
import * as io from '@actions/io'
import * as ioUtil from '@actions/io/lib/io-util'
const DEFAULT_DEPLOY_BRANCH = 'master'
async function run(): Promise<void> {
try {
const skipPublish = (core.getInput('skip-publish') || 'false').toUpperCase()
const accessToken = core.getInput('access-token')
if (!accessToken && skipPublish !== 'TRUE') {
core.setFailed(
'No personal access token found. Please provide one by setting the `access-token` input for this action, or disable publishing by setting `skip-publish`.',
)
return
}
let deployBranch = core.getInput('deploy-branch')
if (!deployBranch) deployBranch = DEFAULT_DEPLOY_BRANCH
const deployRepo = core.getInput('deploy-repo')
const isSameRepo = !deployRepo || deployRepo === github.context.repo.repo
if (isSameRepo && github.context.ref === `refs/heads/${deployBranch}`) {
console.log(`Triggered by branch used to deploy: ${github.context.ref}.`)
console.log('Nothing to deploy.')
return
}
const workingDir = core.getInput('working-dir') || '.'
const pkgManager = (await ioUtil.exists(`${workingDir}/yarn.lock`)) ? 'yarn' : 'npm'
console.log(`Installing your site's dependencies using ${pkgManager}.`)
await exec.exec(`${pkgManager} install`, [], {cwd: workingDir})
console.log('Finished installing dependencies.')
let gatsbyArgs = core.getInput('gatsby-args').split(/\s+/).filter(Boolean)
if (gatsbyArgs.length > 0) {
gatsbyArgs = ['--', ...gatsbyArgs]
}
console.log('Ready to build your Gatsby site!')
console.log(`Building with: ${pkgManager} run build ${gatsbyArgs.join(' ')}`)
await exec.exec(`${pkgManager} run build`, gatsbyArgs, {cwd: workingDir})
console.log('Finished building your site.')
const cnameExists = await ioUtil.exists(`${workingDir}/CNAME`)
if (cnameExists) {
console.log('Copying CNAME over.')
await io.cp(`${workingDir}/CNAME`, `${workingDir}/public/CNAME`, {force: true})
console.log('Finished copying CNAME.')
}
if (skipPublish === 'TRUE') {
console.log('Building completed successfully - skipping publish')
return
}
const repo = `${github.context.repo.owner}/${deployRepo || github.context.repo.repo}`
const repoURL = `https://${accessToken}@github.com/${repo}.git`
console.log('Ready to deploy your new shiny site!')
console.log(`Deploying to repo: ${repo} and branch: ${deployBranch}`)
console.log('You can configure the deploy branch by setting the `deploy-branch` input for this action.')
await exec.exec(`git init`, [], {cwd: `${workingDir}/public`})
const gitUserName = core.getInput('git-config-name') || github.context.actor
const gitEmail = core.getInput('git-config-email') || `${github.context.actor}@users.noreply.github.com`
await exec.exec(`git config user.name`, [gitUserName], {
cwd: `${workingDir}/public`,
})
await exec.exec(`git config user.email`, [gitEmail], {
cwd: `${workingDir}/public`,
})
await exec.exec(`git add`, ['.'], {cwd: `${workingDir}/public`})
const commitMessageInput =
core.getInput('commit-message') || `deployed via Gatsby Publish Action 🎩 for ${github.context.sha}`
await exec.exec(`git commit`, ['-m', commitMessageInput], {
cwd: `${workingDir}/public`,
})
await exec.exec(`git push`, ['-f', repoURL, `master:${deployBranch}`], {
cwd: `${workingDir}/public`,
})
console.log('Finished deploying your site.')
console.log('Enjoy! ✨')
} catch (err) {
core.setFailed((err as Error).message)
}
}
// Don't auto-execute in the test environment
if (process.env['NODE_ENV'] !== 'test') {
run()
}
export default run