Skip to content

Commit 3bf3aa0

Browse files
loxygenKsiketyan
andcommitted
containersへのデプロイの準備をした (#18)
* †追加† 設定ファイルを環境変数のコンテンツから見るようになった * †追加† 多分Docker imagesにpushできるようになった * †追加† Dockerで動くようになった * †編集† Alpineイメージじゃなくてpython:3.8.5-busterイメージにお世話になるようにした * †修正† dockerignoreファイルのファイル末改行を修正した (suggestionより) Co-authored-by: Naoki Ikeguchi <[email protected]> Co-authored-by: Naoki Ikeguchi <[email protected]>
1 parent 4f20bc4 commit 3bf3aa0

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
venv/
2+
.git
3+
.github
4+
.gitignore
5+
.idea
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Docker
2+
3+
on:
4+
push:
5+
# Publish `master` as Docker `latest` image.
6+
branches:
7+
- master
8+
9+
# Publish `v1.2.3` tags as releases.
10+
tags:
11+
- v*
12+
13+
env:
14+
IMAGE_NAME: delitter
15+
16+
jobs:
17+
push:
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
23+
- name: Build an image with a tag
24+
run: docker build -t app .
25+
26+
- name: Log into the registry
27+
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
28+
29+
- name: Push the image
30+
run: |
31+
IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME
32+
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
33+
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
34+
[ "$VERSION" == "master" ] && VERSION=latest
35+
docker tag app $IMAGE_ID:$VERSION
36+
docker push $IMAGE_ID:$VERSION

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3.8.5-buster
2+
RUN mkdir /src
3+
COPY . /src
4+
WORKDIR /src
5+
RUN python3 -m pip install -r requirements.txt
6+
7+
ENTRYPOINT ["python3", "main.py"]

lib/settings/discord.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ def __init__(
4848
self.approve_rate = approve_rate
4949

5050

51-
def create(file: TextIO) -> DiscordSetting:
51+
def create(json_text: str) -> DiscordSetting:
5252
"""
5353
Jsonファイルから設定をパースしてDiscordSettingを生成する
54-
:param file: Jsonファイルを参照しているIO
54+
:param json_text: Jsonで記述された設定
5555
:return: 生成されたDiscordSetting
5656
"""
5757
json_type = Dict[str, Union[None, str, int]]
@@ -60,7 +60,7 @@ def create(file: TextIO) -> DiscordSetting:
6060
with open("static/scheme/discord_scheme.json") as f:
6161
scheme_json: json_type = json.load(f)
6262

63-
raw_json: json_type = json.load(file)
63+
raw_json: json_type = json.loads(json_text)
6464
validate(raw_json, scheme_json)
6565

6666
raw_json.setdefault("token", None)

lib/settings/twitter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def __init__(
3636
self.access_secret_token = access_token_secret
3737

3838

39-
def create(file: TextIO) -> TwitterSetting:
39+
def create(json_text: str) -> TwitterSetting:
4040
"""
4141
Jsonファイルから設定をパースしてTwitterSettingを生成する
42-
:param file: Jsonファイルを参照しているIO
42+
:param json_text: Jsonで記述された設定
4343
:return: 生成されたTwitterSetting
4444
"""
4545
json_type = Dict[str, Union[None, str, int]]
@@ -48,7 +48,7 @@ def create(file: TextIO) -> TwitterSetting:
4848
with open("static/scheme/twitter_scheme.json") as f:
4949
scheme_json: json_type = json.load(f)
5050

51-
raw_json: json_type = json.load(file)
51+
raw_json: json_type = json.loads(json_text)
5252
validate(raw_json, scheme_json)
5353

5454
raw_json.setdefault("token", None)

main.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
------------------------
44
プログラムのエントリポイント。
55
"""
6+
import base64
7+
import os
8+
69
from lib.data.tweet_votes_record import TweetsVoteRecord
710
from lib.discord.client import MainClient
811
from lib.discord.op.command.command_register import CommandRegister
@@ -15,11 +18,18 @@
1518
from lib.twitter.tweeter import Tweeter
1619

1720
if __name__ == '__main__':
18-
with open("settings/discord.json") as f:
19-
discord_setting: discord.DiscordSetting = discord.create(f)
2021

21-
with open("settings/twitter-api.json") as f:
22-
twitter_setting: twitter.TwitterSetting = twitter.create(f)
22+
if "DELITTER_DISCORD_SETTING_JSON_B64" not in os.environ:
23+
raise RuntimeError("DELITTER_DISCORD_SETTING_JSON_B64 is not set!")
24+
25+
if "DELITTER_TWITTER_SETTING_JSON_B64" not in os.environ:
26+
raise RuntimeError("DELITTER_TWITTER_SETTING_JSON_B64 is not set!")
27+
28+
raw_discord_setting = base64.b64decode(os.environ["DELITTER_DISCORD_SETTING_JSON_B64"]).decode("utf-8")
29+
raw_twitter_setting = base64.b64decode(os.environ["DELITTER_TWITTER_SETTING_JSON_B64"]).decode("utf-8")
30+
31+
discord_setting: discord.DiscordSetting = discord.create(raw_discord_setting)
32+
twitter_setting: twitter.TwitterSetting = twitter.create(raw_twitter_setting)
2333

2434
votes_record = TweetsVoteRecord()
2535
command_register = CommandRegister(discord_setting)

0 commit comments

Comments
 (0)