build: 多架构镜像构建

This commit is contained in:
CaptainB 2022-02-16 23:36:31 +08:00 committed by 刘瑞斌
parent d0bdd8fce1
commit c6a8e58700
3 changed files with 79 additions and 59 deletions

View File

@ -4,56 +4,85 @@ on:
push: push:
branches: branches:
- master - master
- v1* - v1*
- test-build
workflow_dispatch: workflow_dispatch:
jobs: jobs:
build_push: build_push:
runs-on: ubuntu-latest runs-on: ubuntu-latest
name: Build Docker Image and Push name: Build Docker Image and Push
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
with: with:
submodules: true submodules: true
token: ${{ secrets.ACCESS_TOKEN }} token: ${{ secrets.ACCESS_TOKEN }}
- name: Inject slug/short variables - name: Inject slug/short variables
uses: rlespinasse/github-slug-action@v3.x uses: rlespinasse/github-slug-action@v3.x
- name: Cache node modules - name: Cache node modules
uses: actions/cache@v2 uses: actions/cache@v2
env: env:
cache-name: cache-node-modules cache-name: cache-node-modules
with: with:
path: ~/.npm path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: | restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}- ${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build- ${{ runner.os }}-build-
${{ runner.os }}- ${{ runner.os }}-
- name: Cache local Maven repository - name: Cache local Maven repository
uses: actions/cache@v2 uses: actions/cache@v2
with: with:
path: ~/.m2/repository path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: | restore-keys: |
${{ runner.os }}-maven- ${{ runner.os }}-maven-
- name: Set up JDK 1.8 - name: Set up JDK 1.8
uses: actions/setup-java@v1 uses: actions/setup-java@v1
with: with:
java-version: 1.8 java-version: 1.8
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
settings-path: ${{ github.workspace }} # location for the settings.xml file settings-path: ${{ github.workspace }} # location for the settings.xml file
- name: Build with Maven - name: Build with Maven
run: mvn -B package --file pom.xml run: mvn -B package --file pom.xml
- name: Push to Docker Hub - name: Unzip jar
uses: docker/build-push-action@v1 run: mkdir -p backend/target/dependency && (cd backend/target/dependency; jar -xf ../*.jar)
with:
username: metersphere - name: Prepare
password: ${{ secrets.DOCKER_HUB_TOKEN }} id: prepare
repository: metersphere/metersphere run: |
tags: ${{ env.GITHUB_REF_SLUG }} DOCKER_IMAGE=metersphere/metersphere
build_args: MS_VERSION=${{ env.GITHUB_REF_SLUG }}-${{ env.GITHUB_SHA_SHORT }} 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 }}

View File

@ -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 FROM metersphere/fabric8-java-alpine-openjdk8-jre
LABEL maintainer="FIT2CLOUD <support@fit2cloud.com>" LABEL maintainer="FIT2CLOUD <support@fit2cloud.com>"
ARG MS_VERSION=dev ARG MS_VERSION=dev
ARG DEPENDENCY=/workspace/app/dependency ARG DEPENDENCY=backend/target/dependency
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app COPY ${DEPENDENCY}/BOOT-INF/classes /app
RUN mv /app/lib/ms-jmeter-core-*.jar /app/lib/ms-jmeter-core.jar RUN mv /app/lib/ms-jmeter-core-*.jar /app/lib/ms-jmeter-core.jar
RUN mv /app/jmeter /opt/ RUN mv /app/jmeter /opt/
RUN mkdir -p /opt/jmeter/lib/junit 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 JAVA_MAIN_CLASS=io.metersphere.Application
ENV AB_OFF=true ENV AB_OFF=true
ENV MS_VERSION=${MS_VERSION} ENV MS_VERSION=${MS_VERSION}

8
Jenkinsfile vendored
View File

@ -1,7 +1,7 @@
pipeline { pipeline {
agent { agent {
node { node {
label 'metersphere' label 'metersphere-buildx'
} }
} }
options { quietPeriod(600) } options { quietPeriod(600) }
@ -17,15 +17,13 @@ pipeline {
// sh "yarn install" // sh "yarn install"
// sh "cd .." // sh "cd .."
sh "./mvnw clean package --settings ./settings.xml" sh "./mvnw clean package --settings ./settings.xml"
sh "mkdir -p backend/target/dependency && (cd backend/target/dependency; jar -xf ../*.jar)"
} }
} }
} }
stage('Docker build & push') { stage('Docker build & push') {
steps { 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 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"
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}"
} }
} }
} }