- Creating a new environment when a branch is created or a pull request is opened.
- Rebuilding the environment when new code is pushed to GitHub.
- Deleting the environment when a pull request is merged.
Before you begin
To manage source integrations, you need to be a project admin. You also need a GitHub repository with working code.1. Generate a token
To integrate your Upsun project with an existing GitHub repository, you need to generate a new token. You can generate a classic personal access token, or a fine-grained personal access token for even greater control over the permissions you grant. For the integration to work, your GitHub user needs to have permission to push code to the repository. When you set up or update an integration, it also needs permission to manage its webhooks. This means your user needs to be a repository admin to create the integration. You can remove this permission after setup. Make sure you give your token a description. If you’re generating a classic personal access token, ensure the token has the appropriate scopes based on what you want to do:| Scope | Purpose |
|---|---|
admin:repo_hook | To create webhooks for events in repositories. Always needed. |
public_repo | To integrate with public repositories. |
repo | To integrate with your private repositories. |
repo and read:org | To integrate with private repositories in organizations you belong to. |
| Permission | Access level |
|---|---|
Commit statuses | Read and write |
Contents | Read and write |
Metadata | Read-only |
Pull request | Read and write |
Webhooks | Read and write |
2. Enable the integration
To enable the integration, use either the CLI or the Console.- Using the CLI
- In the Console
Run the following command:
PROJECT_IDis the ID of your Upsun project.OWNER/REPOSITORYis the name of your repository in GitHub.GITHUB_ACCESS_TOKENis the token you generated.GITHUB_URLis the base URL for your GitHub server if you self-host. If you use the publichttps://github.com, omit the--base-urlflag when running the command.
https://github.com/platformsh/platformsh-docs,
the command is similar to the following:| CLI flag | Default | Description |
|---|---|---|
fetch-branches | true | Whether to mirror and update branches on Upsun and create inactive environments from them. When enabled, merging on an Upsun environment isn’t possible. That is, merging environments must be done on the source repository rather than on the Upsun project. See note below for details related to this flag and synchronizing code from a parent environment. |
prune-branches | true | Whether to delete branches from Upsun that don’t exist in the GitHub repository. When enabled, branching (creating environments) must be done on the source repository rather than on the Upsun project. Branches created on Upsun that are not on the source repository will not persist and will be quickly pruned. Automatically disabled when fetching branches is disabled. |
build-pull-requests | true | Whether to track all pull requests and create active environments from them, which builds the pull request. |
build-draft-pull-requests | true | Whether to also track and build draft pull requests. Automatically disabled when pull requests aren’t built. |
pull-requests-clone-parent-data | true | Whether to clone data from the parent environment when creating a pull request environment. |
build-pull-requests-post-merge | false | Whether to build what would be the result of merging each pull request. Turning it on forces rebuilds any time something is merged to the target branch. |
resources-init | false | To specify a resource initialization strategy for new containers. Once set, the strategy applies to all the deployments you launch through your source integration. See more information on available resource initialization strategies. |
fetch-branches and prune-branches options.
3. Validate the integration
Verify that your integration is functioning properly using the CLI:Add the webhook manually
If the integration was added with the correct permissions, the necessary webhook is added automatically. If you see a message that the webhook wasn’t added, add one manually. To configure a webhook on a GitHub repository, you need to have Admin user permissions.- Get the webhook URL by running this command:
upsun integration:get --property hook_url. - Copy the returned URL.
- In your GitHub repository, click Settings > Webhooks > Add webhook.
- In the Payload URL field, paste the URL you copied.
- For the content type, select application/json.
- Select Send me everything.
- Click Add webhook.
Environment parent and status
When a branch is created in GitHub, an environment is created in Upsun with the default branch as its parent. It starts as an inactive environment with no data or services. When a pull request is opened in GitHub, an environment is created in Upsun with the pull request’s target branch as its parent. It starts as an active environment with a copy of its parent’s data.Source of truth
When you add an integration, your GitHub repository is considered to be the source of truth for the project. Your Upsun project is only a mirror of that repository and you can only push commits to GitHub. To clone your code, follow these steps:- Using the CLI
- Using Git
Run the following command:
Sync, fetch, and prune
An integration from your source repository to Upsun establishes that:- your source repository is the source of truth, where Git operations occur
- Upsun is a mirror of that repository, provisioning infrastructure according to configuration, and orchestrating environments according to the branch structure of the your source repository repository
fetch-branches (track branches on your source repository) and prune-branches (delete branches that don’t exist on your source repository) by default.
You can change these settings but it is recommended to keep them.
When enabled by default, you are limited by design as to what actions can be performed within the context of an Upsun project with a your source repository integration:
| Action | Observation | Recommendation |
|---|---|---|
| Branch from parent | Running environment:branch with the CLI, or selecting Branch in Console produces a new child environment, but it’s deleted shortly after automatically. | Contribute to the your source repository repository itself by creating a branch and pull request. When the PR has been opened, a new environment will be provisioned for it. |
| Merge in parent | Running environment:merge with the CLI fails locally, and the Merge option in Console is not clickable. | Review and merge pull requests and/or branches on the your source repository repository. |
| Merge into child (sync code) | Running environment:synchronize with the CLI fails locally, and the Sync option in Console won’t allow you to include code in that sync. | Perform the merge locally from a matching branch on your source repository. For example, clone the most recent parent (git pull origin parent-branch), switch to the pull request branch (git checkout ga-staging), and then merge the parent into the current branch (git merge main). |