|
| 1 | +{-# LANGUAGE OverloadedStrings, RecordWildCards, LambdaCase, ScopedTypeVariables #-} |
| 2 | + |
| 3 | +import Turtle hiding (option) |
| 4 | +import Prelude hiding (FilePath) |
| 5 | +import qualified Filesystem.Path.CurrentOS as FP |
| 6 | +import Control.Monad.Trans.Maybe |
| 7 | +import Control.Exception |
| 8 | +import qualified Data.Text as T |
| 9 | +import Safe |
| 10 | +import System.Exit (exitWith) |
| 11 | +import Options.Applicative |
| 12 | + |
| 13 | +data BuildkiteEnv = BuildkiteEnv |
| 14 | + { bkBuildNum :: Int |
| 15 | + , bkPipeline :: Text |
| 16 | + , bkBranch :: Text |
| 17 | + } deriving (Show) |
| 18 | + |
| 19 | +data CICacheConfig = CICacheConfig |
| 20 | + { ccMaxSize :: Maybe Text |
| 21 | + , ccBucket :: Text |
| 22 | + , ccRegion :: Maybe Text |
| 23 | + , ccPrefix :: Text |
| 24 | + , ccBranch :: Text |
| 25 | + , ccBaseBranch :: Text |
| 26 | + } deriving (Show) |
| 27 | + |
| 28 | +data RebuildOpts = RebuildOpts |
| 29 | + { optBuildDirectory :: Maybe FilePath |
| 30 | + , optBaseBranch :: Text |
| 31 | + } deriving (Show) |
| 32 | + |
| 33 | +rebuildOpts :: Parser RebuildOpts |
| 34 | +rebuildOpts = RebuildOpts <$> optional buildDir <*> baseBranch |
| 35 | + where |
| 36 | + buildDir = option (FP.decodeString <$> str) (long "build-dir" <> metavar "DIR" <> help "Copy sources to directory before building") |
| 37 | + baseBranch = option str ( long "base-branch" <> value "master" <> showDefault <> help "Fallback base branch for cache-s3" ) |
| 38 | + |
| 39 | +parseOpts :: IO RebuildOpts |
| 40 | +parseOpts = execParser opts |
| 41 | + where opts = info (rebuildOpts <**> helper) |
| 42 | + ( fullDesc <> progDesc "Build cardano-sl project with stack in Buildkite" ) |
| 43 | + |
| 44 | +main :: IO () |
| 45 | +main = do |
| 46 | + RebuildOpts{..} <- parseOpts |
| 47 | + awsCreds |
| 48 | + bk <- getBuildkiteEnv |
| 49 | + maybe (pure ()) setupBuildDirectory optBuildDirectory |
| 50 | + cacheConfig <- getCacheConfig bk optBaseBranch |
| 51 | + cacheDownloadStep cacheConfig |
| 52 | + buildResult <- buildStep |
| 53 | + cacheUploadStep cacheConfig |
| 54 | + exitWith buildResult |
| 55 | + |
| 56 | +buildStep :: IO ExitCode |
| 57 | +buildStep = do |
| 58 | + echo "+++ Build and test" |
| 59 | + build .&&. test |
| 60 | + where |
| 61 | + cfg = ["--dump-logs", "--color", "always"] |
| 62 | + stackBuild args = run "stack" $ cfg ++ ["build", "--fast"] ++ args |
| 63 | + buildArgs = ["--bench", "--no-run-benchmarks", "--no-haddock-deps"] |
| 64 | + buildAndTest = stackBuild $ ["--tests"] ++ buildArgs |
| 65 | + build = stackBuild $ ["--no-run-tests"] ++ buildArgs |
| 66 | + test = stackBuild ["--test", "--jobs", "1"] |
| 67 | + |
| 68 | +-- buildkite agents have S3 creds installed, but under different names |
| 69 | +awsCreds :: IO () |
| 70 | +awsCreds = mapM_ (uncurry copy) things |
| 71 | + where |
| 72 | + copy src dst = need src >>= maybe (pure ()) (export dst) |
| 73 | + things = [ ( "BUILDKITE_S3_ACCESS_KEY_ID" |
| 74 | + , "AWS_ACCESS_KEY_ID") |
| 75 | + , ( "BUILDKITE_S3_SECRET_ACCESS_KEY" |
| 76 | + , "AWS_SECRET_ACCESS_KEY") |
| 77 | + , ( "BUILDKITE_S3_DEFAULT_REGION" |
| 78 | + , "AWS_DEFAULT_REGION") |
| 79 | + ] |
| 80 | + |
| 81 | +getBuildkiteEnv :: IO (Maybe BuildkiteEnv) |
| 82 | +getBuildkiteEnv = runMaybeT $ do |
| 83 | + bkBuildNum <- MaybeT $ needRead "BUILDKITE_BUILD_NUMBER" |
| 84 | + bkPipeline <- MaybeT $ need "BUILDKITE_PIPELINE_SLUG" |
| 85 | + bkBranch <- MaybeT $ need "BUILDKITE_BRANCH" |
| 86 | + pure BuildkiteEnv{..} |
| 87 | + |
| 88 | +needRead :: Read a => Text -> IO (Maybe a) |
| 89 | +needRead v = (>>= readMay) . fmap T.unpack <$> need v |
| 90 | + |
| 91 | +getCacheConfig :: Maybe BuildkiteEnv -> Text -> IO (Either Text CICacheConfig) |
| 92 | +getCacheConfig Nothing _ = pure (Left "BUILDKITE_* environment variables are not set") |
| 93 | +getCacheConfig (Just BuildkiteEnv{..}) ccBaseBranch = do |
| 94 | + ccMaxSize <- need "CACHE_S3_MAX_SIZE" |
| 95 | + ccRegion <- need "AWS_REGION" |
| 96 | + need "S3_BUCKET" >>= \case |
| 97 | + Just ccBucket -> pure (Right CICacheConfig{ccBranch=bkBranch, ccPrefix=bkPipeline, ..}) |
| 98 | + Nothing -> pure (Left "S3_BUCKET environment variable is not set") |
| 99 | + |
| 100 | +cacheDownloadStep :: Either Text CICacheConfig -> IO () |
| 101 | +cacheDownloadStep cacheConfig = do |
| 102 | + echo "--- CI Cache Download" |
| 103 | + case cacheConfig of |
| 104 | + Right cfg -> restoreCICache cfg `catch` |
| 105 | + \ (ex :: IOException) -> do |
| 106 | + eprintf ("Failed to download CI cache: "%w%"\nContinuing anyway...\n") ex |
| 107 | + Left ex -> eprintf ("Not using CI cache because "%s%"\n") ex |
| 108 | + |
| 109 | +cacheUploadStep :: Either Text CICacheConfig -> IO () |
| 110 | +cacheUploadStep cacheConfig = do |
| 111 | + echo "--- CI Cache Upload" |
| 112 | + case cacheConfig of |
| 113 | + Right cfg -> saveCICache cfg `catch` |
| 114 | + \ (ex :: IOException) -> do |
| 115 | + eprintf ("Failed to upload CI cache: "%w%"\n") ex |
| 116 | + Left _ -> printf "CI cache not configured.\n" |
| 117 | + |
| 118 | +restoreCICache :: CICacheConfig -> IO () |
| 119 | +restoreCICache cfg = do |
| 120 | + -- cacheS3 cfg (Just $ ccBaseBranch cfg) "restore stack" |
| 121 | + cacheS3 cfg (Just $ ccBaseBranch cfg) "restore stack work" |
| 122 | + |
| 123 | +saveCICache :: CICacheConfig -> IO () |
| 124 | +saveCICache cfg = do |
| 125 | + -- cacheS3 cfg Nothing "save stack" |
| 126 | + cacheS3 cfg Nothing "save stack work" |
| 127 | + |
| 128 | +cacheS3 :: CICacheConfig -> Maybe Text -> Text -> IO () |
| 129 | +cacheS3 CICacheConfig{..} baseBranch cmd = void $ run "cache-s3" args |
| 130 | + where |
| 131 | + args = ml maxSize ++ ml regionArg ++ |
| 132 | + [ format ("--bucket="%s) ccBucket |
| 133 | + , format ("--prefix="%s) ccPrefix |
| 134 | + , format ("--git-branch="%s) ccBranch |
| 135 | + , "--suffix=linux" |
| 136 | + , "-v" |
| 137 | + , "info" |
| 138 | + ] ++ cmds ++ ml baseBranchArg |
| 139 | + baseBranchArg = format ("--base-branch="%s) <$> baseBranch |
| 140 | + maxSize = format ("--max-size="%s) <$> ccMaxSize |
| 141 | + regionArg = format ("--region="%s) <$> ccRegion |
| 142 | + cmds = ("-c":T.words cmd) |
| 143 | + ml = maybe [] pure |
| 144 | + |
| 145 | +-- cache-s3 needs a build directory that is the same across all |
| 146 | +-- buildkite agents. The build directory option can be used to ensure |
| 147 | +-- this is the case. |
| 148 | +setupBuildDirectory :: FilePath -> IO () |
| 149 | +setupBuildDirectory buildDir = do |
| 150 | + exists <- testpath buildDir |
| 151 | + when exists $ do |
| 152 | + printf ("Removing old build directory "%fp%"\n") buildDir |
| 153 | + rmtree buildDir |
| 154 | + src <- pwd |
| 155 | + printf ("Copying source tree "%fp%" -> "%fp%"\n") src buildDir |
| 156 | + cptree src buildDir |
| 157 | + cd buildDir |
| 158 | + |
| 159 | +run :: Text -> [Text] -> IO ExitCode |
| 160 | +run cmd args = do |
| 161 | + printf (s%" "%s%"\n") cmd (T.unwords args) |
| 162 | + res <- proc cmd args empty |
| 163 | + case res of |
| 164 | + ExitSuccess -> pure () |
| 165 | + ExitFailure code -> eprintf ("error: Command exited with code "%d%"!\nContinuing...\n") code |
| 166 | + pure res |
0 commit comments