diff --git a/gitlab-delete-project.yml b/gitlab-delete-project.yml index 8a17ff2..b488056 100755 --- a/gitlab-delete-project.yml +++ b/gitlab-delete-project.yml @@ -6,7 +6,7 @@ gather_facts: no vars: - namespace: robertdebock + namespace: robertdebock-iac vars_files: - vars/main.yml diff --git a/gitlab-import.yml b/gitlab-import.yml new file mode 100755 index 0000000..0ecee69 --- /dev/null +++ b/gitlab-import.yml @@ -0,0 +1,45 @@ +#!/usr/bin/env ansible-playbook +--- +- name: Import repository into 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: + - gitlab_namespace is defined + - github_owner is defined + - role is defined + quiet: yes + + - name: Get GitHub repo information + uri: + url: "https://api.github.com/repos/{{ github_owner }}/{{ role }}" + headers: + Authorization: "Bearer {{ github_token }}" + register: github_repo + + - name: Import repository + uri: + url: "https://gitlab.com/api/v4/import/github" + method: POST + headers: + PRIVATE-TOKEN: "{{ gitlab_private_token }}" + body_format: json + body: + personal_access_token: "{{ github_token }}" + repo_id: "{{ github_repo.json.id }}" + target_namespace: "{{ gitlab_namespace }}" + status_code: + - 201 diff --git a/gitlab-pipeline-trigger.yml b/gitlab-pipeline-trigger.yml new file mode 100755 index 0000000..a9648e5 --- /dev/null +++ b/gitlab-pipeline-trigger.yml @@ -0,0 +1,62 @@ +#!/usr/bin/env ansible-playbook +--- +- name: Kick off a pipeline for a GitLab project + hosts: localhost + become: no + gather_facts: no + + vars: + 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: + - gitlab_namespace is defined + - role is defined + quiet: yes + + - name: Get GitLab project number + uri: + url: https://gitlab.com/api/v4/projects/{{ gitlab_namespace }}%2F{{ role }} + headers: + PRIVATE-TOKEN: "{{ gitlab_private_token }}" + register: gitlab_project_details + + - name: Create pipeline trigger + uri: + url: "https://gitlab.com/api/v4/projects/{{ gitlab_project_details.json.id }}/triggers" + method: POST + headers: + PRIVATE-TOKEN: "{{ gitlab_private_token }}" + body_format: form-urlencoded + body: + description: "Ansible" + status_code: + - 201 + register: pipeline_trigger + + - name: Trigger pipeline + uri: + url: "https://gitlab.com/api/v4/projects/{{ gitlab_project_details.json.id }}/trigger/pipeline" + method: POST + body_format: form-urlencoded + body: + token: "{{ pipeline_trigger.json.token }}" + ref: master + status_code: + - 201 + + - name: Delete pipeline trigger + uri: + url: "https://gitlab.com/api/v4/projects/{{ gitlab_project_details.json.id }}/triggers/{{ pipeline_trigger.json.id }}" + method: DELETE + headers: + PRIVATE-TOKEN: "{{ gitlab_private_token }}" + status_code: + - 204 diff --git a/gitlab-pull-mirror.yml b/gitlab-pull-mirror.yml new file mode 100755 index 0000000..b20a7b5 --- /dev/null +++ b/gitlab-pull-mirror.yml @@ -0,0 +1,73 @@ +#!/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: + - gitlab_namespace is defined + - github_owner is defined + - role is defined + quiet: yes + + - name: Get GitHub repo information + uri: + url: "https://api.github.com/repos/{{ github_owner }}/{{ role }}" + headers: + Authorization: "Bearer {{ github_token }}" + register: github_repo + +# - name: Show shit +# debug: +# msg: "{{ github_repo.json }}" + + - name: Get GitLab namespace_id + uri: + url: "https://gitlab.com/api/v4/namespaces" + headers: + PRIVATE-TOKEN: "{{ gitlab_private_token }}" + body_format: json + 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 }}" + mirror: true + mirror_triggers_builds: true + status_code: + - 201 + register: gitlab_project + +# - name: Show shit +# debug: +# msg: "{{ gitlab_project }}" diff --git a/gitlab.yml b/gitlab.yml index 919e4ed..b881657 100755 --- a/gitlab.yml +++ b/gitlab.yml @@ -6,7 +6,7 @@ gather_facts: no vars: - namespace: robertdebock + namespace: robertdebock-iac vars_files: - defaults/main.yml @@ -23,7 +23,7 @@ - name: urlencode path set_fact: - encoded_path: "{{ namespace + '%2F' + role }}" + encoded_path: "{{ namespace + '%2F' + 'ansible-role-' + role }}" - name: place variable block: @@ -76,20 +76,20 @@ loop_control: label: "{{ item.id }}" -# - name: create a new pipeline schedule -# uri: -# url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/pipeline_schedules" -# method: POST -# body_format: json -# status_code: -# - 201 -# headers: -# PRIVATE-TOKEN: "{{ gitlab_private_token }}" -# body: -# description: "Monthly test" -# ref: master -# cron: "{{ letter_minute_mapping[role[15]] | default('13') }} {{ letter_hour_mapping[role[13]] }} {{ letter_day_mapping[role[13]] }} * *" -# cron_timezone: Amsterdam + - name: create a new pipeline schedule + uri: + url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/pipeline_schedules" + method: POST + body_format: json + status_code: + - 201 + headers: + PRIVATE-TOKEN: "{{ gitlab_private_token }}" + body: + description: "Monthly test" + ref: master + cron: "{{ letter_minute_mapping[role[:1]] | default('13') }} {{ letter_hour_mapping[role[:1]] }} {{ letter_day_mapping[role[:1]] }} * *" + cron_timezone: Amsterdam # - name: disable runnner # uri: @@ -104,18 +104,18 @@ # body: # runner_id: "{{ gitlab_runner_id }}" - - name: enable runnner - uri: - url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/runners" - method: POST - body_format: json - status_code: - - 201 - - 400 - headers: - PRIVATE-TOKEN: "{{ gitlab_private_token }}" - body: - runner_id: "{{ gitlab_runner_id }}" +# - name: enable runnner +# uri: +# url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/runners" +# method: POST +# body_format: json +# status_code: +# - 201 +# - 400 +# headers: +# PRIVATE-TOKEN: "{{ gitlab_private_token }}" +# body: +# runner_id: "{{ gitlab_runner_id }}" - name: disable shared runnners uri: diff --git a/templates/gitlab-ci.yml.j2 b/templates/gitlab-ci.yml.j2 index 5afee60..6a005e6 100644 --- a/templates/gitlab-ci.yml.j2 +++ b/templates/gitlab-ci.yml.j2 @@ -1,5 +1,5 @@ --- -image: "robertdebock/github-action-molecule:4.0.8" +image: "robertdebock/github-action-molecule:5.0.0" variables: PY_COLORS: 1 diff --git a/templates/molecule-action.yml.j2 b/templates/molecule-action.yml.j2 index fdf381e..2d07789 100644 --- a/templates/molecule-action.yml.j2 +++ b/templates/molecule-action.yml.j2 @@ -46,7 +46,7 @@ jobs: - name: parse apparmor for mysql run: sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld - name: molecule - uses: robertdebock/molecule-action@4.0.8 + uses: robertdebock/molecule-action@5.0.0 with: image: {% raw %}${{ matrix.config.image }}{% endraw %} diff --git a/templates/tox.ini.j2 b/templates/tox.ini.j2 index 4ca3131..0c1ff7e 100644 --- a/templates/tox.ini.j2 +++ b/templates/tox.ini.j2 @@ -1,22 +1,22 @@ {{ ansible_managed | comment }} [tox] -minversion = 3.21.4 +minversion = 4.2.4 {% if tox_ansible_versions is defined %} -envlist = py{310}-ansible-{% raw %}{{% endraw %}{% for version in tox_ansible_versions %}{{ version }}{% if not loop.last %},{% endif %}{% endfor %}{% raw %}}{% endraw %} +envlist = py{310}-ansible{% raw %}{{% endraw %}{% for version in tox_ansible_versions %}{{ version }}{% if not loop.last %},{% endif %}{% endfor %}{% raw %}}{% endraw %} {% else %} -envlist = py{310}-ansible-{5,6,7} +envlist = py{310}-ansible{5,6,7} {% endif %} skipsdist = true [testenv] deps = - 5: ansible == 5.* - 6: ansible == 6.* - 7: ansible == 7.* + ansible5: ansible == 5.* + ansible6: ansible == 6.* + ansible7: ansible == 7.* molecule[docker] - docker == 5.* - ansible-lint == 5.* + docker == 6.* + ansible-lint == 6.* commands = molecule test setenv = TOX_ENVNAME={envname} @@ -24,4 +24,8 @@ setenv = ANSIBLE_FORCE_COLOR=1 ANSIBLE_ROLES_PATH=../ -passenv = namespace image tag DOCKER_HOST +passenv = + namespace + image + tag + DOCKER_HOST