使用Jenkins将Docker映像部署到AWS ECS

时间:2020-09-12 07:27:08

标签: dockerfile jenkins-pipeline amazon-cloudformation amazon-ecs

我在GitHub上创建了一个示例项目,我想在每个PR上完成以下工作:

  1. 构建React应用程序并使其在Docker容器中运行
  2. 使用管道构建Docker映像
  3. 使用管道,将映像推送到公司Docker Hub
  4. 使用管道,从公司Docker Hub中获取Docker映像并将其部署到AWS Elastic Container Service中
  5. 允许React应用程序在端口上侦听,它不必向公众开放,如果您使用公司VPN,就可以访问它

我遵循了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")
}

0 个答案:

没有答案