Search code examples
mavengitlabgitlab-cibuildpackgitlab-autodevops

How to use custom settings.xml with GitLab-CI Auto DevOps?


I'm using GitLab CI Auto DevOps to compile a project based on Maven based in a DinD (docker-in-docker) runner.

CI job start, and buildpack for Maven is correctly detected (based on herokuish).

How can I configure a custom settings.xml file without switching to a custom .gitlab-ci.yml?

I would like to continue using Auto DevOps.


Solution

  • Because maven needs access to a private repository based on Nexus it is convenient to configure a custom settings.xml (and version it with your source code)

    The easiest solution is to include a custom settings.xml. This feature was included in the corresponding buildpack used by Auto DevOps some time ago, as you can see in this article from Heroku buildpacks about "Using a Custom Maven Settings File".

    So I defined MAVEN_SETTINGS_PATH variable in .gitlab-ci.yml config file:

    variables:
     - MAVEN_SETTINGS_PATH: ".m2/settings.xml"
    

    Then, included the file settings.xml in the repository. Avoid to include secrets or another sensible information

    When using a private maven repository with credentials

    Finally, you can define in Gitlab some variables to be used in settings.xml. Using Gitlab UI or API add variables for the user, password, and repository url, to be included as environment variables from Gitlab into the job. Then you can use it in settings.xml like ${env.VARIABLE_NAME}

    Example of Gitlab-CI configuration file:

    include:
      - template: Auto-DevOps.gitlab-ci.yml
    
    variables:
        MAVEN_SETTINGS_PATH: ".m2/settings.xml"
        AUTO_DEVOPS_BUILD_IMAGE_FORWARDED_CI_VARIABLES: NEXUS_REPO_USER,NEXUS_REPO_PASSWORD,NEXUS_REPO_URL
    

    As a final recommendation, you should avoid to use passwords in environment variables, use native methods from your environment for credentials storage is recommended.