amplify-hosting icon indicating copy to clipboard operation
amplify-hosting copied to clipboard

Is there a way to programmatically access the deployment URL or the PR name?

Open jonvanausdeln opened this issue 4 years ago • 9 comments

** Please describe which feature you have a question about? ** Is there an env variable or way to access the app URL?

If not, is there a way to access the PR branch name in a web preview build. For example, https://pr-375.xxxxx.amplifyapp.com/ The only env variable I can see is "USER_BRANCH" which does not give me the 'pr-375` . Also, "AWS_BRANCH" is set to the branch where the PR came from.

** Provide additional details**

jonvanausdeln avatar Mar 31 '20 18:03 jonvanausdeln

@jonvanausdeln Can you clarify here: Is your question specifically related to the PR preview feature, or just in general are you asking is there a way to programmatically access the app URL, or both?

ganipcanot avatar Apr 01 '20 18:04 ganipcanot

Sorry, I just re-read my original post and it's confusing.

We have an app that in the code needs to know it's origin URL (e.g https://master.xxxx.amplifyapp.com", so I inject that at build time.

Ideally, I'd like to get the URL for all branches/PR previews. As of now, for normal branch builds, I hardcode part of the URL, and pull the URL prefix from "AWS_BRANCH". However, this doesn't work for PR previews as the "AWS_BRANCH" for those is not the URL prefix.

jonvanausdeln avatar Apr 01 '20 21:04 jonvanausdeln

@jonvanausdeln For each pull request we will increment the number against that application so for example you will see something like "https://pr-#.appId.amplifyapp.com" where "#" goes from 1-n.

The appId in this case should be the same as the root application.

Does that help?

Ref: image

image

ganipcanot avatar Apr 02 '20 23:04 ganipcanot

Thanks for the appId info, that part makes senses. And yes, I see that the URL prefix matches the pr #. I just don't see a way to know what that is for each given PR preview build.

jonvanausdeln avatar Apr 02 '20 23:04 jonvanausdeln

It should automatically increment by 1 for each new PR. You may also be able to use the aws cli to fetch information programmatically if you wanted:

For example: https://docs.aws.amazon.com/cli/latest/reference/amplify/list-branches.html

someTerminalHostName % aws amplify list-branches --app-id <APP-ID> --region <REGION>
{
    "branches": [
        {
            "branchArn": "arn:aws:amplify:<REGION>:<ACCOUNT-ID>:apps/<APP-ID>/branches/master",
            "branchName": "master",
            "tags": {},
            "stage": "SomeStageNameHere",
            "displayName": "master",
            "enableNotification": false,
            "createTime": 1585679590.136,
            "updateTime": 1585868290.707,
            "environmentVariables": {
                "USER_BRANCH": "master"
            },
            "enableAutoBuild": true,
            "framework": "React - Amplify",
            "activeJobId": "0000000001",
            "totalNumberOfJobs": "0",
            "enableBasicAuth": false,
            "thumbnailUrl": "LongStringHere",
            "ttl": "5",
            "enablePullRequestPreview": true,
            "backendEnvironmentArn": "arn:aws:amplify:<REGION>:<ACCOUNT-ID>:apps/<APP-ID>/backendenvironments/master"
        },
        {
            "branchArn": "arn:aws:amplify:<REGION>:<ACCOUNT-ID>:apps/<APP-ID>/branches/pr-8",
            "branchName": "pr-8",
            "description": "-",
            "tags": {},
            "stage": "PULL_REQUEST",
            "displayName": "pr-8",
            "enableNotification": false,
            "createTime": 1585869975.688,
            "updateTime": 1585870632.529,
            "environmentVariables": {
                "USER_BRANCH": "pri"
            },
            "enableAutoBuild": true,
            "activeJobId": "0000000002",
            "totalNumberOfJobs": "0",
            "enableBasicAuth": false,
            "thumbnailUrl": "LongStringHere",
            "ttl": "5",
            "associatedResources": [
                "arn:aws:cloudformation:<REGION>:<ACCOUNT-ID>:stack/stackName/HexNumbers"
            ],
            "destinationBranch": "master",
            "sourceBranch": "testpr",
            "backendEnvironmentArn": "arn:aws:amplify:<REGION>:<ACCOUNT-ID>:apps/<APP-ID>/backendenvironments/pri"
        }
    ]
}

ganipcanot avatar Apr 03 '20 00:04 ganipcanot

Thanks for the additional information. I'll look, but I don't think that will meet the needs. I don't see how I can have the build script "know" what PR # to use. For example, if there are multiple PR's out at the same time, or if the PR gets updated and the preview builds trigger again.

Even if I stored a PR number that incremented with each build, it could easily get out of sync.

It sounds like I may not be able to get the URL prefix for the given build. Maybe I should make this a feature request?

jonvanausdeln avatar Apr 03 '20 20:04 jonvanausdeln

Thanks for the additional information. I'll look, but I don't think that will meet the needs. I don't see how I can have the build script "know" what PR # to use. For example, if there are multiple PR's out at the same time, or if the PR gets updated and the preview builds trigger again.

Even if I stored a PR number that incremented with each build, it could easily get out of sync.

It sounds like I may not be able to get the URL prefix for the given build. Maybe I should make this a feature request?

I'll +1 this feature request, I need it to build a proper URL to the build status page to send in a webhook during my builds.

@ganipcanot 's suggestion seems like a finicky way to find out which specific # a PR has. I kind of assumed AWS_BRANCH would be the one used in the PR previews, but as you mention, it's not.

A new env variable with the PR display name would be nice, perhaps it would only be set if the build in question is a PR preview?

ankhuve avatar May 05 '20 15:05 ankhuve

Found a workaround: git branch --show-current produces pr-123 which can then be used as needed.

For example, to access only the PR #, you can do something like echo "PR key (like 123) = $(git branch --show-current | cut -d '-' -f2)"

But having Amplify provide an environment variable that holds this value would be much easier.

grimsa avatar Sep 05 '21 12:09 grimsa

Providing a System Defined Environment Variable for Amplify URL will help the application to consume it easily. I still don't see any Env variable in the documentation.

nehemiahj avatar Mar 20 '24 10:03 nehemiahj

@jonvanausdeln @nehemiahj this has been fixed. We have a fix for this feature. There are two new env variables for AWS_PULL_REQUEST_SOURCE_BRANCH and AWS_PULL_REQUEST_DESTINATION_BRANCH that can give you the required information during the build. https://docs.aws.amazon.com/amplify/latest/userguide/environment-variables.html#amplify-console-environment-variables

AWS_BRANCH also now returns the PR branch information.

swaminator avatar Jun 24 '24 18:06 swaminator

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.

github-actions[bot] avatar Jun 24 '24 18:06 github-actions[bot]

This issue has been automatically locked.

github-actions[bot] avatar Jun 24 '24 19:06 github-actions[bot]