This commit is contained in:
Robert de Bock 2023-01-09 08:50:25 +01:00
parent fe79fff48f
commit e2a95426a0
3 changed files with 102 additions and 53 deletions

54
gitlab-public.yml Executable file
View File

@ -0,0 +1,54 @@
#!/usr/bin/env ansible-playbook
---
- name: Create a pull mirror gitlab project
hosts: localhost
become: no
gather_facts: no
vars:
github_owner: robertdebock
gitlab_namespace: robertdebock-iac
vars_files:
- defaults/main.yml
- vars/main.yml
- vars/vault.yml
tasks:
- name: see if all variables are set
assert:
that:
- github_owner is defined
- role is defined
quiet: yes
- name: Get a detailed list of owned GitLab projects
uri:
url: "https://gitlab.com/api/v4/projects?owned=true&search={{ role }}"
method: GET
headers:
PRIVATE-TOKEN: "{{ gitlab_private_token }}"
return_content: yes
register: gitlab_projects
- name: Pick project_ids from GitLab projects
set_fact:
gitlab_projects: "{{ gitlab_projects.json | selectattr('name', 'equalto', role) | list }}"
- name: pick a single project.
set_fact:
gitlab_project_id: "{{ gitlab_projects[0].id }}"
- name: Patch existing project to mirror
uri:
url: "https://gitlab.com/api/v4/projects/{{ gitlab_project_id }}"
method: PUT
headers:
PRIVATE-TOKEN: "{{ gitlab_private_token }}"
body_format: json
body:
visibility: public
status_code:
- 200
register: create_project
changed_when: yes

View File

@ -30,44 +30,39 @@
Authorization: "Bearer {{ github_token }}" Authorization: "Bearer {{ github_token }}"
register: github_repo register: github_repo
# - name: Show shit - name: Get a detailed list of owned GitLab projects
# debug:
# msg: "{{ github_repo.json }}"
- name: Get GitLab namespace_id
uri: uri:
url: "https://gitlab.com/api/v4/namespaces" url: "https://gitlab.com/api/v4/projects?owned=true&search={{ role }}"
method: GET
headers:
PRIVATE-TOKEN: "{{ gitlab_private_token }}"
return_content: yes
register: gitlab_projects
- name: Pick project_ids from GitLab projects
set_fact:
gitlab_projects: "{{ gitlab_projects.json | selectattr('name', 'equalto', role) | list }}"
- name: pick a single project.
set_fact:
gitlab_project_id: "{{ gitlab_projects[0].id }}"
- name: Patch existing project to mirror
uri:
url: "https://gitlab.com/api/v4/projects/{{ gitlab_project_id }}"
method: PUT
headers: headers:
PRIVATE-TOKEN: "{{ gitlab_private_token }}" PRIVATE-TOKEN: "{{ gitlab_private_token }}"
body_format: json body_format: json
body: body:
search: "{{ gitlab_namespace }}"
register: gitlab_namespace
# - name: Show shit
# debug:
# msg: "{{ gitlab_namespace.json }}"
- name: Create a project that pulls from GitHub
uri:
url: "https://gitlab.com/api/v4/projects"
method: POST
headers:
PRIVATE-TOKEN: "{{ gitlab_private_token }}"
body_format: json
body:
name: "{{ github_repo.json.name }}"
description: "{{ github_repo.json.description }}"
# TODO: Should the `path` include the namespace:
path: "{{ github_repo.json.name }}"
namespace_id: 59906445
import_url: "{{ github_repo.json.clone_url }}" import_url: "{{ github_repo.json.clone_url }}"
mirror: true mirror: true
mirror_triggers_builds: true mirror_trigger_builds: true
status_code: status_code:
- 201 - 200
register: gitlab_project register: create_project
changed_when: yes
# - name: Show shit # - name: Show shit
# debug: # debug:
# msg: "{{ gitlab_project }}" # msg: "{{ create_project }}"

View File

@ -23,7 +23,7 @@
- name: urlencode path - name: urlencode path
set_fact: set_fact:
encoded_path: "{{ namespace + '%2F' + 'ansible-role-' + role }}" encoded_path: "{{ namespace + '%2F' + role }}"
- name: place variable - name: place variable
block: block:
@ -56,25 +56,25 @@
protects: true protects: true
masked: true masked: true
- name: list pipeline schedules # - name: list pipeline schedules
uri: # uri:
url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/pipeline_schedules" # url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/pipeline_schedules"
method: GET # method: GET
headers: # headers:
PRIVATE-TOKEN: "{{ gitlab_private_token }}" # PRIVATE-TOKEN: "{{ gitlab_private_token }}"
register: gitlab_schedules # register: gitlab_schedules
#
- name: delete all pipeline schedules # - name: delete all pipeline schedules
uri: # uri:
url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/pipeline_schedules/{{ item.id }}" # url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/pipeline_schedules/{{ item.id }}"
method: DELETE # method: DELETE
headers: # headers:
PRIVATE-TOKEN: "{{ gitlab_private_token }}" # PRIVATE-TOKEN: "{{ gitlab_private_token }}"
status_code: # status_code:
- 204 # - 204
loop: "{{ gitlab_schedules.json }}" # loop: "{{ gitlab_schedules.json }}"
loop_control: # loop_control:
label: "{{ item.id }}" # label: "{{ item.id }}"
- name: create a new pipeline schedule - name: create a new pipeline schedule
uri: uri: