Space logo

Space

The intelligent code collaboration platform

Space is pivoting to SpaceCode, focused on Git hosting and code reviews. Learn more →

How-To's

Space Automation. Running Scripts in a Container


This post starts a series of blog posts about Space Automation. If you’re new to the topic, consider reading the Introduction to Space Automation first.

Currently, Docker containers are the main execution environment in Space Automation. So, what can you basically run inside the containers? These are: shell scripts, arbitrary Kotlin code, and container image commands. Let’s take a look at how you can do all of these.

Selecting a container image

Some basics first. To run something inside a container, you should use the container keyword and specify a particular container image. Currently, you can use images either from Docker Hub (just specify an image name):

job("Hello from Docker Hub!") {
   // run nothing in a hello-world container
   container("hello-world") {}
}

or from a Space Packages registry (here you must specify an image URL):

job("Hello from Space Packages!") {
   // run nothing in a custom hello-world container
   container("mycompany.registry.jetbrains.space/p/mpj/mydocker/hello-world") {}
}

Running a shell script

It’s easy: simply write your script inside the shellScript item. The script’s working directory is the project’s root directory (by default, /mnt/space/work). Note that the default script interpreter is /bin/sh, but you can change it using interpreter.

job("Build and copy") {
   container("ubuntu") {
      shellScript {
         interpreter = "/bin/bash"
         content = """
            ./gradlew build 
            cp output $mountDir/share
         """
      }
   }
}

In the script above, we first run the gradlew wrapper and then copy build ouput to the file share. Note that we specify the mount directory using a variable (because it’s Kotlin!).

Running a script file

The next popular scenario is running an existing file from a project repository, e.g. a .sh file. All you need is to specify its location:

job("Run .sh file") {
   container("ubuntu") {
      shellScript {
         location = "./myscript.sh"
      }
   }
}

Automation automatically marks the file specified in location as executable, so, there’s no need in chmod +x. Note that if you specify both content and location, whichever property is specified last takes precedence.

Running Kotlin code

No changes here apart of the naming: Instead of script, you should use kotlinScript. Though, for the sake of backward compatibility, script is still supported.

job("Print total members") {
   container("openjdk:11") {
      kotlinScript { api ->
         val totalMembers = api.space().teamDirectory.stats.getAllStats().totalMembers
         println("Employees total is $totalMembers")
      }
   }
}

Running container image commands

In some cases, you may want to run the default image command. The args keyword runs the default image command with provided arguments. entrypoint and args override the default image command.

job("Hello world") {
   // override the default command set in 'myImage' 
   container("mycompany.registry.jetbrains.space/p/mpj/mydocker/myImage") { 
      entrypoint("/bin/sh") 
      args("echo", "Hello World!") 
   } 
}

That’s it for now! Leave your comments below and, of course, sign up for Space for free and try running your build scripts in Automation.

image description