GitLab Logo

GitLab retry all failed jobs

Sometimes maybe you have a problem with the runner configuration or the test docker image and then all jobs will fail. Its really hard to retry all jobs manually and in GitLab there is no build in future to do this over the web interface. There is a easy way in GitLab retry all failed jobs.

First you must open the gitlab rails console:

If you using the docker image from gitlab:

sudo docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                    PORTS                                                                                                                  NAMES
c21d357eeca6        gitlab/gitlab-ee:latest   "/assets/wrapper"        32 minutes ago      Up 32 minutes (healthy)   159.168.170.154:22->22/tcp, 159.168.170.154:80->80/tcp, 159.168.170.154:443->443/tcp, 159.168.170.154:4567->4567/tcp   gitlab

docker exec -it c21d357eeca6 gitlab-rails console

Or if you using the package installation:

sudo gitlab-rails console

In the gitlab-rails console now you can write ruby code and talk to the system:

If you like to retry all jobs from all projects you can run this:

# First assign a user to user variable
user = User.find_by(username: 'your username here')

# Find all failed jobs
job = Ci::Build.find_by_sql("select * from ci_builds where status = 'failed'")

job.each { |j| Ci::Build.retry(j, user) }

If you like to retry only all jobs from a specific project:

Find the project_id with the url:

https://git.safematix.com/api/v4/projects/safematix%2Ftomcat_databag

After projects its group/project_name. 🙂

Now copy the id and use this for the db search string!

#First assign a user to user variable
user = User.find_by(username: 'your username here')

# Find all failed jobs for the project with the id 62
job = Ci::Build.find_by_sql("select * from ci_builds where status = 'failed' and project_id = '62'")

job.each { |j| Ci::Build.retry(j, user) }

Leave a Reply

Your email address will not be published. Required fields are marked *

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