If a native cube, while running, changes the worker environment, such as installs a package, creates or deletes a file, etc., such environment will still be there for all other cubes running within a single task.
Docker cube: Runs within a Docker container started on a worker. Technically, it runs a custom script or a container script, if the container has an entry point.
If a Docker cube, while running, changes the environment, such environment will be available to other cubes within the task only in case the changes were made within the /sourcecraft directory. All other changes will be removed along with the Docker container.
When working from a container, the directories are mounted as follows:
The directory housing files associated with the task in progress is mounted to /sourcecraft.
The directory to clone the repository to and that is also the working directory (workdir) by default, is mounted to /sourcecraft/workspace.
You can get the paths of these directories from the $SOURCECRAFT_ROOT_DIRECTORY and $SOURCECRAFT_WORKSPACEpredefined environment variables, respectively.
To configure a Docker cube, use the image property to specify the Docker image name and, optionally, username, password, entry point, and arguments.
In cubes, you can use environment variables and secrets. To provide environment variables from a certain cube to others as KEY=VALUE pairs, you can use the $SOURCECRAFT_ENVpredefined variable.
By default, cubes within a single task are run one by one. To link cubes, use the needs property, where you can specify the list of cubes to execute before the current one. If you skip this property, the cube will depend on the one defined immediately before it.
The artifacts that may be created after the cube is run are saved for further use. They will be available for download from the cube in the CI/CD section of the repository for 14 days.
allow_failure: Flag to control your CI/CD process behavior when errors occur in individual cubes. If true, the task will continue as if the cube has completed successfully, even if there is an error. The default value is false. For more information, see Example of a configuration where the task continues despite cube failures.
artifacts: List of paths to the files that will be created after the cube is run and saved for further use. The artifacts will be available for download from the cube in the CI/CD section of the repository for 14 days. For more information, see Example of a cube configuration using artifacts.
if: Cube execution condition. If the condition is not met, the cube is skipped and the job continues. Supports the following operators:
always(): Execute the cube regardless of whether or not the previous cubes were executed successfully, e.g., if you need to clean the environment.
contains(): Check the string for any content, e.g., contains(env.SOURCECRAFT_COMMIT_REF_NAME, "release") or env.SOURCECRAFT_COMMIT_REF_NAME.contains("release").
To reuse a cube from another repository, the cube must have the exported: true flag. If this flag is not set, an attempt to reuse the cube in another repository will fail with an error.
with: GitHub Action parameters. It can only be used together with action.
Warning
Only cloud workers support running GitHub Actions or GitLab pipelines in SourceCraft CI/CD.
Example of a cube configuration using variables, including predefined ones
# Defining global variables# to provide to all cubes of all tasks in all workflows.env:GLOBAL_VAR:global_varGLOBAL_SECRET:${{secrets.<secret_name>}}workflows:my-workflow:# Here you define variables that will be available in all cubes # of all my-workflow tasksenv:WORKFLOW_VAR:workflow-vartasks:-name:my-task# Here you define variables that will be available in all cubes # within my-taskenv:TASK_ENV_VAR:Thisvariableisavailableinallcubesofthistask.# Multi-line variableMULTILINE_VAR:|
multi-var
multi-var
this is my multi-var
cubes:-name:my-cube-1# Here you define variables that will only be available# within my-cube-1env:CUBE_ENV_VAR:Thisvariableisavailableonlyincubemy-cube-1.# Variable with a value from a secretSECRET_VAR:${{secrets.<secret_name>}}# Reusing global variables, # e.g., GLOBAL_VAR and GLOBAL_SECRETLOCAL_VAR:${{env.<global_variable_1>}}LOCAL_SECRET:${{env.<global_variable_2>}}# Reusing workflow variables,# e.g., WORKFLOW_VARLOCAL_VAR2:${{env.<workflow_variable>}}script:-echo"$TASK_ENV_VAR"-echo"$MULTILINE_VAR"-echo"$CUBE_ENV_VAR"-echo"$SECRET_VAR"-echo"$WORKFLOW_VAR"-echo"$LOCAL_VAR"-echo"$LOCAL_VAR2"-echo"$LOCAL_SECRET"-name:my-cube-2# Here you define variables that will only be available # within my-cube-2env:CUBE_ENV_VAR:Thisvariableisavailableonlyincubemy-cube-2.script:-echo"$TASK_ENV_VAR"-echo"$CUBE_ENV_VAR"# Using a predefined variable-echo"$SOURCECRAFT_TASK"-echo"$WORKFLOW_VAR"-echo"$GLOBAL_VAR"-name:my-task-2cubes:-name:my-cube-3script:-echo"$WORKFLOW_VAR"-echo"$GLOBAL_VAR"
cubes:-name:external-cubeenv:CUBE_VAR:helloscript:-echo$CUBE_VARtasks:-name:first-taskcubes:-name:first-cubeuses:external-cube# Reusing a cube with a redefined parameter-name:second-cubeenv:CUBE_VAR:worlduses:external-cubeworkflows:first-workflow:tasks:-first-task
Example of reusing cube parameters from a YAML file in the same repository
The .ci-files/cubes/bash-lib.yaml file containing the cube to reuse:
Example of reusing cube parameters from a YAML file in another repository
The .ci-files/cubes/bash-lib.yaml file with the cube to reuse, stored in the myrepo repository of the myorg organization:
cubes:-name:external-cube-1env:CUBE_VAR:helloscript:-echo$CUBE_VAR# Flag that enables using the cube in other repositoriesexported:true-name:external-cube-2script:-echo"world"exported:true
The repository’s .sourcecraft/ci.yaml file containing the CI configuration:
Example of a configuration with conditional cube execution
workflows:workflow-name:tasks:-name:task-namecubes:# The cube will be executed only if the workflow has been executed in the branch whose name # contains "release", and the target branch in the pull request is "main"-name:condition-cube-1if:contains(env.SOURCECRAFT_COMMIT_REF_NAME,"release")&&env.SOURCECRAFT_BASE_REF=="main"script:-echo'The source branch name contains text "release" and the target branch is "main".'# The cube will be executed only if the workflow has been executed in the branch whose name# contains "feature", and the target branch in the pull request is not "main"-name:condition-cube-2if:env.SOURCECRAFT_COMMIT_REF_NAME.contains("feature")||env.SOURCECRAFT_BASE_REF!="main"script:-echo'The source branch name contains text "feature" or the target branch is not "main".'# Executing a cube regardless of whether the previous cubes were successful # or not-name:condition-cube-3if:always()script:-env-ibash
Docker images
The image section contains parameters of the Docker image used to execute the cube.
You can specify a standard Docker image name or a path in a particular registry.
If the image section is not specified, the commands will be executed in Linux environment.
Supported optional properties:
args: Arguments to provide as input to a starting container. You cannot use this property in a cube where the script property is specified.
entrypoint: Redefining the container entry point. Same as the --entrypoint flag for the docker run command.
To learn more about using the entry point and arguments, see the relevant section.
name: Path to the Docker image in the registry.
password: Password to access the registry. You may want to use secrets to store your passwords.
username: Username to access the registry.
Example of a Docker image configuration with a standard name
Example of a Docker image configuration with authentication
If authentication is required to access the registry, you can use the docker login command in your task or configure authentication in the image section and use a secret, e.g.:
To run commands in the container context, specify the Docker image path in the registry using the image or image:name property, and provide the commands in the script property, e.g.:
If the container has a redefined entry point, you will need to reset the latter before running a command from script. You can do this by specifying "" in the entrypoint property. For example, you can use a container that has the ENTRYPOINT ["/usr/bin/docker"] entry point set by default:
Providing command-line arguments to a Docker container
To provide arguments, which you need to run a container, to a container with a redefined entry point, use the args property. For example, this may be the case for a container used to create a new Docker image: