Skip to content

Commit

Permalink
Setup git page deployment and fix test case failures
Browse files Browse the repository at this point in the history
  • Loading branch information
steven.chiu committed Mar 18, 2019
1 parent 6f63464 commit 05f754d
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 30 deletions.
26 changes: 17 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ script:
- go test -v | go-junit-report > reports/testcase.xml
- go test -coverprofile=cover.out && go tool cover -html=cover.out -o reports/coverage.html
before_script:
- docker-compose -f ci/docker-compose.yml up -d
- docker-compose -f devops/docker-compose.yml up -d
services:
- docker
deploy:
provider: releases
api_key:
secure: ET1YBzj+oCDch5XQYgvjvPDpFOcqxR2mJ7zpdGwXtkIpm93fRRCheoLJHcx2ndhsMiJE/eL5fxqFdncwQL576xTkHfjWOxUPWCGA+w6vy5ASY1RMFgu1BAkeLXxSiSa4Ioi2t/xZ8mUMHuTD5kPP4LHUyg135XIHYOtZiHMFcBor8oEMpWCi6krd05gA0SzTLKC6QV+7KDtL96LsZYzWwS7s8QiUKP+pw8s6vq8paDcagmGMbRn2KUNFl6tGxoAVSx6oJbGeLDo+7ieC+cm+9AO+uCVyUbFowTYA+G9BSy9aQTtem7BJsOQHK46JHG3V7yZDqtmVUs4fDhKp0xY0oQ5foOeEIWCiLMbjfto00x992U4Co5KSSWaftUXZxmiF3nugvMItXxEuRB64wCrQgLXT43oqUg06r92sWhUY4nIcuzxhUBKObH87qyaxAHu+WIi2Ed5FXV0G1iSPsGhT/JIeKjG5j036oQbHkdgUHNCR4YJfGYh0j9jg6AqeIlnUOZ6NXBRmpaGcJZCpyEqlUG2W+gQXx6KHAa9YRVywEXeoC+DGVhlM8hORARqDUhEyHzaaxvQMkyCLNZtL6ak7AVN5caLw2YgdAOyjr6sxHX2Mat7CxAVPtuHdK7L5d8hEXczJrrqZ2tEXSnD00k2ImSCHbkfLi3nW8fZjfAHDHY0=
file_glob: true
file: 'reports/*'
skip_cleanup: true
on:
repo: stevenchiu7311/melody
- provider: releases
api_key:
secure: KcVFyJdx20Qp7ZIFbdBjkPTl3Lg6q6TP6C3MDEDME01SweWGNZKckK4OfVOgctLNCZmZqJVxoRErzTrzAQiqpMuL1eKvN1g+QsWwukaR6U0dM52UwHv6vBmCbtTxVfPP3b5bPlDVSCJwYE75A0yR21oa2HXggp7yjkCa1wah8UOmjsZnESWBvs/6KmdPtTJyybv0FccGRdQ28+NYrT99QiDzMD6NYFJpdpKiQDF8eTzIgQU+UpL3uz0MZvvqwOniEhbRLYZc2b6uvEKttuFFGEPMpjwTJcVqU9KxchivtzQU/jgIeUqdiqhwiPNPqVu7W/syr/h+2c5Mt3ao5tbK+0NARSBijYh3AxUztQa9pQ3YQwmlJUDm3sabJenbQ3PMnjllRSn0HaqkAEZ8rY0tGX5Vc0BC8uw2Kyy4dBMc0P8+E7GHHXisL+Gq1I9o+lBAbTwb0I70ogJXvsyvh7+635ejgqtiG6GLP9ntx/8FPRZXBXVsaiEaY0KeTS4NzfpdpTvphSPHxdcN+lJNnmb5REvC3h26NJSvfYQbZIcfRuuWUXd9UGEGhwvBUkPIjQlBx/PKSo+KwCGf+8bjcAAJKHaVHQH8BvyIAC3QPRvMNnDvsV0QNvAEtq+5CVWbQlZ92AHoAL+bSQmLPQnHc4hn0o8OayDLgP0unB9eKlJLYck=
file_glob: true
file: 'reports/*'
skip_cleanup: true
on:
repo: stevenchiu7311/melody
- provider: pages
local-dir: 'reports'
skip-cleanup: true
github-token:
secure: KcVFyJdx20Qp7ZIFbdBjkPTl3Lg6q6TP6C3MDEDME01SweWGNZKckK4OfVOgctLNCZmZqJVxoRErzTrzAQiqpMuL1eKvN1g+QsWwukaR6U0dM52UwHv6vBmCbtTxVfPP3b5bPlDVSCJwYE75A0yR21oa2HXggp7yjkCa1wah8UOmjsZnESWBvs/6KmdPtTJyybv0FccGRdQ28+NYrT99QiDzMD6NYFJpdpKiQDF8eTzIgQU+UpL3uz0MZvvqwOniEhbRLYZc2b6uvEKttuFFGEPMpjwTJcVqU9KxchivtzQU/jgIeUqdiqhwiPNPqVu7W/syr/h+2c5Mt3ao5tbK+0NARSBijYh3AxUztQa9pQ3YQwmlJUDm3sabJenbQ3PMnjllRSn0HaqkAEZ8rY0tGX5Vc0BC8uw2Kyy4dBMc0P8+E7GHHXisL+Gq1I9o+lBAbTwb0I70ogJXvsyvh7+635ejgqtiG6GLP9ntx/8FPRZXBXVsaiEaY0KeTS4NzfpdpTvphSPHxdcN+lJNnmb5REvC3h26NJSvfYQbZIcfRuuWUXd9UGEGhwvBUkPIjQlBx/PKSo+KwCGf+8bjcAAJKHaVHQH8BvyIAC3QPRvMNnDvsV0QNvAEtq+5CVWbQlZ92AHoAL+bSQmLPQnHc4hn0o8OayDLgP0unB9eKlJLYck=
keep-history: true
on:
repo: stevenchiu7311/melody
File renamed without changes.
108 changes: 108 additions & 0 deletions docs/devops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Deployment with TravisCI
### Installation
```
stevenchiu@CM-PC-02034:~/cmgs$ sudo apt-get install ruby-dev
stevenchiu@CM-PC-02034:~/cmgs$ sudo gem install travis -v 1.8.9 --no-rdoc --no-ri
```
## Github release
### 不加密 sensitive data
```
stevenchiu@CM-PC-02034:~/melody$ sudo travis setup releases
Username: stevenchiu7311
Password for stevenchiu7311: **********
File to Upload:
Deploy only from stevenchiu7311/melody? |yes| yes
Encrypt API key? |yes| no
```
會在
[Developer settings](https://github.com/settings/developers) => [Personal access tokens](https://github.com/settings/tokens)
底下看到
**[automatic releases for stevenchiu7311/melody](https://github.com/settings/tokens/269591027)** _— public_repo_
的產生,如果重複執行指令,這筆 key 就會被覆蓋新的

不過在 .travis 檔裡面這筆 api token 變因此暴露出來,如下
```
deploy:
provider: releases
api_key: ce23d3058032f492ccacff92f15fecd737900617
file: ''
on:
repo: stevenchiu7311/melody
```

要解決此問題需要把 api token 加在 Travis 後台的環境變數中

![enter image description here](https://s3-ap-northeast-1.amazonaws.com/steven-dev/presentation/travis/env.PNG)

然後在 .travis 中使用此環境變數取帶原本暴露的 api token
`all_branches: true` 此為宣告讓 master 以外的分支也能允許 deploy stage 的設定

```
deploy:
provider: releases
api_key:
secure: $API_KEY_SECURE
file_glob: true
file: 'reports/*'
skip_cleanup: true
on:
repo: stevenchiu7311/melody
all_branches: true
```

### 加密 sensitive data
```
stevenchiu@CM-PC-02034:~/melody$ sudo travis setup releases
Username: stevenchiu7311
Password for stevenchiu7311: **********
File to Upload:
Deploy only from stevenchiu7311/melody? |yes| yes
Encrypt API key? |yes| yes
```
會在 .travis 中看到一個 api token 被加密過後的字串
```
deploy:
provider: releases
api_key:
secure: KcVFyJdx20Qp7ZIFbdBjkPTl3Lg6q6TP6C3MDEDME01SweWGNZKckK4OfVOgctLNCZmZqJVxoRErzTrzAQiqpMuL1eKvN1g+QsWwukaR6U0dM52UwHv6vBmCbtTxVfPP3b5bPlDVSCJwYE75A0yR21oa2HXggp7yjkCa1wah8UOmjsZnESWBvs/6KmdPtTJyybv0FccGRdQ28+NYrT99QiDzMD6NYFJpdpKiQDF8eTzIgQU+UpL3uz0MZvvqwOniEhbRLYZc2b6uvEKttuFFGEPMpjwTJcVqU9KxchivtzQU/jgIeUqdiqhwiPNPqVu7W/syr/h+2c5Mt3ao5tbK+0NARSBijYh3AxUztQa9pQ3YQwmlJUDm3sabJenbQ3PMnjllRSn0HaqkAEZ8rY0tGX5Vc0BC8uw2Kyy4dBMc0P8+E7GHHXisL+Gq1I9o+lBAbTwb0I70ogJXvsyvh7+635ejgqtiG6GLP9ntx/8FPRZXBXVsaiEaY0KeTS4NzfpdpTvphSPHxdcN+lJNnmb5REvC3h26NJSvfYQbZIcfRuuWUXd9UGEGhwvBUkPIjQlBx/PKSo+KwCGf+8bjcAAJKHaVHQH8BvyIAC3QPRvMNnDvsV0QNvAEtq+5CVWbQlZ92AHoAL+bSQmLPQnHc4hn0o8OayDLgP0unB9eKlJLYck=
file: ''
on:
repo: stevenchiu7311/melody
```

這個 secure 後面的字串是用 travis 提供的公鑰加密的,從而只有 travis 能解密看到 secure 裏的內容,別人即使看到了 travis.yml 的 secure 也沒法解密得到我的 token ,從而不可能代替我操作 travis ,所以放在 .travis 裡暴露出來也沒關係。我想這也跟實驗這個 secure 字串用環境變數的方式設定在後台卻沒有作用相呼應吧。﹝可以在 build 流程 echo 出來但是 deploy 時仍然顯示錯誤﹞

## Github pages

先建立一個空內容的 branch gh-pages,此為容納佈署內容的分支
```
stevenchiu@CM-PC-02034:~/melody$ git checkout --orphan gh-pages
stevenchiu@CM-PC-02034:~/melody$ touch index.html
stevenchiu@CM-PC-02034:~/melody$ git add index.html
stevenchiu@CM-PC-02034:~/melody$ git commit -m 'first gh-pages commit'
```

以下範例為用 api_key 加密方式認證來同時佈署到 github repository 以及發佈到 github release
```
deploy:
- provider: releases
api_key:
secure: KcVFyJdx20Qp7ZIFbdBjkPTl3Lg6q6TP6C3MDEDME01SweWGNZKckK4OfVOgctLNCZmZqJVxoRErzTrzAQiqpMuL1eKvN1g+QsWwukaR6U0dM52UwHv6vBmCbtTxVfPP3b5bPlDVSCJwYE75A0yR21oa2HXggp7yjkCa1wah8UOmjsZnESWBvs/6KmdPtTJyybv0FccGRdQ28+NYrT99QiDzMD6NYFJpdpKiQDF8eTzIgQU+UpL3uz0MZvvqwOniEhbRLYZc2b6uvEKttuFFGEPMpjwTJcVqU9KxchivtzQU/jgIeUqdiqhwiPNPqVu7W/syr/h+2c5Mt3ao5tbK+0NARSBijYh3AxUztQa9pQ3YQwmlJUDm3sabJenbQ3PMnjllRSn0HaqkAEZ8rY0tGX5Vc0BC8uw2Kyy4dBMc0P8+E7GHHXisL+Gq1I9o+lBAbTwb0I70ogJXvsyvh7+635ejgqtiG6GLP9ntx/8FPRZXBXVsaiEaY0KeTS4NzfpdpTvphSPHxdcN+lJNnmb5REvC3h26NJSvfYQbZIcfRuuWUXd9UGEGhwvBUkPIjQlBx/PKSo+KwCGf+8bjcAAJKHaVHQH8BvyIAC3QPRvMNnDvsV0QNvAEtq+5CVWbQlZ92AHoAL+bSQmLPQnHc4hn0o8OayDLgP0unB9eKlJLYck=
file_glob: true
file: 'reports/*'
skip_cleanup: true
on:
repo: stevenchiu7311/melody
all_branches: true
- provider: pages
local-dir: 'reports'
skip-cleanup: true
github-token:
secure: KcVFyJdx20Qp7ZIFbdBjkPTl3Lg6q6TP6C3MDEDME01SweWGNZKckK4OfVOgctLNCZmZqJVxoRErzTrzAQiqpMuL1eKvN1g+QsWwukaR6U0dM52UwHv6vBmCbtTxVfPP3b5bPlDVSCJwYE75A0yR21oa2HXggp7yjkCa1wah8UOmjsZnESWBvs/6KmdPtTJyybv0FccGRdQ28+NYrT99QiDzMD6NYFJpdpKiQDF8eTzIgQU+UpL3uz0MZvvqwOniEhbRLYZc2b6uvEKttuFFGEPMpjwTJcVqU9KxchivtzQU/jgIeUqdiqhwiPNPqVu7W/syr/h+2c5Mt3ao5tbK+0NARSBijYh3AxUztQa9pQ3YQwmlJUDm3sabJenbQ3PMnjllRSn0HaqkAEZ8rY0tGX5Vc0BC8uw2Kyy4dBMc0P8+E7GHHXisL+Gq1I9o+lBAbTwb0I70ogJXvsyvh7+635ejgqtiG6GLP9ntx/8FPRZXBXVsaiEaY0KeTS4NzfpdpTvphSPHxdcN+lJNnmb5REvC3h26NJSvfYQbZIcfRuuWUXd9UGEGhwvBUkPIjQlBx/PKSo+KwCGf+8bjcAAJKHaVHQH8BvyIAC3QPRvMNnDvsV0QNvAEtq+5CVWbQlZ92AHoAL+bSQmLPQnHc4hn0o8OayDLgP0unB9eKlJLYck=
keep-history: true
on:
repo: stevenchiu7311/melody
all_branches: true
```

接著就可以在 `https://stevenchiu7311.github.io/melody/coverage.html` 看到佈署的頁面
7 changes: 7 additions & 0 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ func (h *hub) len() int {
return len(h.sessions)
}

func (h *hub) registered(session *Session) bool {
h.rwmutex.RLock()
defer h.rwmutex.RUnlock()

return h.sessions[session]
}

func (h *hub) readRedisConn(index int) {
if bucketDebugDump {
go func() {
Expand Down
15 changes: 15 additions & 0 deletions melody.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,21 @@ func (m *Melody) HandleRequestWithKeys(w http.ResponseWriter, r *http.Request, k

session.readPump()

// Ensure session's registration in channel
var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()
for {
registered := m.hub.registered(session)
if registered {
return
}
}
}()
wg.Wait()

if !m.hub.closed() {
m.hub.unregister <- session
}
Expand Down
Loading

0 comments on commit 05f754d

Please sign in to comment.