簡體   English   中英

如何在非 Spring 應用程序的 application.properties 中從 (.gitlab-ci.yml) 設置變量?

[英]How to set variables from (.gitlab-ci.yml) in application.properties in non Spring application?

我在 GitLab 中設置了一個項目變量 - NONPROD_TEST_VARIABLE

我的 gitlab-ci 文件:

variables:
  JAVA_IMAGE_NAME: maven:3.8.4-amazoncorretto-17
  TEST_VARIABLE: ${NONPROD_TEST_VARIABLE}

acceptanceTest:
  image: $JAVA_IMAGE_NAME
  script:
    - ./gradlew test
  tags:
    - nonprod-docker-runner
  when: always

我已經檢查並在作業運行期間成功設置了此 NONPROD_TEST_VARIABLE 中的TEST_VARIABLE

我的資源中也有application.properties文件:

##Set Test variable
TEST_VARIABLE=${TEST_VARIABLE}

和 PropertiesLoader 在運行時從application.properties讀取變量:

public final class PropertiesLoader {

  private PropertiesLoader() {}

  public static String getTestProperty() throws IOException {
    return loadProperties().getProperty("TEST_VARIABLE");
  }

  private static Properties loadProperties() throws IOException {
    Properties configuration = new Properties();
    InputStream inputStream =
        PropertiesLoader.class.getClassLoader().getResourceAsStream("application.properties");
    configuration.load(inputStream);
    inputStream.close();
    return configuration;
  }
}

但是在 GitLab 中運行作業期間,我得到了這個 PropertiesLoader.class 的java.lang.IllegalArgumentException ,這意味着來自 gitlab-ci 的這個變量沒有在application.properties中成功設置。

它不是 Spring 應用程序,我沒有任何不同的環境,我正在使用 gradle。

test {
    useJUnitPlatform()
}

實際上有一個測試類正在運行一些測試,我只想要在測試運行期間來自 gitlab 的變量

有人可以告訴我我做錯了什么嗎? 也許有更好的方法來存儲和傳遞這些變量,或者我忘記了什么?

我設法解決了我的問題。 似乎像我以前那樣從application.properties讀取變量對於非彈簧應用程序來說是錯誤的方式。

我刪除了

##Set Test variable
TEST_VARIABLE=${TEST_VARIABLE}

來自application.properties因為它是不必要的。 變量直接在 gitlab-ci.yml 中設置,就像我之前做的那樣

variables:
  JAVA_IMAGE_NAME: maven:3.8.4-amazoncorretto-17
  TEST_VARIABLE: ${NONPROD_TEST_VARIABLE}

我從PropertiesLoader中得到它的價值,如下所示:

public static String getTestProperty() {
    return System.getenv("TEST_VARIABLE");
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM