mirror of
https://gitlab.com/TECHNOFAB/nix-gitlab-ci.git
synced 2025-12-12 02:00:13 +01:00
chore: improve "default" handling & caching
add new env variables to configure which CI source gets mapped to default add logic to check if pipeline exists in flake, else build "default"
This commit is contained in:
parent
55f90b4261
commit
85431f78aa
1 changed files with 17 additions and 7 deletions
|
|
@ -28,6 +28,10 @@ variables:
|
||||||
_NIX_CI_CACHE_STRATEGY: $[[ inputs.cache_strategy ]]
|
_NIX_CI_CACHE_STRATEGY: $[[ inputs.cache_strategy ]]
|
||||||
# for multiple pipelines
|
# for multiple pipelines
|
||||||
_NIX_CI_PIPELINE_NAME: ${CI_PIPELINE_SOURCE}
|
_NIX_CI_PIPELINE_NAME: ${CI_PIPELINE_SOURCE}
|
||||||
|
# set NIX_CI_DEFAULT_SOURCES to something like "/^web/" or "/^web|push/",
|
||||||
|
# then this will default to the "default" pipeline for these sources.
|
||||||
|
# By default we map everything to "default" (man that's quite a bunch of defaults)
|
||||||
|
_NIX_CI_DEFAULT_SOURCES: "/.*/"
|
||||||
.env_overrides: &env_overrides
|
.env_overrides: &env_overrides
|
||||||
rules:
|
rules:
|
||||||
- if: $NIX_CI_IMAGE != null
|
- if: $NIX_CI_IMAGE != null
|
||||||
|
|
@ -36,6 +40,12 @@ variables:
|
||||||
- if: $NIX_CI_CACHE_STRATEGY != null
|
- if: $NIX_CI_CACHE_STRATEGY != null
|
||||||
variables:
|
variables:
|
||||||
_NIX_CI_CACHE_STRATEGY: $NIX_CI_CACHE_STRATEGY
|
_NIX_CI_CACHE_STRATEGY: $NIX_CI_CACHE_STRATEGY
|
||||||
|
- if: $NIX_CI_DEFAULT_SOURCES != null
|
||||||
|
variables:
|
||||||
|
_NIX_CI_DEFAULT_SOURCES: $NIX_CI_DEFAULT_SOURCES
|
||||||
|
- if: '$CI_PIPELINE_SOURCE =~ $_NIX_CI_DEFAULT_SOURCES'
|
||||||
|
variables:
|
||||||
|
_NIX_CI_PIPELINE_NAME: default
|
||||||
- if: $NIX_CI_PIPELINE_NAME != null
|
- if: $NIX_CI_PIPELINE_NAME != null
|
||||||
variables:
|
variables:
|
||||||
_NIX_CI_PIPELINE_NAME: $NIX_CI_PIPELINE_NAME
|
_NIX_CI_PIPELINE_NAME: $NIX_CI_PIPELINE_NAME
|
||||||
|
|
@ -47,30 +57,30 @@ nix-ci:build:
|
||||||
cache:
|
cache:
|
||||||
- key:
|
- key:
|
||||||
files: $[[ inputs.cache_files ]]
|
files: $[[ inputs.cache_files ]]
|
||||||
|
prefix: $_NIX_CI_PIPELINE_NAME
|
||||||
paths:
|
paths:
|
||||||
- generated-gitlab-ci.yml
|
- generated-gitlab-ci.yml
|
||||||
- key: nix
|
- key: nix
|
||||||
paths:
|
paths:
|
||||||
- .nix-cache/
|
- .nix-cache/
|
||||||
before_script:
|
before_script:
|
||||||
# generated-gitlab-ci-<pipeline>.yml exists in the cache
|
# generated-gitlab-ci.yml exists in the cache
|
||||||
- '[ -f "generated-gitlab-ci-${_NIX_CI_PIPELINE_NAME}.yml" ] && export CACHED=true && echo "A cached pipeline file exists (skip cache with NIX_CI_FORCE_BUILD)" || true'
|
- '[ -f "generated-gitlab-ci.yml" ] && export CACHED=true && echo "A cached pipeline file exists (skip cache with NIX_CI_FORCE_BUILD)" || true'
|
||||||
# allow the user to manually skip the cache (when the key files are not correctly configured etc.)
|
# allow the user to manually skip the cache (when the key files are not correctly configured etc.)
|
||||||
- '[ -n "$_NIX_CI_FORCE_BUILD" ] && unset CACHED && echo "Caching skipped for this job (through NIX_CI_FORCE_BUILD)" || true'
|
- '[ -n "$_NIX_CI_FORCE_BUILD" ] && unset CACHED && echo "Caching skipped for this job (through NIX_CI_FORCE_BUILD)" || true'
|
||||||
# only setup when we need to generate the pipeline yaml
|
# only setup when we need to generate the pipeline yaml
|
||||||
- 'if [ -z "$CACHED" ]; then source setup_nix_ci; fi'
|
- 'if [ -z "$CACHED" ]; then source setup_nix_ci; fi'
|
||||||
script:
|
script:
|
||||||
# build the generated-gitlab-ci.yml if it does not exist in the cache
|
# build the generated-gitlab-ci.yml if it does not exist in the cache
|
||||||
- 'if [ -z "$CACHED" ]; then nix build .#gitlab-ci:pipeline:${_NIX_CI_PIPELINE_NAME} && install result generated-gitlab-ci-${_NIX_CI_PIPELINE_NAME}.yml; fi'
|
- 'if [ -z "$CACHED" ]; then nix eval --json ".#gitlab-ci:pipeline:${_NIX_CI_PIPELINE_NAME}" &>/dev/null && _NAME=$_NIX_CI_PIPELINE_NAME || _NAME="default"; fi'
|
||||||
|
- 'if [ -z "$CACHED" ]; then nix build .#gitlab-ci:pipeline:${_NAME} && install result generated-gitlab-ci.yml; fi'
|
||||||
after_script:
|
after_script:
|
||||||
# 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
|
# 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
|
# 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'
|
- 'if [ -f "/tmp/nix-store-before" ]; then finalize_nix_ci; fi'
|
||||||
artifacts:
|
artifacts:
|
||||||
paths:
|
paths:
|
||||||
- generated-gitlab-ci-${_NIX_CI_PIPELINE_NAME}.yml
|
- generated-gitlab-ci.yml
|
||||||
nix-ci:trigger:
|
nix-ci:trigger:
|
||||||
<<: *env_overrides
|
<<: *env_overrides
|
||||||
stage: trigger
|
stage: trigger
|
||||||
|
|
@ -78,7 +88,7 @@ nix-ci:trigger:
|
||||||
- nix-ci:build
|
- nix-ci:build
|
||||||
trigger:
|
trigger:
|
||||||
include:
|
include:
|
||||||
- artifact: generated-gitlab-ci-${_NIX_CI_PIPELINE_NAME}.yml
|
- artifact: generated-gitlab-ci.yml
|
||||||
job: nix-ci:build
|
job: nix-ci:build
|
||||||
strategy: depend
|
strategy: depend
|
||||||
forward:
|
forward:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue