我有一个摇晃的构建(版本0.16.4),它从Dockerfile
和其他支持文件中构建了很多docker镜像。我想排除所有构建在单个“规则”中的内容,这些“规则”的输出将是docker映像。我已经阅读过How to define a timer rule in Shake的有关自定义规则的信息,但这并不能解释如何定义自己的输出。
我想表达类似的东西
Image "foo" "latest" %> \ img -> do
need [ ... ]
buildDockerImage "docker/foo" img
Image "bar" "latest" %> \ img ->
needImage "foo" "latest"
...
,然后摇晃图像作为依赖项。我已经在旧版本的摇晃中实现了这种功能,但是我对在> 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)
然后在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
这似乎可行,但是一个缺点是无法want
映像:我必须在文件中添加规则以使其正常工作。
答案 0 :(得分:0)
因此,建议的解决方案有效。可以添加一个“伪”目标以确保图像按需构建:
"haskell.img" ~> void (needImage "haskell")