-
Notifications
You must be signed in to change notification settings - Fork 1
156 lines (155 loc) · 5.18 KB
/
deploy.yml
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
name: Actionsflow Site Deploy
on:
push:
branches:
- main
pull_request:
workflow_dispatch:
repository_dispatch:
types: [redeploy]
jobs:
init:
runs-on: ubuntu-latest
# outpus result type is string, true will be "true"
outputs:
sourceBranch: ${{ steps.sourceBranch.outputs.result }}
deployBranch: ${{ steps.deployBranch.outputs.result }}
isDeployBranchExist: ${{ steps.isDeployBranchExist.outputs.result }}
isSourceBranch: ${{ steps.isSourceBranch.outputs.result }}
isValidForBuild: ${{ steps.isValidForBuild.outputs.result }}
steps:
- id: sourceBranch
name: get source branch
uses: actions/github-script@v2
with:
script: |
return 'main'
result-encoding: string
- id: deployBranch
name: get deploy branch
uses: actions/github-script@v2
with:
script: |
return 'gh-pages'
result-encoding: string
- id: isSourceBranch
name: get current branch is source branch
uses: actions/github-script@v2
env:
SOURCE_BRANCH: ${{ steps.sourceBranch.outputs.result }}
with:
script: |
if(`refs/heads/${process.env.SOURCE_BRANCH}` === context.ref){
return true
}else{
return false
}
result-encoding: string
- id: isDeployBranchExist
name: get is deploy branch exist
uses: actions/github-script@v2
env:
DEPLOY_BRANCH: ${{ steps.deployBranch.outputs.result }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
return github.repos.getBranch({
owner: context.repo.owner,
repo: context.repo.repo,
branch: process.env.DEPLOY_BRANCH
}).then(data=>{
return true;
}).catch((e)=>{
if(e && e.status===404){
// then manual trigger
return false
}else{
core.setFailed('check isDeployBranchExist error: ',e.message)
}
})
- id: isValidForBuild
name: check if valid for build
uses: actions/github-script@v2
env:
RESPONSE: ${{ steps.blogaAuth1ClientResponse.outputs.response }}
with:
script: |
if(process.env.SSH_DEPLOY_KEY){
return true
}else{
return false
}
result-encoding: string
build:
needs: init
runs-on: ubuntu-latest
name: Build
steps:
- name: Dump INIT_OUTPUTS
run: echo $INIT_OUTPUTS
env:
INIT_OUTPUTS: ${{ toJson(needs.init.outputs) }}
- name: checkout repo
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Get gatsby cache key from last build
id: get-gatsby-latest-cache-key-file
if: needs.init.outputs.isDeployBranchExist == 'true'
uses: actions/checkout@v2
continue-on-error: true
with:
ref: ${{ needs.init.outputs.deployBranch }}
path: .bloga-last-build
- name: Get gatsby-latest-cache-key content
if: ${{ steps.get-gatsby-latest-cache-key-file.outcome == 'success' }}
id: gatsby-latest-cache-key
run: echo "::set-output name=key::$(cat .bloga-last-build/_gatsby-latest-cache-key.txt)"
- name: Get yarn cache directory
id: yarn-cache-dir
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Gatsby cache directory restore
uses: actions/cache@v2
if: ${{ steps.gatsby-latest-cache-key.outcome == 'success' }}
with:
key: ${{ runner.os }}-${{ steps.gatsby-latest-cache-key.outputs.key }}
path: |
.cache
public
- name: Yarn cache
uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache-dir.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Yarn install
run: "yarn --pure-lockfile"
- name: Build app
run: yarn build
- name: Get gatsby latest cache folder hash
id: gatsby-latest-cache-hash
uses: theowenyoung/[email protected]
with:
path: |
.cache
public
- name: Save gatsby cache directory
uses: actions/cache@v2
with:
key: ${{ runner.os }}-gatsby-cache-directory-${{ steps.gatsby-latest-cache-hash.outputs.hash}}
path: |
.cache
public
- name: Save cache key to file
run: echo gatsby-cache-directory-${{ steps.gatsby-latest-cache-hash.outputs.hash}} > public/_gatsby-latest-cache-key.txt
- name: Deploy
uses: peaceiris/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public
force_orphan: true
publish_branch: ${{ needs.init.outputs.deployBranch }}
user_name: "github-actions[bot]"
user_email: "github-actions[bot]@users.noreply.github.com"
cname: ${{ needs.init.outputs.cname }}