Managing Artifacts Across Stages in GitLab CI
In GitLab CI, you can define multiple stages in your .gitlab-ci.yml file, allowing you to run different scripts in a sequential manner. One common use case is to build a tool in one stage and then use it in a subsequent stage for testing. To achieve this, you can declare the generated tool as an artifact, which makes it available for later jobs.
Example Configuration
In the following example, we have two stages: build and test. The build stage compiles a tool and saves it as an artifact, while the test stage runs tests using that tool.
releasebuild:
stage: build
script:
- chcp 65001
- build.cmd
artifacts:
paths:
- artifacts/bin/TestTool/
systemtests:
stage: test
script:
- chcp 65001
- artifacts/bin/TestTool/TestTool.exe
- run_tests.bat
Explanation of the Configuration
- releasebuild Job: This job is responsible for building the tool. It runs the
build.cmdscript and specifies that the directoryartifacts/bin/TestTool/should be saved as an artifact. This directory contains the executable and any necessary files (like DLLs). - systemtests Job: This job runs in the
teststage and retrieves the artifact created in the previous stage. The script first sets the code page to UTF-8 and then executes the built tool (TestTool.exe) along with a script (run_tests.bat) to perform the tests.
Important Notes
- Ensure that the paths you specify in the
artifactssection are relative to the repository root. - Artifacts are automatically downloaded to the working directory of the job that follows the one that created them, so you can directly reference them in your scripts.
- If you need to pass additional files or directories, you can modify the
artifactspaths accordingly.
By structuring your .gitlab-ci.yml file this way, you can effectively manage dependencies between different stages of your CI/CD pipeline, ensuring that each job has access to the necessary files produced in earlier stages.