記事:
- name: jenkins crumb uri: url: '{{ jenkins_host }}/crumbIssuer/api/json' method: GET user: "{{ jenkins_user }}" password: "{{ jenkins_password }}" force_basic_auth: yes status_code: - 200 return_content: yes register: jenkins_crumb
- name: Get config.xml uri: url: '{{ jenkins_host }}/job/{{ job_name }}/config.xml' method: GET user: "{{ jenkins_user }}" password: "{{ jenkins_password }}" force_basic_auth: yes headers: Jenkins-Crumb: "{{ jenkins_crumb.json.crumb }}" Cookie: "{{ jenkins_crumb.set_cookie }}" status_code: - 200 - 201 return_content: yes register: result_job - name: Export job copy: dest: "{{ dest_path }}/{{ job_name }}.config.xml" content: "{{ result_job.content }}"
- name: createItem uri: url: '{{ jenkins_host }}/createItem?name={{ job_name }}' method: POST user: "{{ jenkins_user }}" password: "{{ jenkins_password }}" force_basic_auth: yes headers: Jenkins-Crumb: "{{ jenkins_crumb.json.crumb }}" Cookie: "{{ jenkins_crumb.set_cookie }}" Content-type: "text/xml" body_format: raw body: "{{ lookup('file','/tmp/ansible-tmp.{{ job_name }}.xml') }}" status_code: - 200 - 201
GUIからpluginを無効化して、jenkinsが起動しなくなった場合、「<plugin>.disabled」ファイルを消すと、pluginが有効になる。
jenkins_plugin=<plugin name> rm $JENKINS_HOME/plugins/${jenkins_plugin}.jpi.disabled sudo service jenkins restart
特定のユーザは特定のjobの実行のみできる等の権限を絞りたい。
curlでjob単体の状態を得る。jqでjsonの整形。
jenkins_url=https://jenkins.example.com:8080/jenkins cookie_jar=/tmp/cookies jenkins_job=test01 jenkins_user=user01 read -sp 'jenkins_password: ' jenkins_password # clumbの取得 jenkins_clumb=$(curl -s -u "${jenkins_user}:${jenkins_password}" -c $cookie_jar ${jenkins_url}/crumbIssuer/api/json | jq -r '.crumb') # job単体の取得 curl -s -u "${jenkins_user}:${jenkins_password}" -H "Jenkins-Crumb:${jenkins_clumb}" ${jenkins_url}/job/${jenkins_job}/api/json | jq
curlでjobの実行:
curl -s -u "${jenkins_user}:${jenkins_password}" \ -H "Jenkins-Crumb:${jenkins_clumb}" \ -b $cookie_jar ${jenkins_url}/job/${jenkins_job}/buildWithParameters?id=123 \ --data-urlencode json='{}'