Skip to content

Instantly share code, notes, and snippets.

@jovenbico
Last active August 22, 2022 04:59
Show Gist options
  • Save jovenbico/22a191e098b396047ea32f8f77980ab6 to your computer and use it in GitHub Desktop.
Save jovenbico/22a191e098b396047ea32f8f77980ab6 to your computer and use it in GitHub Desktop.

Ansible playbook - Gitlab API

Delete pipeline per project

$ ansible-playbook \
  --extra-vars "project_id=** private_token=** updated_before=$(date -d '30 days ago' -u +'%Y-%m-%dT%H:%M:%SZ')" \
  delete-pipeline.yml

Run script with cron

$ sudo apt install cron
$ crontab -e

## every 3rd minute ##
*/3 * * * *  ansible-playbook --extra-vars "project_id=** private_token=** updated_before=2022-07-19T00:00:00Z" /workspaces/gitlab-cleanup/delete-pipeline.yml >> /workspaces/gitlab-cleanup/output.txt

$ sudo service cron start

Reference

https://docs.ansible.com/ansible/2.9/user_guide/playbooks_delegation.html#local-playbooks
https://docs.gitlab.com/ee/user/gitlab_com/index.html#gitlabcom-specific-rate-limits
https://docs.gitlab.com/ee/api/pipelines.html#list-project-pipelines
https://docs.gitlab.com/ee/api/index.html#pagination
https://docs.gitlab.com/ee/api/pipelines.html#delete-a-pipeline

---
- hosts: 127.0.0.1
connection: local
gather_facts: False
tasks:
- name: debuging
debug:
msg: "https://gitlab.com/api/v4/projects/{{ project_id }}/pipelines?per_page=100&sort=asc&updated_before={{ updated_before }}"
- name: get pipeline list
uri:
url: https://gitlab.com/api/v4/projects/{{ project_id }}/pipelines?per_page=100&sort=asc&updated_before={{ updated_before }}
method: GET
headers:
PRIVATE-TOKEN: "{{ private_token }}"
register: _result
# - name: debug _result
# debug: var=_result
# - name: loop _result
# debug:
# msg: "pipelineID >> {{ item.id }}"
# loop: "{{ _result.json }}"
- name: delete pipeline by id
uri:
url: https://gitlab.com/api/v4/projects/{{ project_id }}/pipelines/{{ item.id }}
method: DELETE
headers:
PRIVATE-TOKEN: "{{ private_token }}"
status_code: 204
loop: "{{ _result.json }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment