How to automatically create branch from schedule job

Automatically create branch from schedule job

I have some scripts, which create part of repository. Now - i run them manually, and them commit it, create merge requests etc.

I would like to put that job into a schedule job - runned every hour, which after changing something create a new branch and create merge request - so after that i (or any other developer) only need to approve them and merge.

How to achieve something like it?

I think about schedule job started on master every hour and put there credentials(ssh key?) to push into repository? How to automaticly create a merge request after creating a new branch? API?

Maybe there is other, simpler sollution?

Did you found a solution for this?

You can create a git token to access the repository. I am using this to push tags git push --tags "https://${CI_GIT_USER}:${CI_GIT_TOKEN}@${CI_REPOSITORY_URL#*@}"

For this to work you need to create user ant token and store them in CI/DI settings.

And for MR you can use API https://docs.gitlab.com/ee/api/merge_requests.html#create-mr

Yes, it’s something what can work. But - it need to have an “system” user in gitlab($$$). Better idea is using ssh to push code.
But - for MR there is need for an user… I still look for working idea :wink:

Is there a script available to show the steps or is it as simple as:

  1. git pull other repo
  2. git branch
  3. apply changed code
  4. git commit
  5. git push
  6. curl to open a PR for the new branch

You can easily get user using predefined variables:

Variable GitLab Runner Description
GITLAB_USER_EMAIL 8.12 all The email of the user who started the job
GITLAB_USER_ID 8.12 all The id of the user who started the job
GITLAB_USER_LOGIN 10.0 all The login username of the user who started the job
GITLAB_USER_NAME 10.0 all The real name of the user who started the job

It’s not so easy. I can use it to commit files, but - not for push nor create MR. For pushing - i can use ssh-key which is added as deploy key with write permission. But for create MR i need real token of user who is able to create MR (what mean - who takes a license).
Another problem(?) is that i don’t want do it by hand, but from regular “cronlike” job.

I did some poc solution for that. It works, but:

  • there is need for token of real user. Is there any way to don’t use real users token for create merge request? Something like system token who can create merge requests?
  • schedule job - i’ve created cron’like entry */10 * * * * - but it still run every 1 hour. Is there any way to run it more often?
  • we had previously created configuration which put message on slack after every build (failed, or not). Is there any way to don’t put messages on slack for scheduler jobs? I see only option to show failed jobs - which partialy solve that need - but - i would like to put message after every build except scheduled…