Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
go.mod、DockerfileやCI設定に分散しがちなGoのバージョンをまとめて管理する ...
Search
Arthur
October 23, 2024
Programming
0
380
go.mod、DockerfileやCI設定に分散しがちなGoのバージョンをまとめて管理する / Go Connect #3
https://gotalk.connpass.com/event/331992/
Arthur
October 23, 2024
Tweet
Share
More Decks by Arthur
See All by Arthur
Mackerel開発チームの障害対応演習 ──新卒エンジニアが障害対応指揮官を務めるに至るまでのステップ / Mackerel Drink Up 出張版@福岡
arthur1
0
220
slog登場に伴うloggerの取り回し手法の見直し / kamakura.go #6
arthur1
1
1.8k
otelcol receiver 自作RTA / Pepabo Tech Conference #22 春のSREまつり
arthur1
0
2.9k
見せ算をScalaで実装してみた / Scalaわいわい勉強会 #2
arthur1
0
1.9k
技術習得を支え続けた私の個人開発ヒストリー / Hatena Engineer Seminar #28
arthur1
1
1.6k
Scala の好きなところ 難しいところ / #scala_waiwai
arthur1
0
1.2k
学園祭Web開発の現場とPHPのこれまでとこれから ── 技術選定と教育から語る / PHP Conference Japan 2023
arthur1
0
1k
仮説検証サイクルでユーザーの声を 高速に叶える「キカク組」の取り組み / Mackerel Drink Up #11 arthur-1
arthur1
0
1k
孤独な個人開発からの脱却: バトグラ技術部への憧憬 / 個人開発祭り #2
arthur1
0
780
Other Decks in Programming
See All in Programming
Subclassing, Composition, Python, and You
hynek
3
190
【YAPC::Hakodate 2024】TypeScriptエンジニアが感じたPerlのここが面白い
kimitashoichi
1
480
Serverless renderování Webových komponent
rarous
PRO
0
140
モジュラモノリス、その前に / Modular monolith, before that
euglena1215
8
750
2024-10-01 dev2next - Observability for Modern JVM Applications
jonatan_ivanov
1
140
GPU Hash Table | レイトレ合宿10
yknishidate
0
330
Universal Linksの実装方法と陥りがちな罠
kaitokudou
1
130
Vitest Browser Mode への期待 / Vitest Browser Mode
odanado
PRO
1
1.2k
/←このスケジュール表に立ち向かう フロントエンド開発戦略 / A front-end development strategy to tackle a single-slash schedule.
nrslib
1
430
App Store Connect APIで 作業時間を増やそう
mot_techtalk
3
140
Micro Frontends for Java Microservices - dev2next 2024
mraible
PRO
0
230
Integrating AI in Your Enterprise Java Applications
ivargrimstad
0
860
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
31
1.4k
4 Signs Your Business is Dying
shpigford
180
21k
Music & Morning Musume
bryan
46
6.1k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Adopting Sorbet at Scale
ufuk
73
9k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Being A Developer After 40
akosma
85
590k
Bash Introduction
62gerente
608
210k
How to Ace a Technical Interview
jacobian
275
23k
Designing for Performance
lara
604
68k
Teambox: Starting and Learning
jrom
132
8.7k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Transcript
go.mod、DockerfileやCI設定に 分散しがちなGoのバージョンを まとめて管理する id:arthur-1 株式会社はてな 2024-10-23 Go Connect #3 1
Arthurと申します 株式会社はてな アプリケーションエンジニア 好きなプログラミング言語マスコッ トはD言語くんとDenoの恐竜です 𝕏: @Arthur1__ 2
MackerelはGoを使って作ってます 3
昨日はイベントやってました 4 https://www.youtube.com/live/NJM3HK744Bs?si=Z0XYOkDOk5yOCFHL
本題 5
アプリケーションで 利用したい Goのバージョ ン どう宣言してますか? 6
go.modのgo directive 7 module github.com/Arthur1/hoge go 1.22
go.modのtoolchain directive 8 module github.com/Arthur1/hoge go 1.22 toolchain go1.23.0
Dockerfileのbase image tag 9 FROM golang:1.23-bookworm AS builder COPY .
. RUN go build -o server ./cmd/server
GitHub Actionsのsetup-go 10 - uses: actions/setup-go@v5 with: go-version: 1.23
GitHub Actionsのsetup-go 11 - uses: actions/setup-go@v5 with: go-version-file: go.mod
こんな悩みはないか? • 様々な場所にGoのバージョンが書かれてい て、アップデート時の書き換えが大変 • パッケージの自動更新をするツールでGoの バージョンも自動アップデートしていきたい 12
ソリューションを 提示する前に 13
Goビルトインの バージョン宣言方法紹介 14
go.modのgo directive > The go directive sets the minimum version
of Go required to use this module. https://go.dev/ref/mod#go-mod-file-go ビルドできる最小の言語バージョンを定義する 15
go.modのtoolchain directive > A toolchain directive declares a suggested Go
toolchain to use with a module. https://go.dev/ref/mod#go-mod-file-toolchain 推奨されるGoのツールチェーンのバージョン (の下限)を定義する 16
Goのツールチェーンの仕組み GOTOOLCHAIN=auto(デフォルト)なら、推奨され るツールチェーンかそれより新しいものがない場合に ダウンロードされる goenvやasdfで複数のGoのバイナリを使い分ける必要 は、基本はないはず 17
Goのツールチェーンの仕組み(例) 手元のGoのバイナリが1.23.0で go directiveが1.23.0 →使用されるツールチェーンのバージョンは? 18
Goのツールチェーンの仕組み(例) 手元のGoのバイナリが1.23.0で go directiveが1.23.0 →使用されるツールチェーンのバージョンは1.23.0 19
Goのツールチェーンの仕組み(例) 手元のGoのバイナリが1.23.3で go directiveが1.23.0 →使用されるツールチェーンのバージョンは? 20
Goのツールチェーンの仕組み(例) 手元のGoのバイナリが1.23.3で go directiveが1.23.0 →使用されるツールチェーンのバージョンは1.23.3 21
Goのツールチェーンの仕組み(例) 手元のGoのバイナリが1.23.0で go directiveが1.22.0 toolchain directiveがgo1.23.3 →使用されるツールチェーンのバージョンは? 22
Goのツールチェーンの仕組み(例) 手元のGoのバイナリが1.23.0で go directiveが1.22.0 toolchain directiveがgo1.23.3 →使用されるツールチェーンのバージョンは1.23.3 23
Goのツールチェーンの仕組み(例) 手元のGoのバイナリが1.23.3で go directiveが1.22.0 toolchain directiveがgo1.22.0 →使用されるツールチェーンのバージョンは? 24
Goのツールチェーンの仕組み(例) 手元のGoのバイナリが1.23.3で go directiveが1.22.0 toolchain directiveがgo1.22.0 →使用されるツールチェーンのバージョンは1.23.3 25
[再掲]こんな悩みはないか? • 様々な場所にGoのバージョンが書かれてい て、アップデート時の書き換えが大変 • パッケージの自動更新をするツールでGoの バージョンも自動アップデートしていきたい 26
ソリューション 27
私の主張 アプリケーションで使用されるべき Goのバージョンは go.modのtoolchain directiveで 宣言しよう! 28
ただし 宣言したバージョンかそれ以上のツールチェーンを許容す るので、宣言したものと常にピッタリ一致させたい場合は 工夫が必要 こういうニーズもあるはずだが、今回触れるには時間が足 りなさすぎるので、後で紹介するブログを読んでください 29
この仮定って現実的? 「宣言したバージョンか、それ以上はOK」という管理方 法で大丈夫か?という話 セキュリティ対応で特定のtoolchain以上にしたいという ニーズはあるはずで、これは満たせる Goの後方互換を信じるなら十分そう 30
パブリックな開発では OSSの場合や、公開packageが他のコードから参照され ている場合に、go directiveで不必要に他人のビルド環境 を制約する必要はない go directiveは利用したい言語機能が増えたときにはじめ てアップデートすれば良い 31
toolchainはRenovateで更新可能 Renovateはtoolchain directiveを最新のものに更新 するPull Requestを勝手に作ってくれる 32
Dockerfileや CIの設定に書かれた バージョンはどうする? 33
Dockerfile 公式のGoのイメージはGOTOOLCHAIN=localに なっている(toolchainをダウンロードしない) DockerfileにENV GOTOOLCHAIN=autoと明示的 に書くことで、必要に応じてtoolchainがダウン ロードされる 34
Dockerfile(例) 35 FROM golang:bookworm AS builder ENV GOTOOLCHAIN=auto COPY .
. RUN go build -o server ./cmd/server
GitHub Actionsのsetup-go go-version-file引数にgo.modを渡してあげ たらtoolchain directiveを参照してくれる? 36
GitHub Actionsのsetup-go go-version-file引数にgo.modを渡してあげ たらtoolchain directiveを参照してくれる? →わけではない 37
GitHub Actionsのsetup-go 38 - uses: actions/setup-go@v5 with: go-version-file: go.mod go
1.21.0 toolchain go1.23.0
GitHub Actionsのsetup-go 39 - uses: actions/setup-go@v5 with: go-version-file: go.mod go
1.21.0 toolchain go1.23.0 ① setup-goがgo directiveを見て1.21.0のGoバイナリを準備する ② build時にtoolchain directiveを見て1.23.0をダウンロードする 動くには動くけど、毎回DLしてて勿体無い
GitHub Actionsのsetup-go 40 - uses: actions/setup-go@v5 with: go-version: stable 宣言したツールチェーンと同じか、それより新しいものを許容する場
合には、とりあえずここはstableでよい。古い分にはtoolchainが自 動でダウンロードされる
まとめ Goのバージョンの決定についてはGoのエコシス テムにすべて寄せることで、テストやアップデー ト時の労力を減らすことができる! 今回のやり方ならRenovateのPRをmergeするだ けで推奨ツールチェーンが勝手に上がっていく 41
合わせて読んでね 1. アプリケーションではなくライブラリの場合 2. strictにGoのバージョンを指定したい場合 にも触れています: • Go製アプリケーション/ライブラリにおけるメンテナ ンス性を重視したGoのバージョン管理戦略 •
GitHub ActionsのGoのバージョンをtoolchainディ レクティブの指定ぴったりで固定したい場合 42
おしまい 43