Skip to content

Commit

Permalink
Merge pull request #4 from daltonfury42/master
Browse files Browse the repository at this point in the history
Add auto commenting to PR with the preview url.
  • Loading branch information
yinlinchen authored Sep 14, 2020
2 parents e2079f3 + 45b4f56 commit 0940cb2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ LABEL maintainer="Yinlin Chen <[email protected]>"
ENV AWSCLI_VERSION='1.18.14'

RUN pip install --quiet --no-cache-dir awscli==${AWSCLI_VERSION}
RUN apk --no-cache add curl

ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
ENTRYPOINT ["/entrypoint.sh"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
AmplifyAppId: ${{ secrets.AmplifyAppId }}
BackendEnvARN: ${{ secrets.BackendEnvARN }}
AWS_REGION: 'us-east-1'
Expand All @@ -58,6 +59,7 @@ The following settings must be passed as environment variables as shown in the e
| `AWS_REGION` | The region where you created your bucket. Set to `us-east-1` by default. [Full list of regions here.](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions) | `env` | **Yes** | `us-east-1` |
| `AmplifyAppId` | The unique ID for an Amplify app. For example, `d6a88fjhifqlks` | `secret env` | **Yes** | N/A |
| `BackendEnvARN` | The Amazon Resource Name (ARN) for a backend environment that is part of an Amplify app. | `secret env` | **Yes** | N/A |
| `GITHUB_TOKEN` | The GITHUB_TOKEN, should be supplied if a comment with the preview URL is to be posted on the PR | `github env` | No | N/A |
| `NewBackendEnvARN` | The Amazon Resource Name (ARN) for a backend environment that is part of an Amplify app. | `secret env` | No | N/A |

## Inputs
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ inputs:
amplify_command:
description: 'amplify command: SUPPORTED: [deploy, delete]'
required: true
comments_url:
description: 'Github comments url'
default: "${{ github.event.pull_request.comments_url }}"
required: false

runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.branch_name }}
- ${{ inputs.amplify_command }}
- ${{ inputs.comments_url }}

branding:
icon: 'git-pull-request'
color: 'blue'
29 changes: 20 additions & 9 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

set -e

if [ -z "$AWS_ACCESS_KEY_ID" ] && [ -z "$AWS_SECRET_ACCESS_KEY" ] ; then
BRANCH_NAME=$1
AMPLIFY_COMMAND=$2
COMMENT_URL=$3

if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ] ; then
echo "You must provide the action with both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables in order to deploy"
exit 1
fi
Expand All @@ -21,12 +25,12 @@ if [ -z "$BackendEnvARN" ] ; then
exit 1
fi

if [ -z "$1" ] ; then
if [ -z "$BRANCH_NAME" ] ; then
echo "You must provide branch name input parameter in order to deploy"
exit 1
fi

if [ -z "$2" ] ; then
if [ -z "$AMPLIFY_COMMAND" ] ; then
echo "You must provide amplify_command input parameter in order to deploy"
exit 1
fi
Expand All @@ -38,23 +42,23 @@ ${AWS_REGION}
text
EOF

case $2 in
case $AMPLIFY_COMMAND in

deploy)
sh -c "aws amplify create-branch --app-id=${AmplifyAppId} --branch-name=$1 \
sh -c "aws amplify create-branch --app-id=${AmplifyAppId} --branch-name=$BRANCH_NAME \
--backend-environment-arn=${BackendEnvARN} --region=${AWS_REGION}"

sleep 10

sh -c "aws amplify start-job --app-id=${AmplifyAppId} --branch-name=$1 --job-type=RELEASE --region=${AWS_REGION}"
sh -c "aws amplify start-job --app-id=${AmplifyAppId} --branch-name=$BRANCH_NAME --job-type=RELEASE --region=${AWS_REGION}"
;;

delete)
sh -c "aws amplify delete-branch --app-id=${AmplifyAppId} --branch-name=$1 --region=${AWS_REGION}"
sh -c "aws amplify delete-branch --app-id=${AmplifyAppId} --branch-name=$BRANCH_NAME --region=${AWS_REGION}"
;;

*)
echo "amplify command $2 is invalid or not supported"
echo "amplify command $AMPLIFY_COMMAND is invalid or not supported"
exit 1
;;

Expand All @@ -65,4 +69,11 @@ null
null
null
text
EOF
EOF

if [ -z "$GITHUB_TOKEN" ] ; then
echo "Skipping comment as GITHUB_TOKEN not provided"
else
SUBDOMAIN_NAME=$(echo $BRANCH_NAME | sed 's/[^a-zA-Z0-9-]/-/')
curl -X POST $COMMENT_URL -H "Content-Type: application/json" -H "Authorization: token $GITHUB_TOKEN" --data '{ "body": "'"Preview branch generated at https://$SUBDOMAIN_NAME.${AmplifyAppId}.amplifyapp.com"'" }'
fi

0 comments on commit 0940cb2

Please sign in to comment.