github_integration.md 4.2 KB
Newer Older
E
Evan Read 已提交
1 2 3 4
---
type: howto
---

5
# Using GitLab CI/CD with a GitHub repository **(PREMIUM)**
M
Marcel Amirault 已提交
6 7

GitLab CI/CD can be used with **GitHub.com** and **GitHub Enterprise** by
M
Marcia Ramos 已提交
8
creating a [CI/CD project](index.md) to connect your GitHub repository to
M
Marcel Amirault 已提交
9 10
GitLab.

M
Marcia Ramos 已提交
11 12
<i class="fa fa-youtube-play youtube" aria-hidden="true"></i>
Watch a video on [Using GitLab CI/CD pipelines with GitHub repositories](https://www.youtube.com/watch?v=qgl3F2j-1cI).
M
Marcel Amirault 已提交
13

E
Evan Read 已提交
14
NOTE: **Note:**
15
Because of [GitHub limitations](https://gitlab.com/gitlab-org/gitlab/issues/9147),
E
Evan Read 已提交
16 17
[GitHub OAuth](../../integration/github.html#enabling-github-oauth)
cannot be used to authenticate with GitHub as an external CI/CD repository.
M
Marcia Ramos 已提交
18

M
Marcel Amirault 已提交
19 20
## Connect with Personal Access Token

E
Evan Read 已提交
21 22
NOTE: **Note:**
Personal access tokens can only be used to connect GitHub.com
M
Marcel Amirault 已提交
23 24
repositories to GitLab.

E
Evan Read 已提交
25
To perform a one-off authorization with GitHub to grant GitLab access your
M
Marcel Amirault 已提交
26 27
repositories:

E
Evan Read 已提交
28
1. Open <https://github.com/settings/tokens/new> to create a **Personal Access
29
   Token**. This token will be used to access your repository and push commit
M
Marcel Amirault 已提交
30
   statuses to GitHub.
E
Evan Read 已提交
31

32 33 34
   The `repo` and `admin:repo_hook` should be enable to allow GitLab access to
   your project, update commit statuses, and create a web hook to notify
   GitLab of new commits.
M
Marcel Amirault 已提交
35 36 37 38

1. In GitLab create a **CI/CD for external repo** project and select
   **GitHub**.

39
   ![Create project](img/github_omniauth.png)
M
Marcel Amirault 已提交
40 41 42 43 44 45

1. Paste the token into the **Personal access token** field and click **List
   Repositories**. Click **Connect** to select the repository.

1. In GitHub, add a `.gitlab-ci.yml` to [configure GitLab CI/CD](../quick_start/README.md).

E
Evan Read 已提交
46 47 48
GitLab will:

1. Import the project.
49
1. Enable [Pull Mirroring](../../user/project/repository/repository_mirroring.md#pulling-from-a-remote-repository-starter)
E
Evan Read 已提交
50 51
1. Enable [GitHub project integration](../../user/project/integrations/github.md)
1. Create a web hook on GitHub to notify GitLab of new commits.
M
Marcel Amirault 已提交
52 53 54

## Connect manually

M
Marcia Ramos 已提交
55
NOTE: **Note:**
E
Evan Read 已提交
56
To use **GitHub Enterprise** with **GitLab.com**, use this method.
M
Marcia Ramos 已提交
57

E
Evan Read 已提交
58
To manually enable GitLab CI/CD for your repository:
M
Marcel Amirault 已提交
59

E
Evan Read 已提交
60
1. In GitHub open <https://github.com/settings/tokens/new> create a **Personal
M
Marcel Amirault 已提交
61 62
   Access Token.** GitLab will use this token to access your repository and
   push commit statuses.
E
Evan Read 已提交
63

64
   Enter a **Token description** and update the scope to allow:
M
Marcel Amirault 已提交
65

66
   `repo` so that GitLab can access your project and update commit statuses
M
Marcel Amirault 已提交
67 68 69 70 71

1. In GitLab create a **CI/CD project** using the Git URL option and the HTTPS
   URL for your GitHub repository. If your project is private, use the personal
   access token you just created for authentication.

72
   GitLab will automatically configure polling-based pull mirroring.
M
Marcel Amirault 已提交
73

74
1. Still in GitLab, enable the [GitHub project integration](../../user/project/integrations/github.md)
M
Marcel Amirault 已提交
75 76
   from **Settings > Integrations.**

77 78
   Check the **Active** checkbox to enable the integration, paste your
   personal access token and HTTPS repository URL into the form, and **Save.**
M
Marcel Amirault 已提交
79 80 81 82 83 84 85

1. Still in GitLab create a **Personal Access Token** with `API` scope to
   authenticate the GitHub web hook notifying GitLab of new commits.

1. In GitHub from **Settings > Webhooks** create a web hook to notify GitLab of
   new commits.

86 87 88
   The web hook URL should be set to the GitLab API to
   [trigger pull mirroring](../../api/projects.md#start-the-pull-mirroring-process-for-a-project-starter),
   using the GitLab personal access token we just created.
M
Marcel Amirault 已提交
89

90 91 92
   ```
   https://gitlab.com/api/v4/projects/<NAMESPACE>%2F<PROJECT>/mirror/pull?private_token=<PERSONAL_ACCESS_TOKEN>
   ```
M
Marcel Amirault 已提交
93

94
   ![Create web hook](img/github_push_webhook.png)
M
Marcel Amirault 已提交
95 96

1. In GitHub add a `.gitlab-ci.yml` to configure GitLab CI/CD.
E
Evan Read 已提交
97 98 99 100 101 102 103 104 105 106 107 108

<!-- ## Troubleshooting

Include any troubleshooting steps that you can foresee. If you know beforehand what issues
one might have when setting this up, or when something is changed, or on upgrading, it's
important to describe those, too. Think of things that may go wrong and include them here.
This is important to minimize requests for support, and to avoid doc comments with
questions that you know someone might ask.

Each scenario can be a third-level heading, e.g. `### Getting error message X`.
If you have none to add when creating a doc, leave this section in place
but commented out to help encourage others to add to it in the future. -->