Skip to content

Commit

Permalink
Merge pull request #2 from duckbytes/get_env_name
Browse files Browse the repository at this point in the history
Get graphql api and backend env name
  • Loading branch information
duckbytes authored Apr 10, 2024
2 parents 08a6b72 + 57e118f commit 8341773
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Container image that runs your code
FROM alpine:3.16
FROM alpine:latest

RUN apk add aws-cli jq

Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Example:

```
- name: Wait for Amplify to finish remote build
uses: duckbytes/amplify-build-status@v1.1
uses: duckbytes/amplify-build-status@v2
with:
app-id: ${{ secrets.AMPLIFY_APP_ID }}
branch-name: ${{ github.ref_name }}
Expand All @@ -31,7 +31,7 @@ Click on *Edit backend* and copy the appId value.

### Inputs

`app-id`, `branch-name` and `commit-id` are all required input. You also must set `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_REGION` in your environnment variables.
`app-id`, `branch-name` and `commit-id` are all required input. You also must set `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_REGION` in your environment variables.

Other inputs are:

Expand All @@ -41,8 +41,20 @@ Other inputs are:

### Outputs
- `status` # The build status output according to the AWS CLI.
- `backend_environment` # The environment name.
- `graphql_endpoint` # The GraphQL endpoint.

You can access these values later in your workflow like this:

`${{ steps.amplify_status.outputs.environment_name }}`

## Known issues

Sometimes the Amplify console doesn't automatically associate a backend with a new deployment.

Amplify will successfully complete the new build, but it is necessary click `(Edit)` next to "Continuous deploys set up" and set it to the correct backend before the action can return the environment name and GraphQL endpoint.

**only on V1**

If you are connecting a branch to Amplify for the first time, the commit-id may be `HEAD` instead of the commit sha.
Any subsequent bulds triggered by commits will use the actual commit sha.
Any subsequent builds triggered by commits will use the actual commit sha.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ inputs:
outputs:
status:
description: 'The result of the build.'
backend_environment:
description: 'The environment name'
graphql_endpoint:
description: 'The GraphQL endpoint.'
runs:
using: 'docker'
image: 'Dockerfile'
Expand Down
68 changes: 61 additions & 7 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh -l

set -e

APP_ID=$1
BRANCH_NAME=$2
COMMIT_ID=$3
Expand Down Expand Up @@ -43,13 +45,65 @@ if [[ $TIMEOUT -lt 0 ]]; then
exit 1
fi

get_backend_env_name () {
local env_name;
local env_arn;
local next_token="";
local list_result;
# get backendEnvironmentArn from get branch first
env_arn=$(aws amplify get-branch --app-id "$APP_ID" --branch-name "$BRANCH_NAME" | jq -r ".branch.backendEnvironmentArn")
# search the list of backend environments for the environment name
while : ; do
list_result=$(aws amplify list-backend-environments --app-id "$APP_ID" --next-token "$next_token")
env_name=$(echo $list_result | jq -r ".backendEnvironments[] | select(.backendEnvironmentArn == \"$env_arn\") | .environmentName")
if [[ -n $env_name ]]; then
env_name=$(echo $env_name | tr -d " \t\n\r")
break
fi
next_token=$(echo $list_result | jq -r ".nextToken")
next_token=$(echo $next_token | tr -d " \t\n\r")
if [[ -z $next_token ]] || [[ $next_token == "null" ]]; then
break
fi
done
exit_status=$?
echo "$env_name"
return $exit_status
}

get_backend_graphql_endpoint () {
local endpoint;
local env_name;
local test;
env_name=$(get_backend_env_name)
echo "Found env name getting graphql endpoint: $env_name" >&2
endpoint=$(aws amplifybackend get-backend --app-id "$APP_ID" --backend-environment-name "$env_name" | jq -r ".AmplifyMetaConfig" | jq -r ".api.platelet.output.GraphQLAPIEndpointOutput")
exit_status=$?
endpoint=$(echo $endpoint | tr -d " \t\n\r")
echo "$endpoint"
return $exit_status
}


write_output () {
local env_name;
local graphql_endpoint;
echo "status=$STATUS" >> $GITHUB_OUTPUT
env_name=$(get_backend_env_name)
graphql_endpoint=$(get_backend_graphql_endpoint)
echo "Found environment name: $env_name"
echo "Found graphql endpoint: $graphql_endpoint"
echo "environment_name=$env_name" >> $GITHUB_OUTPUT
echo "graphql_endpoint=$graphql_endpoint" >> $GITHUB_OUTPUT
}

get_status () {
local status;
status=$(aws amplify list-jobs --app-id "$1" --branch-name "$2" | jq -r ".jobSummaries[] | select(.commitId == \"$3\") | .status")
status=$(aws amplify list-jobs --app-id "$APP_ID" --branch-name "$BRANCH_NAME" | jq -r ".jobSummaries[] | select(.commitId == \"$COMMIT_ID\") | .status")
exit_status=$?
# it seems like sometimes status ends up with a new line in it?
# strip it out
status=$(echo $status | tr '\n' ' ')
status=$(echo $status | tr -d " \t\n\r")
echo "$status"
return $exit_status
}
Expand All @@ -71,11 +125,11 @@ fi

if [[ $STATUS == "SUCCEED" ]]; then
echo "Build Succeeded!"
echo "status=$STATUS" >> $GITHUB_OUTPUT
echo $(write_output)
exit 0
elif [[ $STATUS == "FAILED" ]]; then
echo "Build Failed!"
echo "status=$STATUS" >> $GITHUB_OUTPUT
echo $(write_output)
no_fail_check
fi

Expand Down Expand Up @@ -105,7 +159,7 @@ seconds=$(( $TIMEOUT * 60 ))
count=0

if [[ "$WAIT" == "false" ]]; then
echo "status=$STATUS" >> $GITHUB_OUTPUT
echo $(write_output)
exit 0
elif [[ "$WAIT" == "true" ]]; then
while [[ $STATUS != "SUCCEED" ]]; do
Expand All @@ -121,13 +175,13 @@ elif [[ "$WAIT" == "true" ]]; then
fi
if [[ $STATUS == "FAILED" ]]; then
echo "Build Failed!"
echo "status=$STATUS" >> $GITHUB_OUTPUT
echo $(write_output)
no_fail_check
else
echo "Build in progress... Status: $STATUS"
fi
count=$(( $count + 30 ))
done
echo "Build Succeeded!"
echo "status=$STATUS" >> $GITHUB_OUTPUT
echo $(write_output)
fi

0 comments on commit 8341773

Please sign in to comment.