mirror of
https://gitlab.com/TECHNOFAB/nix-gitlab-ci.git
synced 2025-12-12 02:00:13 +01:00
feat: improve caching
This commit is contained in:
parent
a2a7f7c4ed
commit
78e6c5e278
4 changed files with 149 additions and 47 deletions
|
|
@ -4,28 +4,53 @@ spec:
|
|||
type: string
|
||||
description: "latest | latest-cachix | latest-attic etc."
|
||||
default: latest
|
||||
cache_files:
|
||||
type: array
|
||||
description: |
|
||||
Files to use as the cache key for the generated pipeline yaml.
|
||||
If you use "ci.nix" to define CI, add that here for example
|
||||
default: ["flake.nix", "flake.lock"]
|
||||
disable_cache:
|
||||
type: string
|
||||
description: |
|
||||
Disables any caching provided by this component. Set to any non-empty value to disable caching.
|
||||
default: ""
|
||||
---
|
||||
|
||||
stages:
|
||||
- build
|
||||
- trigger
|
||||
|
||||
variables:
|
||||
NIX_CI_DISABLE_CACHE: "$[[ inputs.disable_cache ]]"
|
||||
nix-ci:build:
|
||||
stage: build
|
||||
image: registry.gitlab.com/technofab/nix-gitlab-ci/nix-ci:$[[ inputs.image_tag ]]
|
||||
cache:
|
||||
- key:
|
||||
files: $[[ inputs.cache_files ]]
|
||||
paths:
|
||||
- generated-gitlab-ci.yml
|
||||
- key: nix
|
||||
paths:
|
||||
- .nix-cache/
|
||||
before_script:
|
||||
- source setup_nix_ci
|
||||
# generated-gitlab-ci.yml exists in the cache
|
||||
- '[ -f "generated-gitlab-ci.yml" ] && export CACHED=true && echo "Using cached pipeline file (skip cache with NIX_CI_SKIP_CACHE)" || true'
|
||||
# allow the user to manually skip the cache (when the key files are not correctly configured etc.)
|
||||
- '[ -n "$NIX_CI_SKIP_CACHE" ] && unset CACHED && echo "Caching skipped for this job (through NIX_CI_SKIP_CACHE)" || true'
|
||||
# only setup when we need to generate the pipeline yaml
|
||||
- 'if [ -z "$CACHED" ]; then source setup_nix_ci; fi'
|
||||
script:
|
||||
# build the generated-gitlab-ci.yml
|
||||
- nix build .#gitlab-ci-config
|
||||
- install result generated-gitlab-ci.yml
|
||||
# build the generated-gitlab-ci.yml if it does not exist in the cache
|
||||
- 'if [ -z "$CACHED" ]; then nix build .#gitlab-ci-config && install result generated-gitlab-ci.yml; fi'
|
||||
after_script:
|
||||
# upload to binary cache
|
||||
- finalize_nix_ci
|
||||
# NOTE: environment variables of before_script and script don't exist here anymore
|
||||
#
|
||||
# save to binary cache or Gitlab CI cache only if we actually built something
|
||||
# check if /tmp/nix-store-before exists as $CACHED never exists here and the file only exists if "setup_nix_ci" is called
|
||||
- 'if [ -f "/tmp/nix-store-before" ]; then finalize_nix_ci; fi'
|
||||
artifacts:
|
||||
paths:
|
||||
- generated-gitlab-ci.yml
|
||||
|
||||
nix-ci:trigger:
|
||||
stage: trigger
|
||||
needs:
|
||||
|
|
@ -35,4 +60,5 @@ nix-ci:trigger:
|
|||
- artifact: generated-gitlab-ci.yml
|
||||
job: nix-ci:build
|
||||
strategy: depend
|
||||
|
||||
forward:
|
||||
pipeline_variables: true
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue