Skip to content

Commit

Permalink
Deploying web app with Jenkins
Browse files Browse the repository at this point in the history
  • Loading branch information
hoaftq committed May 6, 2024
1 parent 63d3ed1 commit 2bed3f7
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,49 @@ pipeline {
}
}
}
stage('Create deployment package') {
stage('Create UI package') {
steps {
dir('holiday-calendar-web') {
bat 'xcopy /Y "deployment\\Procfile" dist'
zip zipFile: 'dist/holiday-calendar-web.zip', dir: 'dist'
zip zipFile: 'dist/holiday-calendar-web.zip', dir: 'dist', overwrite: true
}
}
}
stage('Upload UI package') {
steps {
dir('holiday-calendar-web') {
withAWS(credentials: 'AWS Credentials') {
bat 'aws s3 cp dist/holiday-calendar-web.zip s3://elasticbeanstalk-holiday-calendar-ap-southeast-1'
}
}
}
}
stage('Create UI Elastic Beanstalk environment') {
steps {
dir('holiday-calendar-web/deployment') {
withAWS(credentials: 'AWS Credentials', region: 'ap-southeast-1') {
script {
webStackName = 'holiday-calendar-web'
if (isStackExisting(webStackName)) {
echo "Elastic Beanstalk stack ${webStackName} already exists. Deploying new source code."
deployNewCode()
} else {
echo 'Create new Elastic Beanstalk stack.'
createWebStack(webStackName)
}
}
}
}
}
}
}
}

def getApiUrl() {
withAWS(credentials: 'AWS Credentials') {
withAWS(credentials: 'AWS Credentials', region: 'ap-southeast-1') {
script = '''
@echo off
aws cloudformation describe-stacks ^
--region ap-southeast-1 ^
--stack-name holiday-calendar-lambda ^
--query "Stacks[0].Outputs[?OutputKey==\'HolidayCalendarApi\'].OutputValue" ^
--output text
Expand All @@ -73,3 +99,41 @@ def replaceApiUrl(apiUrl) {
echo "Override environment.ts with: ${newContent}"
writeFile(file: environmentFile, text: newContent)
}

def isStackExisting(stackName) {
script = """
@echo off
aws cloudformation list-stacks ^
--query \"StackSummaries[?StackName == '${stackName}' && StackStatus == 'CREATE_COMPLETE']\"
"""
output = bat(script: script, returnStdout: true)
return output.contains(stackName)
}

def createWebStack(webStackName) {
bat """
aws cloudformation create-stack ^
--stack-name ${webStackName} ^
--template-body file://template.yaml ^
--capabilities CAPABILITY_NAMED_IAM
"""
}

def deployNewCode() {
version = createNewVersion()
bat """
aws elasticbeanstalk create-application-version ^
--application-name holiday-calendar-web ^
--version-label ${version} ^
--source-bundle S3Bucket=elasticbeanstalk-holiday-calendar-ap-southeast-1,S3Key=holiday-calendar-web.zip
"""
bat """
aws elasticbeanstalk update-environment ^
--environment-name holiday-calendar-web ^
--version-label ${version}
"""
}

def createNewVersion() {
new Date().format('yyyyMMdd-HHmmss-SSS')
}

0 comments on commit 2bed3f7

Please sign in to comment.