我在GitHub上创建了一个示例项目,我想在每个PR上完成以下工作:
我遵循了this tutorial,并停留在上面的步骤4。将图像推送到ECS。我需要使用gradle
还是可以在Jenkinsfile
中完成所有这些操作?
这是我到目前为止所拥有的:
Dockerfile
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]
Jenkinsfile
artifactoryHost = "sten-docker.art-bobcat.sten.com"
buildContainerVersion = "1.0"
imageName = "team-docker/minimal-react-webpack-babel-setup"
fullImageName = artifactoryHost + "/" + imageName
versionedImageName = fullImageName + ":" + buildContainerVersion
pipeline {
agent {
node {
label 'build-internal-use-containers'
}
}
stages {
stage('Build') {
steps {
sh "npm install"
}
}
stage('Test') {
steps {
sh "npm test"
}
}
stage('Build Docker image') {
steps {
sh "docker build -t ${versionedImageName} ."
}
}
stage("Push Docker image") {
steps {
script {
docker.withRegistry("https://"+ artifactoryHost,'svc_d_artifactory'){
sh """
docker push ${versionedImageName}
"""
}
}
}
}
stage('Deploy image to AWS') {
steps {
script {
docker.withRegistry(httpsPrefix + artifactoryHost, 'svc_d_artifactory'){
withAWS(credentials: "aws_vcs_dev_vpc", region: "us-east-1") {
sh './gradlew awsCfnMigrateStack awsCfnWaitStackComplete -PsubnetId=$SUBNET_ID -PdockerHubUsername=$DOCKER_HUB_LOGIN_USR -Pregion=$REGION'
}
}
}
}
}
}
}
ecs.yml
AWSTemplateFormatVersion: "2010-09-09"
Description: 'Account/VPC CFN configuration'
Parameters:
SubnetID:
Type: String
ServiceName:
Type: String
ServiceVersion:
Type: String
DockerHubUsername:
Type: String
Resources:
Cluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: deployment-example-cluster
ServiceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: ServiceSecurityGroup
GroupDescription: Security group for service
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 8080
ToPort: 8080
CidrIp: 0.0.0.0/0
TaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: !Sub ${ServiceName}-task
Cpu: 256
Memory: 512
NetworkMode: awsvpc
ContainerDefinitions:
- Name: !Sub ${ServiceName}-container
Image: !Sub ${DockerHubUsername}/${ServiceName}:${ServiceVersion}
PortMappings:
- ContainerPort: 8080
RequiresCompatibilities:
- EC2
- FARGATE
Service:
Type: AWS::ECS::Service
Properties:
ServiceName: !Sub ${ServiceName}-service
Cluster: !Ref Cluster
TaskDefinition: !Ref TaskDefinition
DesiredCount: 1
LaunchType: FARGATE
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: ENABLED
Subnets:
- !Ref SubnetID
SecurityGroups:
- !GetAtt ServiceSecurityGroup.GroupId
build.gradle
apply plugin: 'jp.classmethod.aws.cloudformation'
version = scmVersion.version
cloudFormation {
stackName "$project.name-stack"
stackParams([
SubnetID: findProperty('subnetId') ?: '',
ServiceName: project.name,
ServiceVersion: project.version,
DockerHubUsername: dockerHubUsernameProperty
])
templateFile project.file("ecs.yml")
}