lmrc_cli/generator/
gitlab_ci.rs

1use colored::Colorize;
2use lmrc_config_validator::LmrcConfig;
3use std::fs;
4use std::path::Path;
5
6use crate::error::Result;
7
8pub fn generate_gitlab_ci(project_path: &Path, config: &LmrcConfig) -> Result<()> {
9    let gitlab_ci_content = generate_gitlab_ci_yaml(config);
10
11    let ci_path = project_path.join(".gitlab-ci.yml");
12    fs::write(&ci_path, gitlab_ci_content)?;
13
14    println!("  {} .gitlab-ci.yml", "Created:".green());
15
16    Ok(())
17}
18
19fn generate_gitlab_ci_yaml(config: &LmrcConfig) -> String {
20    let app_build_jobs = config
21        .apps
22        .applications
23        .iter()
24        .map(|app| {
25            format!(
26                r#"
27build:{}:
28  stage: test
29  image: rust:1.75
30  script:
31    - cd apps/{}
32    - cargo build --release
33    - cargo test
34  artifacts:
35    paths:
36      - apps/{}/target/release/{}
37    expire_in: 1 hour
38  only:
39    - main
40    - merge_requests
41"#,
42                app.name, app.name, app.name, app.name
43            )
44        })
45        .collect::<Vec<_>>()
46        .join("\n");
47
48    format!(
49        r#"# LMRC Stack Pipeline Configuration
50# Generated by lmrc-cli
51
52variables:
53  RUST_VERSION: "1.75"
54  CARGO_HOME: $CI_PROJECT_DIR/.cargo
55
56stages:
57  - build-pipeline
58  - test
59  - provision
60  - setup
61  - deploy
62
63# Cache for Rust builds
64.rust_cache:
65  cache:
66    key: "${{CI_JOB_NAME}}"
67    paths:
68      - .cargo/
69      - target/
70
71# Build the pipeline binary first
72build:pipeline:
73  extends: .rust_cache
74  stage: build-pipeline
75  image: rust:$RUST_VERSION
76  script:
77    - cd infra/pipeline
78    - cargo build --release
79  artifacts:
80    paths:
81      - infra/pipeline/target/release/pipeline
82    expire_in: 1 hour
83  only:
84    - main
85    - merge_requests
86
87# Build and test applications
88{}
89
90# Provision infrastructure
91provision:
92  stage: provision
93  image: rust:$RUST_VERSION
94  dependencies:
95    - build:pipeline
96  script:
97    - ./infra/pipeline/target/release/pipeline provision
98  only:
99    - main
100  when: manual
101
102# Setup infrastructure (K8s, databases, etc.)
103setup:
104  stage: setup
105  image: rust:$RUST_VERSION
106  dependencies:
107    - build:pipeline
108  script:
109    - ./infra/pipeline/target/release/pipeline setup
110  only:
111    - main
112  needs:
113    - provision
114
115# Deploy applications
116deploy:
117  stage: deploy
118  image: rust:$RUST_VERSION
119  dependencies:
120    - build:pipeline
121  script:
122    - ./infra/pipeline/target/release/pipeline deploy
123  only:
124    - main
125  needs:
126    - setup
127"#,
128        app_build_jobs
129    )
130}