Set up continuous deployment from Git
With continuous deployment, AWS Amplify builds and deploys your app when you push to your Git repository or open a pull request. You don’t run a deploy command for each change, and each environment stays current with the branches that it tracks. You connect the repository once for the app. Each environment then has its own settings for which branches it deploys and which events start a deployment.
Connect a repository
You can connect a repository when you create an app in the console. For the console steps, see Create and deploy an app. To connect a repository to an existing app with the CLI, run the following command. Replace the URL with the HTTPS URL of your repository, and main with your production branch.
aws amplifygen3 app edit --git https://github.com/my-org/my-app --git-branch main
The command takes the following options:
-
--gitis the repository URL. Pass.to use the remote of the Git repository in the current directory. The CLI converts an SSH URL to its HTTPS form. -
--git-branchis the production branch. It must be a single branch name, not a pattern. If you omit it, Amplify keeps the production branch that the app already uses for the same repository. For a different repository, it uses the repository’s default branch, ormainif it can’t read the default branch. -
--git-owneris the connected GitLab account that owns the repository. It applies only to GitLab repositories, and only together with--git.
To create an app that is connected from the start, run aws amplifygen3 create --git <repository-url>.
If Amplify can’t access the repository, the command fails and lists the steps that give Amplify access to your GitHub or GitLab account.
Connecting a repository also sets the branches of the two built-in environments. The production environment deploys the production branch, and the preview environment deploys every other branch. The output looks like the following:
✓ App updated: - git: https://github.com/my-org/my-app - branch: main ✓ Environment branches updated: - production: main - preview: every branch except main
To disconnect the repository, run aws amplifygen3 app edit --git "". Pushes and pull requests no longer start deployments, every environment stops tracking branches, and you deploy from local files instead. For more information, see Deploy without Git.
Choose the branches an environment deploys
Each environment has a list of branch patterns. A push to a branch that matches one of the patterns can start a deployment to that environment. To set the list, use --git-branch with aws amplifygen3 env edit. Repeat the option for each pattern. The following example sets the staging environment to deploy main and every branch that starts with release/:
aws amplifygen3 env edit --env staging --git-branch main --git-branch 'release/*' --deploy-on-push enable
The output looks like the following:
✓ Updated environment 'staging' in app 'my-app' • git-branch: main, release/* • deploy-on-push: enable
A pattern can take the following forms:
| Pattern | Matches |
|---|---|
|
|
The branch named |
|
|
Every branch whose name starts with |
|
|
Every branch. |
|
|
Excludes |
A pattern can contain letters, numbers, hyphens (-), underscores (_), dots (.), and slashes (/).
Keep the following in mind when you change the list:
-
The patterns that you pass replace the current list. They aren’t added to it. To add a branch, pass the current patterns and the new one.
-
An environment can have up to 50 patterns, and each pattern can be up to 256 characters long.
-
Put quotation marks around a pattern that contains
*or!, so that your shell passes it to the CLI unchanged. -
To stop an environment from deploying any branch, pass
--git-branch "". You can’t combine it with the--deploy-on-*options. -
To track branches when you create a custom environment, pass
--git-branchtoaws amplifygen3 env create.
Connect the app to a repository before you set branches. Otherwise the command fails and changes nothing.
Choose the events that start a deployment
Each environment has three settings that choose which Git events start a deployment. Set each one to enable or disable with aws amplifygen3 env edit. An environment must track at least one branch before you can enable any of them.
| Option | What it does |
|---|---|
|
|
A push to a branch that the environment tracks starts a deployment. |
|
|
A pull request from a branch in your repository starts a deployment when the environment tracks the pull request’s source branch, the branch that it merges from. The branch that the pull request targets doesn’t matter. For more information, see Pull requests. |
|
|
A pull request from a fork of your repository can start a deployment. This setting works only when |
When you change one of these settings, the other two keep their current values.
When you connect a repository, the built-in environments start with the following settings:
| Environment | Branches | Push | Pull request | Fork pull request |
|---|---|---|---|---|
|
|
The production branch |
Enabled |
Disabled |
Disabled |
|
|
Every branch except the production branch |
Enabled |
Enabled |
Enabled for an app without a backend. Disabled for an app with a backend. |
A custom environment that you create with --git-branch deploys on push only. To change these settings, use aws amplifygen3 env edit.
When the preview environment has both push and pull request deployments enabled, a commit to a branch with an open pull request can start two deployments of the same commit. To build each commit once, disable one of the two settings.
To view an environment’s branches and settings, run aws amplifygen3 env get --env <environment> --output json. The branches are under SourceConfig.GitSource.BranchMatchPatterns, and the enabled events are under SourceConfig.GitSource.DeploymentTriggerEvents.
Skip a deployment
To push a commit without deploying it, put one of the following in the commit message: [skip ci], [ci skip], [no ci], [skip amplify], or [amplify skip]. The match is not case-sensitive. This applies to pushes and to pull requests from your own repository. It doesn’t apply to pull requests from forks.
Security of pull requests from forks
Anyone who can fork your repository can open a pull request against it. When Amplify deploys that pull request, it builds and runs code that you didn’t write, in your environment, with that environment’s IAM role and its environment variables and secrets.
Authorizing a commit lets its code run with the permissions and secrets of the environment. Before you authorize a commit, review all of its changes, including build scripts and dependency files. If you don’t accept code from forks, disable the setting on each environment that has it enabled. For example:
aws amplifygen3 env edit --env preview --deploy-on-fork-pull-request disable
Amplify limits this risk in the following ways:
-
A pull request from a fork never builds on its own. Each commit waits until you authorize it with
aws amplifygen3 deployment authorize, and each new commit needs a new authorization. For the steps, see Pull requests. -
An app with a backend never deploys a pull request from a fork, because the code would run against your backend’s databases. You can’t enable
--deploy-on-fork-pull-requestfor such an app. -
The setting is off by default on every environment except the
previewenvironment of an app without a backend.
Pull requests from a fork don’t use the environment’s branch patterns, because the branch is in the fork. Any environment that has both pull request settings enabled can receive them.