Skip to content

Commit

Permalink
Merge pull request #64 from dsiemensma-m4m/feature/existing_bucket_name
Browse files Browse the repository at this point in the history
Added optional "existing_bucket_name" parameter for using an existing bucket to deploy to. This also prevents the action from creating a storage location.
  • Loading branch information
Einar Egilsson authored Aug 17, 2021
2 parents 1729e92 + de8b37b commit af31173
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
run: zip -r deploy.zip . -x '*.git*'

- name: Deploy to EB
uses: einaregilsson/beanstalk-deploy@v16
uses: einaregilsson/beanstalk-deploy@v18
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Expand All @@ -43,12 +43,12 @@ jobs:
### Deploying an existing version
You can also use the action to deploy an existing version. To do this simply omit the ```deployment-package``` input parameter.
The action will then assume that the version you pass in throught ```version_label``` already exists in Beanstalk and
attempt to deploy that. In the example below the action would attempt do deploy existing version 12345.
The action will then assume that the version you pass in through ```version_label``` already exists in Beanstalk and
attempt to deploy that. In the example below the action would attempt to deploy existing version 12345.

```yaml
- name: Deploy to EB
uses: einaregilsson/beanstalk-deploy@v16
uses: einaregilsson/beanstalk-deploy@v18
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Expand Down Expand Up @@ -85,6 +85,10 @@ triggered the build, `version_description: ${{github.SHA}}`.
`environment_name`: In version 10 this parameter becomes optional. If you don't pass an environment in the action will simply create
the version but not deploy it anywhere.

`existing_bucket_name` *(since v18)*: Use this to provide an existing bucket name to upload your deployment package to.
*It will prevent the action from (re)creating a bucket during deployment as well.*
Omit this parameter to have the action create the bucket. The latter requires the API key used to have the applicable permissions.

### AWS Permissions

It should be enough for your AWS user to have the policies **AWSElasticBeanstalkWebTier** and **AWSElasticBeanstalkManagedUpdatesCustomerRolePolicy** attached
Expand Down Expand Up @@ -119,5 +123,5 @@ everywhere.
1. The S3 upload is a simple PUT request, we don't handle chunked upload. It has worked fine for files that are a
few megabytes in size, if your files are much larger than that it may cause problems.
2. The script does not roll back if a deploy fails.
3. There is no integration with Git, like there is in the official EB cli. This script only takes a readymade zip file and
3. There is no integration with Git, like there is in the official EB cli. This script only takes an already made zip file and
deploys it.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ inputs:
deployment_package:
description: 'Zip file with the version to deploy. If skipped the action will deploy existing version.'
required: false
existing_bucket_name:
description: 'Whether the action should skip creating a new bucket and use the given one to upload the deployment package to instead. When omitted the actions will (try to) create a new one during deployment.'
required: false
use_existing_version_if_available:
description: 'If set to "true" then the action will deploy an existing version with the given version_label if it already exists, but otherwise create the version and deploy it.'
required: false
Expand Down
31 changes: 22 additions & 9 deletions beanstalk-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,29 @@ function expect(status, result, extraErrorMessage) {
}

//Uploads zip file, creates new version and deploys it
function deployNewVersion(application, environmentName, versionLabel, versionDescription, file, waitUntilDeploymentIsFinished, waitForRecoverySeconds) {

//Lots of characters that will mess up an S3 filename, so only allow alphanumeric, - and _ in the actual file name.
function deployNewVersion(application, environmentName, versionLabel, versionDescription, file, bucket, waitUntilDeploymentIsFinished, waitForRecoverySeconds) {
//Lots of characters that will mess up an S3 filename, so only allow alphanumeric, - and _ in the actual file name.
//The version label can still contain all that other stuff though.
let s3filename = versionLabel.replace(/[^a-zA-Z0-9-_]/g, '-');

let s3Key = `/${application}/${s3filename}.zip`;
let bucket, deployStart, fileBuffer;
let deployStart, fileBuffer;

readFile(file).then(result => {
fileBuffer = result;
return createStorageLocation();

if (bucket === null) {
console.log(`No existing bucket name given, creating/requesting storage location`);
return createStorageLocation();
}
}).then(result => {
expect(200, result );
bucket = result.data.CreateStorageLocationResponse.CreateStorageLocationResult.S3Bucket;
if (bucket === null) {
expect(200, result, 'Failed to create storage location');
bucket = result.data.CreateStorageLocationResponse.CreateStorageLocationResult.S3Bucket;
}

console.log(`Uploading file to bucket ${bucket}`);

return checkIfFileExistsInS3(bucket, s3Key);
}).then(result => {
if (result.statusCode === 200) {
Expand Down Expand Up @@ -265,7 +272,8 @@ function main() {
versionLabel,
versionDescription,
region,
file,
file,
existingBucketName = null,
useExistingVersionIfAvailable,
waitForRecoverySeconds = 30,
waitUntilDeploymentIsFinished = true; //Whether or not to wait for the deployment to complete...
Expand All @@ -282,6 +290,10 @@ function main() {
awsApiRequest.sessionToken = strip(process.env.INPUT_AWS_SESSION_TOKEN);
awsApiRequest.region = strip(process.env.INPUT_REGION);

if (process.env.INPUT_EXISTING_BUCKET_NAME) {
existingBucketName = strip(process.env.INPUT_EXISTING_BUCKET_NAME);
}

if ((process.env.INPUT_WAIT_FOR_DEPLOYMENT || '').toLowerCase() == 'false') {
waitUntilDeploymentIsFinished = false;
}
Expand Down Expand Up @@ -339,6 +351,7 @@ function main() {
console.log(' Version description: ' + versionDescription);
console.log(' AWS Region: ' + awsApiRequest.region);
console.log(' File: ' + file);
console.log('Existing bucket Name: ' + existingBucketName);
console.log(' AWS Access Key: ' + awsApiRequest.accessKey.length + ' characters long, starts with ' + awsApiRequest.accessKey.charAt(0));
console.log(' AWS Secret Key: ' + awsApiRequest.secretKey.length + ' characters long, starts with ' + awsApiRequest.secretKey.charAt(0));
console.log(' Wait for deployment: ' + waitUntilDeploymentIsFinished);
Expand Down Expand Up @@ -370,7 +383,7 @@ function main() {
}
} else {
if (file) {
deployNewVersion(application, environmentName, versionLabel, versionDescription, file, waitUntilDeploymentIsFinished, waitForRecoverySeconds);
deployNewVersion(application, environmentName, versionLabel, versionDescription, file, existingBucketName, waitUntilDeploymentIsFinished, waitForRecoverySeconds);
} else {
console.error(`Deployment failed: No deployment package given but version ${versionLabel} doesn't exist, so nothing to deploy!`);
process.exit(2);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "beanstalk-deploy",
"version": "16.0.0",
"version": "18.0.0",
"description": "GitHub Action + command line tool to deploy to AWS Elastic Beanstalk.",
"main": "beanstalk-deploy.js",
"scripts": {
Expand Down

0 comments on commit af31173

Please sign in to comment.