Skip to main content

nerdbank_gitversioning/cloud_build_services/
gitlab.rs

1// Copyright (c) .NET Foundation and Contributors. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4//! [GitLab CI](https://docs.gitlab.com/ci/variables/) support.
5
6use std::io::{self, Write};
7
8use crate::cloud::{CloudBuild, EnvironmentVariables, empty_variables, env, is_nonempty};
9
10/// GitLab CI integration.
11pub struct GitLab;
12
13impl CloudBuild for GitLab {
14    fn name(&self) -> &'static str {
15        "GitLab"
16    }
17
18    fn is_applicable(&self) -> bool {
19        is_nonempty(env("GITLAB_CI").as_deref())
20    }
21
22    fn is_pull_request(&self) -> bool {
23        false
24    }
25
26    fn building_branch(&self) -> Option<String> {
27        env("CI_COMMIT_TAG").is_none().then(|| {
28            env("CI_COMMIT_REF_NAME")
29                .filter(|name| !name.is_empty())
30                .map(|name| format!("refs/heads/{name}"))
31        })?
32    }
33
34    fn building_tag(&self) -> Option<String> {
35        env("CI_COMMIT_TAG")
36            .filter(|tag| !tag.is_empty())
37            .map(|tag| format!("refs/tags/{tag}"))
38    }
39
40    fn git_commit_id(&self) -> Option<String> {
41        env("CI_COMMIT_SHA")
42    }
43
44    fn set_cloud_build_number(
45        &self,
46        _build_number: &str,
47        _stdout: &mut dyn Write,
48        _stderr: &mut dyn Write,
49    ) -> io::Result<EnvironmentVariables> {
50        Ok(empty_variables())
51    }
52
53    fn set_cloud_build_variable(
54        &self,
55        _name: &str,
56        _value: &str,
57        _stdout: &mut dyn Write,
58        _stderr: &mut dyn Write,
59    ) -> io::Result<EnvironmentVariables> {
60        Ok(empty_variables())
61    }
62}