From 6621ce242385e62710174869309fc1878737cb38 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Wed, 16 Feb 2022 23:36:31 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E5=A4=9A=E6=9E=B6=E6=9E=84=E9=95=9C?= =?UTF-8?q?=E5=83=8F=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build-push.yml | 113 +++++++++++++++++++------------ Dockerfile | 19 ++---- Jenkinsfile | 8 +-- 3 files changed, 80 insertions(+), 60 deletions(-) diff --git a/.github/workflows/build-push.yml b/.github/workflows/build-push.yml index bad86b0985..2eca08df4f 100644 --- a/.github/workflows/build-push.yml +++ b/.github/workflows/build-push.yml @@ -4,56 +4,85 @@ on: push: branches: - master - - v1* + - v1* + - test-build workflow_dispatch: - + jobs: build_push: runs-on: ubuntu-latest name: Build Docker Image and Push steps: - - uses: actions/checkout@v2 - with: - submodules: true - token: ${{ secrets.ACCESS_TOKEN }} + - uses: actions/checkout@v2 + with: + submodules: true + token: ${{ secrets.ACCESS_TOKEN }} - - name: Inject slug/short variables - uses: rlespinasse/github-slug-action@v3.x - - name: Cache node modules - uses: actions/cache@v2 - env: - cache-name: cache-node-modules - with: - path: ~/.npm - key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-build-${{ env.cache-name }}- - ${{ runner.os }}-build- - ${{ runner.os }}- + - name: Inject slug/short variables + uses: rlespinasse/github-slug-action@v3.x + - name: Cache node modules + uses: actions/cache@v2 + env: + cache-name: cache-node-modules + with: + path: ~/.npm + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- - - name: Cache local Maven repository - uses: actions/cache@v2 - with: - path: ~/.m2/repository - key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} - restore-keys: | - ${{ runner.os }}-maven- + - name: Cache local Maven repository + uses: actions/cache@v2 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- - - name: Set up JDK 1.8 - uses: actions/setup-java@v1 - with: - java-version: 1.8 - server-id: github # Value of the distributionManagement/repository/id field of the pom.xml - settings-path: ${{ github.workspace }} # location for the settings.xml file + - name: Set up JDK 1.8 + uses: actions/setup-java@v1 + with: + java-version: 1.8 + server-id: github # Value of the distributionManagement/repository/id field of the pom.xml + settings-path: ${{ github.workspace }} # location for the settings.xml file - - name: Build with Maven - run: mvn -B package --file pom.xml + - name: Build with Maven + run: mvn -B package --file pom.xml - - name: Push to Docker Hub - uses: docker/build-push-action@v1 - with: - username: metersphere - password: ${{ secrets.DOCKER_HUB_TOKEN }} - repository: metersphere/metersphere - tags: ${{ env.GITHUB_REF_SLUG }} - build_args: MS_VERSION=${{ env.GITHUB_REF_SLUG }}-${{ env.GITHUB_SHA_SHORT }} + - name: Unzip jar + run: mkdir -p backend/target/dependency && (cd backend/target/dependency; jar -xf ../*.jar) + + - name: Prepare + id: prepare + run: | + DOCKER_IMAGE=metersphere/metersphere + DOCKER_PLATFORMS=linux/amd64,linux/arm64 + TAG_NAME=${{ env.GITHUB_REF_SLUG }} + DOCKER_IMAGE_TAGS="--tag ${DOCKER_IMAGE}:${TAG_NAME} --tag ${DOCKER_IMAGE}:latest" + echo ::set-output name=docker_image::${DOCKER_IMAGE} + echo ::set-output name=version::${TAG_NAME} + echo ::set-output name=buildx_args::--platform ${DOCKER_PLATFORMS} \ + --build-arg VERSION=${TAG_NAME} \ + --build-arg MS_VERSION=${{ env.GITHUB_REF_SLUG }}-${{ env.GITHUB_SHA_SHORT }} \ + --build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \ + --build-arg VCS_REF=${GITHUB_SHA::8} \ + ${DOCKER_IMAGE_TAGS} . + + - name: Set up Docker Buildx + uses: crazy-max/ghaction-docker-buildx@v3 + + - name: Docker Buildx (build) + run: | + docker buildx build --output "type=image,push=false" ${{ steps.prepare.outputs.buildx_args }} + + - name: Login to Registry + uses: docker/login-action@v1 + with: + username: metersphere + password: ${{ secrets.DOCKER_HUB_TOKEN }} + + - name: Docker Buildx (push) + run: | + docker buildx build --output "type=image,push=true" ${{ steps.prepare.outputs.buildx_args }} + diff --git a/Dockerfile b/Dockerfile index 2535600ba6..ecba6c5892 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,19 @@ -FROM openjdk:8-jdk-alpine as build -WORKDIR /workspace/app - -COPY backend/target/*.jar . - -RUN mkdir -p dependency && (cd dependency; jar -xf ../*.jar) - FROM metersphere/fabric8-java-alpine-openjdk8-jre LABEL maintainer="FIT2CLOUD " -ARG MS_VERSION=dev -ARG DEPENDENCY=/workspace/app/dependency +ARG MS_VERSION=v1.18 +ARG DEPENDENCY=backend/target/dependency -COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib -COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF -COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app +COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib +COPY ${DEPENDENCY}/META-INF /app/META-INF +COPY ${DEPENDENCY}/BOOT-INF/classes /app RUN mv /app/lib/ms-jmeter-core-*.jar /app/lib/ms-jmeter-core.jar RUN mv /app/jmeter /opt/ RUN mkdir -p /opt/jmeter/lib/junit -ENV JAVA_CLASSPATH=/app:/app/lib/ms-jmeter-core.jar:/app/lib/* +ENV JAVA_CLASSPATH=/app:/app/lib/ms-jmeter-core-*.jar:/app/lib/* ENV JAVA_MAIN_CLASS=io.metersphere.Application ENV AB_OFF=true ENV MS_VERSION=${MS_VERSION} diff --git a/Jenkinsfile b/Jenkinsfile index dc77be8b8f..e9fda0ea09 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,7 +1,7 @@ pipeline { agent { node { - label 'metersphere' + label 'metersphere-buildx' } } options { quietPeriod(600) } @@ -17,15 +17,13 @@ pipeline { // sh "yarn install" // sh "cd .." sh "./mvnw clean package --settings ./settings.xml" + sh "mkdir -p backend/target/dependency && (cd backend/target/dependency; jar -xf ../*.jar)" } } } stage('Docker build & push') { steps { - sh "docker build --build-arg MS_VERSION=\${TAG_NAME:-\$BRANCH_NAME}-\${GIT_COMMIT:0:8} -t ${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME} ." - sh "docker tag ${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME} ${IMAGE_PREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" - sh "docker push ${IMAGE_PREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" - sh "docker rmi ${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME} ${IMAGE_PREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME}" + sh "docker buildx build --build-arg MS_VERSION=\${TAG_NAME:-\$BRANCH_NAME}-\${GIT_COMMIT:0:8} -t ${IMAGE_PREFIX}/${IMAGE_NAME}:\${TAG_NAME:-\$BRANCH_NAME} --platform linux/amd64,linux/arm64 . --push" } } }