Skip to main content

Configuring prebuilds

You can configure your project to prebuild a codespace automatically each time you push a change to your repository.

People with admin access to a repository can configure prebuilds for the repository.

Codespaces is available for organizations using GitHub Team or GitHub Enterprise Cloud. 詳しい情報については「GitHubの製品」を参照してください。

You can set up a prebuild configuration for a specific branch of your repository.

Any branch created from a prebuild-enabled base branch will typically also get assigned a prebuild during codespace creation. This is true if the dev container on the branch is the same as on the base branch. This is because the majority of the prebuild configuration for branches with the same dev container configuration are identical, so developers can benefit from faster codespace creation times on those branches also. For more information, see "Introduction to dev containers."

Typically, when you configure prebuilds for a branch, prebuilds will be available for Codespaces machine types for that branch. However, if your repository is greater than 32 GB, prebuilds won't be available for 2-core and 4-core machine types, since the storage these provide is limited to 32 GB.

Prebuilds are not available if you choose to use a devcontainer.json file from a .devcontainer/SUBDIRECTORY location when you create a codespace. For information about choosing a devcontainer.json file, see "Creating a codespace."

Prerequisites

Before you can configure prebuilds for your project the following must be true:

Configuring a prebuild

  1. GitHub.comで、リポジトリのメインページにアクセスしてください。

  2. リポジトリ名の下で Settings(設定)をクリックしてください。 リポジトリの設定ボタン

  3. In the "Code & automation" section of the sidebar, click Codespaces.

  4. Under "Prebuild configuration", click Set up prebuild.

    The 'Set up prebuilds' button

  5. Choose the branch for which you want to set up a prebuild.

    The Branch drop-down menu

    Note: Any branches created from a prebuild-enabled base branch will typically also get prebuilds. For example, if you enable prebuilds for the default branch of the repository, branches based on the default branch will, in most cases, also get prebuilds.

  6. Choose how you want to automatically trigger updates of the prebuild template.

    • Every push (the default setting) - With this setting, prebuild configurations will be updated on every push made to the given branch. This will ensure that codespaces generated from a prebuild template always contain the latest codespace configuration, including any recently added or updated dependencies.
    • On configuration change - With this setting, prebuild configurations will be updated every time associated configuration files for a given repo and branch are updated. This ensures that changes to the dev container configuration files for the repository are used when a codespace is generated from a prebuild template. The Actions workflow that updates the prebuild template will run less often, so this option will use fewer Actions minutes. However, this option will not guarantee that codespaces always include recently added or updated dependencies, so these may have to be added or updated manually after a codespace has been created.
    • Scheduled - With this setting, you can have your prebuild configurations update on a custom schedule that's defined by you. This can reduce consumption of Actions minutes, however, with this option, codespaces may be created that do not use the latest dev container configuration changes.

    The prebuild trigger options

  7. Select Reduce prebuild available to only specific regions to limit access to your prebuilt image, then select which regions you want it available in. Developers can only create codespaces from a prebuild if they are located in a region you select. By default, your prebuilt image is available to all regions where codespaces is available and storage costs apply for each region.

    The region selection options

    Notes:

    • The prebuild template for each region will incur individual charges. You should, therefore, only enable prebuilds for regions in which you know they'll be used. For more information, see "About Codespaces prebuilds."
    • Developers can set their default region for Codespaces, which can allow you to enable prebuilds for fewer regions. For more information, see "Setting your default region for Codespaces."
  8. Set the number of prebuild template versions to be retained. You can input any number between 1 and 5. The default number of saved versions is 2, which means that only the latest template version and the previous version are saved.

    Depending on your prebuild trigger settings, your prebuild template could change with each push or on each dev container configuration change. Retaining older versions of prebuild templates enables you to create a prebuild from an older commit with a different dev container configuration than the current prebuild template. Since there is a storage cost associated with retaining prebuild template versions, you can choose the number of versions to be retained based on the needs of your team. For more information on billing, see "About billing for Codespaces."

    If you set the number of prebuild template versions to save to 1, Codespaces will only save the latest version of the prebuild template and will delete the older version each time the template is updated. This means you will not get a prebuilt codespace if you go back to an older dev container configuration.

    The prebuild template history setting

  9. Add users or teams to notify when the prebuild workflow run fails for this configuration. You can begin typing a username, team name, or full name, then click the name once it appears to add them to the list. The users or teams you add will receive an email when prebuild failures occur, containing a link to the workflow run logs to help with further investigation.

    The prebuild failure notification setting

  10. Click Create.

    The prebuild configuration is listed on the Codespaces page of your repository settings. A GitHub Actions workflow is queued and then run to create prebuild templates, based on the branch you selected, in the regions you specified.

    Note: By default, the GitHub Actions workflow for a prebuild configuration can only access resources in its own repository. If your project uses resources from outside of the repository, you'll need to set the CODESPACES_PREBUILD_TOKEN secret to grant the required access. For more information, see "Allowing a prebuild to access external resources."

Configuring access to resources that are not in the repository

By default, the GitHub Actions workflow for a prebuild configuration can only access its own repository contents. If your project needs to access external resources to build the development environment, you will need to set up a personal access token (PAT) with the appropriate access scopes.

For more information, see “Allowing a prebuild to access external resources."

Configuring environment variables

To allow the prebuild process to access environment variables required to create your development environment, you can set these either as Codespaces repository secrets or as Codespaces organization secrets. For more information, see "Adding secrets for a repository" and "Adding secrets for an organization."

Prebuilds do not use any user-level secrets while building your environment, because these are not added until after the codespace has been created.

Codespaces secrets that you create in this way will be accessible by anyone who creates a codespace from this repository. If you do not want this, you can alternatively set the CODESPACES_PREBUILD_TOKEN secret. The CODESPACES_PREBUILD_TOKEN secret is only used for prebuilding and its value is not accessible in users' codespaces. For more information, see “Allowing a prebuild to access external resources."

Configuring time-consuming tasks to be included in the prebuild

You can use the onCreateCommand and updateContentCommand commands in your devcontainer.json to include time-consuming processes as part of the prebuild template creation. For more information, see the Visual Studio Code documentation, "devcontainer.json reference."

onCreateCommand is run only once, when the prebuild template is created, whereas updateContentCommand is run at template creation and at subsequent template updates. Incremental builds should be included in updateContentCommand since they represent the source of your project and need to be included for every prebuild template update.

Further reading