12

I am using Spring Config to share in a git server the configuration for some Spring Boot microservices.

It works great but when I am traveling I have to work offline sometimes.
I have configured Spring Config microservice local profile to get the config from my local git, (file:) and not to be HTTP git server, so I can change config and test without needing access to main git server.

The problem is that as I am not able to do a "git push" to push the change to main repository, Spring Confgig notes it and shows this message:

The local repository is dirty or ahead of origin. Resetting it to origin/master.

and resets it deleting my last local commit with last config changes.

How can I make Spring Config just to get the last committed configuration in my local git ignoring if it is pushed or not to the main server?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
icordoba
  • 1,834
  • 2
  • 33
  • 60

4 Answers4

4

I had a similar problem. (the difference is that I am not using Spring Config microservice, but runnging spring configuration server as a stand alone application)

But this should work for you as well: Instead of launching local configuration server with a local git repository (file:) I have launched it with native spring profile.

spring:
   profiles:
      active: native
   cloud:
        config:
            server:
                native:
                    searchLocations: file:///path/to/local/git/repository

So now content of local repository is served and no reset is done on access

Deniso
  • 61
  • 3
1

Actually going through spring-cloud-config source code you will see:

            if (!isClean(git, label)) {
                this.logger.warn(
                        "The local repository is dirty or ahead of origin. Resetting"
                                + " it to origin/" + label + ".");
                resetHard(git, label, LOCAL_BRANCH_REF_PREFIX + label);
            }

Solution

So it will always try to do this and the only work around will be similar to this answer: How spring cloud config use local property override remote property

You will need to commit:

spring:
cloud:
    config:
        allowOverride: true
        overrideNone: false

To the configuration properties you are using. e.g.:local/my-app.properties. If you do have a remote repo for storing all the properties, make sure it is merged to master.

Then you could change any application property as you wish in application.yml/application.properties in your Spring Boot app. It won't get overriden by the remote properties file.

Alternative

Alternatively, you could just remove the config file for the environment you are working on as dev environment. In my example above, you could just remove local/my-app.properties from remote repo and commit master. So that it will never bother overriding local application properties from there because no properties files exist for cloud-config.

Please comment if anything is unclear so that I could improve the instruction.

BenBen
  • 65
  • 5
1

You can use an identical directory as not only a file system backend and also a git repository for properties files. You should make 2 bootstrap.properties files for config server. One is for the file backend and the other is for the git repository. For example, I want to use a file system backend for develop and a git repository for production. I made bootstarp-dev.properties and bootstrap-prd.properties.

## bootstarp-dev.properties
server.port=8887 // If you want to launch both a config server for dev and a config server for prd, you must assign different ports.
spring.cloud.config.server.native.searchLocations=file://PATH_OF_YOUR_DIRECTORY
## bootstarp-prd.properties
server.port=8888
spring.cloud.config.server.git.uri=YOUR_GIT_URL

If you launch your config server with -Dspring.profiles.active=dev,native parameter, the config sever access your properties files as local files.

If you launch your config server with -Dspring.profiles.active=prd parameter, the config sever access your properties files as git files.

Done.

CAPLE
  • 21
  • 3
-1

How can I make Spring Config just to get the last committed configuration in my local git ignoring if it is pushed or not to the main server?

This is what --assume-unchaged is for.


--assume-unchaged

Raise the --assume-unchaged flag on this file so it will stop tracking changes on this file

--[no-]assume-unchanged

When this flag is specified, the object names recorded for the paths are not updated.

Instead, this option sets/unsets the assume unchanged bit for the paths.

When the assume unchanged bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

enter image description here

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 1
    Sorry, I need to solve this on Spring Config, not by making git assume that files are unchaged. When I finish my long trips, i get back to Wifi and I will push my commits, just issuing git push, so rest of the team gets my changes. I don't want to have to asume unchanged and assume unchaged in every config file every time I need to work offline, so I am looking for a way to solve this on the Spring Config side, not removing my non-pushed changes. – icordoba Dec 09 '18 at 18:02