---
# System prepended metadata

title: GitLab集成Jira

---

# GitLab集成Jira

环境说明：
- GitLab地址
  - https://gitlab.cdneks.com
- Jira地址
  - https://jira.cdneks.com

## 1. Jira安装

官方文档：
- [Install a Jira trial](https://confluence.atlassian.com/adminjiraserver/install-a-jira-trial-1044784650.html)
- [Integrating Jira with Apache using SSL](https://confluence.atlassian.com/adminjiraserver/integrating-jira-with-apache-using-ssl-938847754.html)

### 1.1 下载Jira
[Download Jira Software Server](https://www.atlassian.com/software/jira/download)

选择`Jira Software`，`latest release`, `linux 64bit`

```bash
atlassian-jira-software-8.16.1-x64.bin
```

### 1.2 安装Jira
```bash
chmod a+x atlassian-jira-software-X.X.X-x64.bin

sudo ./atlassian-jira-software-X.X.X-x64.bin
```

- 选择`Express Install` (option 1)
- 安装完成后，Jira web服务监听在8080端口

### 1.3 设置Jira
浏览器打开jira，根据向导设置Jira
- 选择`single node`
- 创建账号
- `generate a license`，这一步需要连外网、申请Jira试用密钥

### 1.4 Jira启用https

1. 安装apache
```bash
yum install httpd mod_ssl
```
2. 准备https证书

可以购买收费的https证书，这里用lets encrypt申请免费的证书：

- [安装certbot](https://certbot.eff.org/lets-encrypt/centosrhel7-apache.html)
- 申请https证书
```bash
certbot certonly --standalone -d $domain
```
- 将`/etc/letsencrypt/live/$domain`下的`cert.pem`,`privkey.pem`, `chain.pem`拷贝到`/etc/httpd/ssl/`目录下

3. 设置apache反向代理

修改`/etc/httpd/conf.d/ssl.conf`

```bash
<VirtualHost *:443>
    ServerName jira.cdneks.com

    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3
    SSLCertificateFile /etc/httpd/ssl/cert.pem
    SSLCertificateKeyFile /etc/httpd/ssl/privkey.pem
    SSLCertificateChainFile /etc/httpd/ssl/chain.pem

    <Files ~ "\.(cgi|shtml|phtml|php3?)$">
        SSLOptions +StdEnvVars
    </Files>
    <Directory "/var/www/cgi-bin">
        SSLOptions +StdEnvVars
    </Directory>

    BrowserMatch "MSIE [2-5]" \
             nokeepalive ssl-unclean-shutdown \
             downgrade-1.0 force-response-1.0

    CustomLog logs/ssl_request_log \
              "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

    # Jira Proxy Configuration:
    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>

    SSLProxyEngine          On
    ProxyRequests           Off
    ProxyPreserveHost       On
    ProxyPass               /       http://127.0.0.1:8080/
    ProxyPassReverse        /       http://127.0.0.1:8080/

</VirtualHost>
```
重启apache：
```bash
systemctl restart httpd
```

4. Jira使用反向代理

- 备份`/opt/atlassian/jira/conf/server.xml`，然后修改该文件
- 将文件中的这段配置复制一段放到上方，然后把这段配置的端口从8080改到8081
- 在上面那段配置下增加https代理配置，注意`proxyName`改成自己的域名

两端配置如下：
```bash
        <Connector port="8080" relaxedPathChars="[]|" relaxedQueryChars="[]|{}^&#x5c;&#x60;&quot;&lt;&gt;"
                   maxThreads="150" minSpareThreads="25" connectionTimeout="20000" enableLookups="false"
                   maxHttpHeaderSize="8192" protocol="HTTP/1.1" useBodyEncodingForURI="true"
                   acceptCount="100" disableUploadTimeout="true" bindOnInit="false"
                   scheme="https" proxyName="jira.cdneks.com" proxyPort="443"/>

        <!-- Relaxing chars because of JRASERVER-67974 -->
        <Connector port="8081" relaxedPathChars="[]|" relaxedQueryChars="[]|{}^&#x5c;&#x60;&quot;&lt;&gt;"
                   maxThreads="150" minSpareThreads="25" connectionTimeout="20000" enableLookups="false"
                   maxHttpHeaderSize="8192" protocol="HTTP/1.1" useBodyEncodingForURI="true"
                   acceptCount="100" disableUploadTimeout="true" bindOnInit="false"/>
```
- 重启jira
```bash
/opt/atlassian/jira/bin/stop-jira.sh

/opt/atlassian/jira/bin/start-jira.sh
```

## 2. GitLab集成Jira

官方文档和视频：

- [Linking GitLab accounts](https://confluence.atlassian.com/adminjiraserver/linking-gitlab-accounts-1027142272.html)
- [GitLab with Jira](https://about.gitlab.com/solutions/jira/)

### 2.1 GitLab创建用户、`group`和`project`
- 创建用户，email等信息与Jira中的用户对应
- 创建一个`test-group`
- 在上面的组中创建一个`test-project`

### 2.2 Jira启用OAuth2

- 管理 > 系统 > OAuth 2.0.
- 新增一个集成
- `服务提供商`选择`自定义`
- 填入集成名称
- `客户ID`和`客户机密`暂时留空，将在下一步设置
- 填写`授权端点`和`令牌端点`，分别是：
```bash
https://gitlab.cdneks.com/oauth/authorize

https://gitlab.cdneks.com/oauth/token
```
- 点击`Generate`生成`重定向URL`，复制以便于下一步操作

### 2.3 GitLab创建Jira Applications

- 用户设置 > Applications,增加新的Application
  - Redirect URI: 填入从Jira拷贝的`重定向URL`
  - Confidential: 选中
  - Scopes: 选择`api`
  - 提交
- 提交后就能得到Application ID和Secret

### 2.4 完成Jira OAuth2设置
- 填入GitLab Application ID、Secret
- 范围选择`api`
- 保存并测试连接

### 2.5 Jira链接GitLab账号
- Jira管理 > 应用程序 > DVCS Accounts.
- 点击`链接账户`
- `Host`选择`GitLab Self-Managed`
- `Host URL`填写`https://gitlab.cdneks.com`
- `Team or User Account`填写GitLab的组名，例如：`test-group`
- 点击`增加`
- 按照向导执行剩下的操作，之后就可以把GitLab的`test-group`和`test-project`与Jira关联起来

## 3. GitLab集成Jira的用法
### 3.1 Jira Issue跟踪、智能提交、开发面板集成

官方文档：
- [Using Smart Commits](https://confluence.atlassian.com/fisheye/using-smart-commits-960155400.html) 

在GitLab 提交信息、合并请求信息中提及Jira Issue ID，可以：
- 在Jira Issue面板中展示关联的GitLab提交、合并请求信息
- 在GitLab提交、合并请求信息中直接打开关联的Jira Issue
- 评论Jira Issue
- 设置Jira Issue状态
- 设置Jira Issue处理时间

### 3.2 GitLab导入Jira Issue

- GitLab Project Settings -> Integrations -> Jira
  - 填入Jira URL、Username、Password
  - 选中Active
  - 保存
- 新建一个空的GitLab Project，进入Issues -> Import Issue -> From Jira
  - 选择Jira Project
  - continue


