Something went wrong. Try again.
A collection of +180 reusable WebAssembly modules for building composable CI/CD pipelines with FluentCI fluentci.io/modules
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081module CI.Cache where
import Preludeimport CI.Cache.HostProgram (hostProgram)import CI.ModuleDependencies as Dependenciesimport Data.Argonaut.Core (Json, stringify)import Data.Argonaut.Decode (decodeJson)import Data.Argonaut.Encode (encodeJson)import Data.Argonaut.Parser as Parserimport Data.Array as Aimport Data.Either (Either, either)import Data.Maybe (Maybe(..), fromMaybe)import Data.String as Simport Data.String.CodeUnits as CUimport Data.String.Pattern (Pattern(..))import Data.Traversable (traverse)import Effect (Effect)import Effect.Exception (throw)import FluentCI.Plugin as Pimport FluentCI.Plugin.Internal.Imports as Imports
unwrap :: forall a. P.Result a -> Effect aunwrap = either (throw <<< P.errorMessage) pure
required :: String -> Effect Stringrequired name = do present <- P.hasEnv name >>= unwrap value <- if present then P.getEnv name >>= unwrap else pure "" when (S.trim value == "") (throw ("Set " <> name)) pure value
payloadChunks :: String -> Array StringpayloadChunks value = if value == "" then [] else [ CU.take 40000 value ] <> payloadChunks (CU.drop 40000 value)
invoke :: forall a. String -> Maybe String -> a -> Effect Jsoninvoke operation url input = do _ <- required "FLUENTCI_CACHE_TOKEN" case url of Just value | S.trim value /= "" -> pure unit _ -> void (required "FLUENTCI_CACHE_URL") Dependencies.ensureFor (if operation == "nixBuild" then "nix" else "node") let bootstrap = "import {gunzipSync} from 'node:zlib';await import('data:text/javascript;base64,'+gunzipSync(Buffer.from(process.argv.slice(3).join(''),'base64')).toString('base64'));" output <- P.pipeline ("fluentci-cache-" <> operation) # P.exec ([ "node", "--input-type=module", "--eval", bootstrap, operation, stringify (encodeInput input) ] <> payloadChunks hostProgram) # P.stdout >>= unwrap let last = fromMaybe "" (A.last (S.split (Pattern "\n") (S.trim output))) json <- either (throw <<< show) pure (Parser.jsonParser last) envelope <- either (throw <<< show) pure (decodeJson json :: Either _ { cacheHostResult :: Json }) pure envelope.cacheHostResult
foreign import encodeInput :: forall row. row -> Json
type Variable = { name :: String, value :: String }type SetupResult = { protocol :: String, url :: String, configDir :: String, files :: Array String, binDir :: String, environment :: Array Variable }type TransferResult = { key :: String, path :: String, hit :: Boolean, bytes :: String }
setup :: forall row. { url :: Maybe String | row } -> Effect SetupResultsetup args = do json <- invoke "setup" args.url args result <- either (throw <<< show) pure (decodeJson json :: Either _ { protocol :: String, url :: String, configDir :: String, files :: Array String, binDir :: String, environment :: Array Variable, secretNames :: Array String }) token <- required "FLUENTCI_CACHE_TOKEN" publicPairs <- traverse ( \v -> if v.name == "NIX_CONFIG" then do present <- P.hasEnv "NIX_CONFIG" >>= unwrap previous <- if present then P.getEnv "NIX_CONFIG" >>= unwrap else pure "" pure [ v.name, previous <> "\n" <> v.value ] else pure [ v.name, v.value ] ) result.environment let pairs = publicPairs <> map (\name -> [ name, token ]) result.secretNames Imports.set_envs (stringify (encodeJson pairs)) pure { protocol: result.protocol, url: result.url, configDir: result.configDir, files: result.files, binDir: result.binDir, environment: result.environment }
nixBuild :: forall row. { url :: Maybe String | row } -> Effect { outcome :: String }nixBuild args = invoke "nixBuild" args.url args >>= either (throw <<< show) pure <<< decodeJson
save :: forall row. { url :: Maybe String | row } -> Effect TransferResultsave args = invoke "save" args.url args >>= either (throw <<< show) pure <<< decodeJson
restore :: forall row. { url :: Maybe String | row } -> Effect TransferResultrestore args = invoke "restore" args.url args >>= either (throw <<< show) pure <<< decodeJson