如何从GitLab CI创建Jira任务?附Jenkins实现参考代码
jiraNewIssue) Unlike Jenkins, which offers a dedicated plugin with the jiraNewIssue step for Jira integration, GitLab CI doesn't have a built-in command specifically for this task. However, you can replicate the exact functionality by directly calling the Jira REST API from your GitLab CI job using tools like curl (pre-installed in most GitLab CI runners by default).
Step 1: Prepare Required Variables
First, store sensitive Jira credentials and configuration as GitLab CI/CD Variables (go to your project's Settings > CI/CD > Variables):
JIRA_URL: Your Jira instance base URL (e.g.,https://your-jira-domain.atlassian.net)JIRA_USER: Your Jira user email (used for API authentication)JIRA_API_TOKEN: An API token generated for your Jira account (create this in Atlassian Account Settings)PROJECT_ABBR: Your Jira project key (matches$PROJECT_ABBRfrom your Jenkins code)
Step 2: GitLab CI Job Example
Here's a job that mirrors your Jenkins workflow to create a Jira Epic:
create_jira_epic: stage: create_jira script: # Encode Jira credentials for basic authentication - JIRA_AUTH=$(echo -n "${JIRA_USER}:${JIRA_API_TOKEN}" | base64) # Define the Epic fields (matches your Jenkins `epicIssueFields` structure) - | EPIC_FIELDS=$(cat <<EOF { "fields": { "project": {"key": "${PROJECT_ABBR}"}, "description": "New JIRA Created from GitLab CI.", "customfield_10007": "Это Epic Name - ${CI_PIPELINE_IID}", # Epic Name custom field ID "customfield_10100": {"id": "10100"}, # Adjust this ID to match your Jira's Epic Status field "summary": "Лучшая тема в мире", "issuetype": {"name": "Epic"} } } EOF ) # Send POST request to Jira API to create the Epic - | RESPONSE=$(curl -s -X POST "${JIRA_URL}/rest/api/3/issue" \ -H "Authorization: Basic ${JIRA_AUTH}" \ -H "Content-Type: application/json" \ -d "${EPIC_FIELDS}") # Extract and echo the Epic key (equivalent to your `EPIC_NUMBER` variable) - EPIC_NUMBER=$(echo "${RESPONSE}" | grep -o '"key":"[^"]*"' | cut -d'"' -f4) - echo "Created Jira Epic: ${EPIC_NUMBER}" only: # Add triggers here (e.g., main branch, tags, etc.) - main
Key Notes:
- Custom Field IDs: The fields
customfield_10007(Epic Name) andcustomfield_10100are specific to your Jira instance. If these don't work, you can find the correct IDs by calling the Jira API endpoint${JIRA_URL}/rest/api/3/issue/createmeta?projectKeys=${PROJECT_ABBR}&issuetypeNames=Epic&expand=issuetypes.fields. - CI Variables: Replace
${CI_PIPELINE_IID}with${BUILD_NUMBER}if you prefer using the build number (GitLab usesCI_PIPELINE_IIDas the pipeline's sequential ID by default). - Error Handling: For production use, you might want to add error checking (e.g., verify the API response status code is 201 Created) to fail the job if the Epic isn't created successfully.
Alternative: GitLab Jira Integration
GitLab has a native Jira integration that can link GitLab pipelines/commits to existing Jira issues, but it doesn't support creating new issues directly out of the box. The REST API approach is the most straightforward way to replicate your Jenkins workflow.
内容的提问来源于stack exchange,提问作者Anna

