Spring Cloud Config Server pushes local changes


Create the Spring Cloud Config Server application

Add configuration

spring:
    application:
        name: config-server
    profiles:
        active: default
    cloud:
        config:
            retry:
                initialInterval: 100000
                multiplier: 1.1
                maxInterval: 900000
                maxAttempts: 6
            server:
                git:
                    uri: https://gitserver.org/configuration-repository.git
                    label: dev-local
                    search-paths: '*'
                    skipSslValidation: false
                    ignoreLocalSshSettings: true
                    strictHostKeyChecking: false
                    clone-on-start: true
                    force-pull: true
                    basedir: ${user.home}/data/configuration-repository
                    username: test_gitlab
                    password: ENC(07b8RBlZEtbdwVMjUBtlyeFPWBIfoVBl)

Add some extended functions for pushing changes

public class ConfigurationManagementServiceImpl
        implements ConfigurationManagementService {

    private static final Logger LOGGER = LoggerFactory
        .getLogger(ConfigurationManagementServiceImpl.class);

    private Git git = null;

    @Autowired
    private MultipleJGitEnvironmentRepository repository;

    @Override
    public void commitAndPush(
        final String application,
        final String profile,
        final String label,
        final String message) throws Exception {

        this.init();

        // Make the local repository ready
        this.repository.getLocations(application, profile, label);

        this.git.commit().setAll(true).setMessage(message).call();

        this.git.push()
            .setCredentialsProvider(this.repository.getGitCredentialsProvider())
            .call();

        LOGGER.info("Pushed local repository with message [{}].", message);
    }

    @SuppressWarnings("unused")
    private File getWorkingDirectory() {
        return this.repository.getBasedir();
    }

    private void init() throws IOException {
        if (this.git != null) {
            return;
        }

        final String repoPath = this.repository.getBasedir().getPath();
        final Repository localRepo = new FileRepository(repoPath + "/.git");
        this.git = new Git(localRepo);
        LOGGER.info("Initialized local repository at path {}", repoPath);
    }

    @SuppressWarnings("unused")
    private void updateLocalRepo() throws Exception {
        LOGGER.info("Updating the local repository.");
        this.init();
        this.git.pull()
            .setCredentialsProvider(this.repository.getGitCredentialsProvider())
            .call();
    }
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.