ins*_*itu 5 haskell build shake-build-system docker
我有一个震动构建(版本 0.16.4),它从Dockerfiles 和其他支持文件中构建了很多 docker 图像。我想将所有这些构建在一个“规则”中,其输出将是一个 docker 图像。我已经阅读了如何在 Shake 中定义计时器规则关于shake 中的自定义规则,但这并没有解释如何定义自己的输出。
我想表达类似的东西
Image "foo" "latest" %> \ img -> do
need [ ... ]
buildDockerImage "docker/foo" img
Image "bar" "latest" %> \ img ->
needImage "foo" "latest"
...
Run Code Online (Sandbox Code Playgroud)
然后将图像作为依赖项进行抖动跟踪。我已经在旧版本的shake中实现了那种东西,但我对如何在> 0.16中做到这一点一无所知。
更新:
我已经尝试过这个,遵循https://hackage.haskell.org/package/shake-0.17.3/docs/Development-Shake-Rule.html的指导方针
newtype Repo = Repo String
deriving (Show, Eq, Hashable, Binary, NFData)
newtype Tag = Tag String
deriving (Show, Eq, Hashable, Binary, NFData)
newtype SHA256 = SHA256 String
deriving (Show, Eq, Hashable, Binary, NFData)
newtype Image = Image (Repo,Tag)
deriving (Show, Eq, Hashable, Binary, NFData)
type instance RuleResult Image = SHA256
data ImageRule = ImageRule Image (Action ())
imageRule :: (Repo, Tag) -> Action () -> Rules ()
imageRule k a = addUserRule $ ImageRule (Image k) a
needImage :: (Repo,Tag) -> Action SHA256
needImage = apply1 . Image
toBytes :: String -> BS.ByteString
toBytes = encodeUtf8 . Text.pack
fromBytes :: BS.ByteString -> String
fromBytes = Text.unpack . decodeUtf8
addBuiltinDockerRule :: Rules ()
addBuiltinDockerRule = addBuiltinRule noLint imageIdentity run
where
imageIdentity _ (SHA256 v) = toBytes v
imageSha (Image (Repo r,Tag t)) = do
Stdout out <- cmd "docker" [ "images", "--no-trunc", "-q", r <> ":" <> t ]
pure $ BS.unpack out
run :: BuiltinRun Image SHA256
run key old mode = do
current <- imageSha key
liftIO $ putStrLn ("current:" <> show current)
if mode == RunDependenciesSame && fmap BS.unpack old == Just current then
return $ RunResult ChangedNothing (BS.pack current) (SHA256 $ fromBytes $ BS.pack current)
else do
(_, act) <- getUserRuleOne key (const Nothing) $ \(ImageRule k act) -> if k == key then Just act else Nothing
act
current <- imageSha key
return $ RunResult ChangedRecomputeDiff (BS.pack current) (SHA256 $ fromBytes $ BS.pack current)
Run Code Online (Sandbox Code Playgroud)
然后在Build.hs文件中使用它:
main :: IO ()
main = shakeArgs options $ do
addBuiltinDockerRule
want [ ".haskell.img" ]
imageRule (Repo "haskell", Tag "latest") $ do
need [ "docker/haskell/Dockerfile" ]
cmd "docker" [ "build", "-t", "haskell:latest", "-f", "docker/haskell/Dockerfile" ]
".haskell.img" %> \ fp -> do
needImage (Repo "haskell", Tag "latest")
Stdout out <- cmd "docker" [ "images", "--no-trunc", "-q", "haskell:latest" ]
writeFile' fp out
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但一个缺点是无法处理want图像:我必须在文件中添加规则才能使其工作。
所以所提出的解决方案是有效的。人们可以添加一个“虚假”目标以确保图像是按需构建的:
"haskell.img" ~> void (needImage "haskell")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
179 次 |
| 最近记录: |