r/gitlab•
absolutejam

Has anyone experimented with ad-hoc jobs via. Gitlab-Ci?

We're in a good position in terms of CI/CD, where all merges into master are linted, built and deployed to Kubernetes.

But Gitlab CI is such a flexible and unopinionated solution that I wondered about the feasibility of using it for on demand jobs that aren't directly related to a CD process - has anyone here tried this?

It would mean we don't have to spin and manage something like Rundeck, and it limits the scope of everyone's abilities to predefined runbook-style operations with our existing permissions model.

I was thinking of the following use cases:

  • For whatever reason a but gets past QA and gets into production, it would be nice to have a way of spinning up the inactive replica (blue/green deploy) and swap traffic to that.
  • We don't have many on demand jobs, but there are a couple go lower priority services that we kick once in a while, and I don't want to venture down the route of automated remediation based on alerts just yet. It would be trivial to implement simple ssh script, or more likely a redeploy for K8s workloads.
  • One off build processes outside a CD workflow.

It seems like the current possibilities to this end are either:

  • Create one or more repos made for this and trigger a CI job - Probably makes it more obvious and provided better separation or concerns.
  • Create one or more bare branches in existing repos that are for this sole purpose - Would be more difficult on some of our larger projects and probably makes it less discoverable.

To start the pipeline, which would probably consist of one or more jobs that fire a Dockerised script/binary, I'm thinking:

  • Use the CI tab in the Gitlab UI to launch job and use the env vars feature to configure any flags.
  • Use the git based flow and make users update a markdown log to explain why it was done (I really like this one)
  • Add the ability to fire these off to our Mattermost bot (meaning we can tick off the ChatOps buzzword 😜)

Apologies if this reads a bit like a 'choose your own adventure', but I wanted to point out my thoughts and get some feedback about idea as a whole and possible implementation.

Posted Sep 26, 2020, 5:20 PM UTC

47
ignurant•

My company has been doing this for about three years on a locally hosted instance. We have hundreds of ETL jobs that run weekly, and we use GitLab to coordinate them. In all, I think we have about 300 jobs per week scheduled via GitLab schedules. Everything from scheduling/triggering weekly jobs in a larger job system (in Sidekiq and Faktory), running large scrape jobs (Python/scrapy), and packaging and delivering data (Ruby/kiba). At first it felt hacky as heck and I was embarrassed by it. But you know what? It has worked fantastic for years, and I don't think I should be embarrassed by it any more. It's a piece of my infrastructure that just reliably works and I find myself not worrying about it. GitLab gets you a lot of cool things with no effort.

I wrote about this just a few months back, so I'll let my old post speak for itself:

https://www.reddit.com/r/ruby/comments/hbi3gv/alternative_to_sidekiq_for_longrunning_jobs/fv9c9xq/

Something I didn't mention in the other comment that I would like to stress is that I like how the project is totally self-contained -- everything from the code and issues, all the way to the scheduling and monitoring. Everything about it is totally discoverable from one source.

5
absolutejamOP•

I'll have a read, thanks!

Yeah, we host our own Gitlab and runners so it feels like a no brainer to leverage the same 'runner' abstraction and more importantly, the same knowledge.

Your point about having everything self contained in its own repo totally makes sense. We're using Jira for issue tracking at the moment (thanks, product team 🙄) but I'm working on an integration because the devs are crying out for a dedicated Gitlab workflow.

EDIT: Do you have one repo per distinct task and do they live parallel to the associated application repo, or under a subgroup?

1
ignurant•

We have a mix of everything. There are two main projects that I work out of in this way -- one is a scrapy project with about 100 scrapers that get triggered through CI (currently ~140 scheduled tasks), and the other is a Ruby project that does a lot of ETL work (currently using about 50 scheduled tasks) -- the libs are all shared, so this project builds a docker image that can power most of our ETL. However, we have several dozen 1-off projects that are essentially single purpose, and I would guess that a lot of them could be consolidated.

We find our development strategy often looks a bit like this:

  • We need to build a new task -- does it fit in an existing project?
  • Build the task wherever that made sense
  • Build up Docker executable so it's repeatable
  • Now that it seems to work, we need to figure out how to deploy it in a production environment -- oh hey, let's just do that one weird trick we've been using and schedule it in GitLab.

I guess it depends on the type of code you are writing. I tend to think of it being broken down into a project per "execution environment" (ruby, with my domain libs).

1
davispw•

See https://docs.gitlab.com/ee/ci/chatops/

Can use this for all kinds of useful jobs, triggered by chat. See the link from that article to GitLab’s own repo of jobs. Things like DB explain queries, getting the current on-call list...useful for ops and debugging jobs, with the bonus that the response gets posted to your chat, so your team can discuss the result.

I just made one to do a rolling restart of my kubernetes deployments for example. (kubectl rollout restart...) which is a useful one-off job in case of emergency, that doesn’t otherwise fit into our declarative GitOps pipeline.

I’m using the Mattermost chat ops plugin. Limitation is mattermost doesn’t echo the original /slash command to the chat. So the first thing I did is make my scripts output a chat_reply section (see docs) and echo $CHAT_INPUT.

AFAIK the ChatOps integration is the only way to trigger a specific job by name, rather than an entire pipeline. If you don’t want to use ChatOps, you can have CI variables and use workflow: rules: if: $RUN_JOB == $CI_JOB_NAME, or something. Conversely, the ChatOps trigger is limited to single jobs. Or your dangling branch idea would work too.

1
absolutejamOP•

I looked at Gitlab ChatOps a while back and I can't honestly remember what I didn't like about it.

It may have been the idea of 'Developer or higher' being able to run, but if we went for the repo per task/task grouo approach then its not really an issue.

I'll weigh up whether this will do or if I'd like a more personalised approach via our bot - eg. Providing info and warnings - but this could be the first step into using this approach.

2
davispw•

Makes sense. I have a separate repo for the permissions question and according to GitLab’s own examples, that’s their approach, too.

I should mention, we got had GitLab ChatOps approved by our cyber security folks. I think they’d be more reluctant about a home-grown bot. Because GitLab is already secured, integrated with AD, requires you to do a handshake to authorize mattermost, jobs are auditable, and all that.

1
jarv•

We also use Chatops internally for running ad-hoc jobs at GitLab, if you are comfortable with Ruby you could borrow some of the supporting code for this here https://gitlab.com/gitlab-com/chatops

1