Skip to content

Commit cbdeeb9

Browse files
committed
feat: initial commit
0 parents  commit cbdeeb9

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
3+
# ide
4+
.idea

Dockerfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM amazon/aws-cli
2+
3+
# Move files in for deployment & cleanup
4+
COPY deploy.sh /deploy.sh
5+
COPY cleanup.sh /cleanup.sh
6+
7+
# Get tools needed for packaging
8+
RUN yum update -y \
9+
&& yum install -y zip unzip jq \
10+
&& yum clean all

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# AWS CodeDeploy Action
2+
To automatically deploy applications to EC2 via CodeDeploy.
3+
4+
---
5+
6+
## Usage
7+
8+
---
9+
### Install as Local Action
10+
For quicker troubleshooting cycles, the action can be copied directly into another project. This way, changes to the action and it's usage can happen simultaneously, in one commit.
11+
12+
1. Copy this repository into your other project as `.github/actions/aws-codedeploy-action`. Be careful: simply cloning in place will likely install it as a submodule--make sure to copy the files without `.git`
13+
2. In your other project's workflow, in the action step, set\
14+
`uses: ./.github/actions/aws-codedeploy-action`

action.yaml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: 'AWS CodeDeploy'
2+
description: 'Deploy projects to EC2 via CodeDeploy'
3+
inputs:
4+
aws_access_key:
5+
description: 'AWS Access Key'
6+
required: true
7+
aws_secret_key:
8+
description: 'AWS Secret Key'
9+
required: true
10+
aws_region:
11+
description: 'AWS Region'
12+
required: false
13+
default: 'us-east-1'
14+
s3_bucket:
15+
description: 'S3 Bucket for CodeDeploy Assets'
16+
required: true
17+
s3_prefix:
18+
description: 'S3 Prefix for ZIP. Defaults to $BUILD#-$BUILDSHAHASH.zip'
19+
required: false
20+
default: ''
21+
s3_folder:
22+
description: 'S3 Folder for ZIP.'
23+
required: true
24+
directory:
25+
description: 'Directory to be archived instead of entire workspace.'
26+
required: false
27+
excluded_files:
28+
description: 'Files to be excluded during archiving (space delimited).'
29+
required: false
30+
codedeploy_name:
31+
description: 'AWS CodeDeploy Application Name'
32+
required: true
33+
codedeploy_group:
34+
description: 'AWS CodeDeploy Application Group'
35+
required: true
36+
runs:
37+
using: 'docker'
38+
image: 'Dockerfile'
39+
entrypoint: '/deploy.sh'
40+
post-entrypoint: '/cleanup.sh'

cleanup.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh -l
2+
3+
# Empty secure environments
4+
export AWS_ACCESS_KEY_ID=''
5+
export AWS_SECRET_ACCESS_KEY=''
6+
export AWS_DEFAULT_REGION=''

deploy.sh

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash -l
2+
set -e
3+
MAX_POLLING_ITERATIONS=60
4+
5+
# 1) Load our permissions in for aws-cli
6+
export AWS_ACCESS_KEY_ID=$INPUT_AWS_ACCESS_KEY
7+
export AWS_SECRET_ACCESS_KEY=$INPUT_AWS_SECRET_KEY
8+
export AWS_DEFAULT_REGION=$INPUT_AWS_REGION
9+
10+
# 2) Zip up the package
11+
DIR_TO_ZIP="./$INPUT_DIRECTORY"
12+
if [ ! -f "$DIR_TO_ZIP/appspec.yml" ]; then
13+
echo "::error::appspec.yml was not located at: $DIR_TO_ZIP"
14+
exit 1;
15+
fi
16+
17+
ZIP_FILENAME=$GITHUB_RUN_ID-$GITHUB_SHA.zip
18+
EXCLUDED_FILES_COMMAND=$(sed -En "s/ /-x/p")
19+
20+
zip -r --quiet "$ZIP_FILENAME" "$DIR_TO_ZIP" -x "$EXCLUDED_FILES_COMMAND" -x \*.git\*
21+
if [ ! -f "$ZIP_FILENAME" ]; then
22+
echo "::error::$ZIP_FILENAME was not generated properly (zip generation failed)."
23+
exit 1;
24+
fi
25+
26+
if [ "$(unzip -l "$ZIP_FILENAME" | grep -q appspec.yml)" = "0" ]; then
27+
echo "::error::$ZIP_FILENAME was not generated properly (missing appspec.yml)."
28+
exit 1;
29+
fi
30+
31+
# 3) Upload the deployment to S3, drop old archive.
32+
function getArchiveETag() {
33+
aws s3api head-object --bucket "$INPUT_S3_BUCKET" \
34+
--key "$INPUT_S3_FOLDER"/"$ZIP_FILENAME" \
35+
--query ETag --output text
36+
}
37+
38+
aws s3 cp "$ZIP_FILENAME" s3://"$INPUT_S3_BUCKET"/"$INPUT_S3_FOLDER"/"$ZIP_FILENAME" > /dev/null 2>&1
39+
ZIP_ETAG=$(getArchiveETag)
40+
41+
rm "$ZIP_FILENAME"
42+
43+
# 4) Start the CodeDeploy
44+
function getActiveDeployments() {
45+
aws deploy list-deployments --application-name "$INPUT_CODEDEPLOY_NAME" \
46+
--deployment-group-name "$INPUT_CODEDEPLOY_GROUP" \
47+
--include-only-statuses "Queued" "InProgress" "Stopped" | jq -r '.deployments';
48+
}
49+
50+
function pollForActiveDeployments() {
51+
deadlockCounter=0;
52+
while [ "$(getActiveDeployments)" != "[]" ]; do
53+
echo "::warning::Deployment in progress. Sleeping 15 seconds. (Try $((++deadlockCounter)))";
54+
55+
if [ "$deadlockCounter" -gt "$MAX_POLLING_ITERATIONS" ]; then
56+
echo "::error:: Max polling iterations reached (15min)."
57+
exit 1;
58+
fi
59+
sleep 15s;
60+
done;
61+
}
62+
pollForActiveDeployments
63+
64+
# 5) Poll / Complete
65+
function deployRevision() {
66+
aws deploy create-deployment \
67+
--application-name "$INPUT_CODEDEPLOY_NAME" \
68+
--deployment-group-name "$INPUT_CODEDEPLOY_GROUP" \
69+
--description "$GITHUB_REF - $GITHUB_SHA" \
70+
--s3-location bucket="$INPUT_S3_BUCKET",bundleType=zip,eTag="$ZIP_ETAG",key="$INPUT_S3_FOLDER"/"$ZIP_FILENAME" > /dev/null 2>&1
71+
}
72+
73+
echo "Deploying to $INPUT_CODEDEPLOY_GROUP.";
74+
deployRevision
75+
76+
sleep 10;
77+
pollForActiveDeployments
78+
echo "Deployed to $INPUT_CODEDEPLOY_GROUP!";
79+
exit 0;

0 commit comments

Comments
 (0)