From 43c086a4e565f3b7600913f4dd2173323e20d7b8 Mon Sep 17 00:00:00 2001 From: Jamie Curnow Date: Mon, 8 Jul 2019 11:49:09 +1000 Subject: [PATCH] CI improvements --- .gitignore | 1 + Jenkinsfile.debian9 | 24 ++++---- Jenkinsfile.mint19 | 24 ++++---- build | 144 ++++++++++++++++++++++++++++++++++++++++++++ build.sh | 29 --------- debian/rules | 6 +- dpkg.json | 8 +++ 7 files changed, 178 insertions(+), 58 deletions(-) create mode 100755 build delete mode 100755 build.sh create mode 100644 dpkg.json diff --git a/.gitignore b/.gitignore index 30c686d..93050ba 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ DEPS BUILD BUILDROOT *.swp +sign pushover-cli* diff --git a/Jenkinsfile.debian9 b/Jenkinsfile.debian9 index 7a4a3cc..2f7bd3f 100644 --- a/Jenkinsfile.debian9 +++ b/Jenkinsfile.debian9 @@ -7,29 +7,27 @@ pipeline { label 'rpm' } environment { - PACKAGE = "pushover-cli" - DOCKER_BUILD = "jc21/dpkg-debian:9-golang" + PACKAGE = "pushover-cli" } stages { stage('Prepare') { steps { - sh 'docker pull ${DOCKER_BUILD}' - sh 'docker pull ${DOCKER_CI_TOOLS}' - sh 'rm -rf sign' - sh 'mkdir -p sign' + ansiColor('xterm') { + sh 'rm -rf sign' + sh 'mkdir -p sign' - dir(path: 'sign') { - git(url: "${RPM_SIGN_GIT_REPO}", credentialsId: "${RPM_SIGN_CREDENTIALS_ID}") - sh 'chmod 600 .gnupg/*' + dir(path: 'sign') { + git(url: "${RPM_SIGN_GIT_REPO}", credentialsId: "${RPM_SIGN_CREDENTIALS_ID}") + sh 'chmod 600 .gnupg/*' + } } - - // Change ownership of the directory we're going to mess with - sh 'docker run --rm -v $(pwd):/home/builder/package ${DOCKER_BUILD} sudo chown -R builder:builder /home/builder/package' } } stage('Build') { steps { - sh 'docker run --rm --name dpkgbuild-${BUILD_TAG} -e GOPROXY=${GOPROXY} -v $(pwd):/home/builder/package -v $(pwd)/sign/.gnupg:/home/builder/.gnupg -w /home/builder/package ${DOCKER_BUILD} bash -c "sudo chown -R builder:builder /home/builder/.gnupg && /home/builder/package/build.sh"' + ansiColor('xterm') { + sh './build debian 9-golang' + } } } stage('Publish') { diff --git a/Jenkinsfile.mint19 b/Jenkinsfile.mint19 index d56c9aa..941996c 100644 --- a/Jenkinsfile.mint19 +++ b/Jenkinsfile.mint19 @@ -7,29 +7,27 @@ pipeline { label 'rpm' } environment { - PACKAGE = "pushover-cli" - DOCKER_BUILD = "jc21/dpkg-mint:19-golang" + PACKAGE = "pushover-cli" } stages { stage('Prepare') { steps { - sh 'docker pull ${DOCKER_BUILD}' - sh 'docker pull ${DOCKER_CI_TOOLS}' - sh 'rm -rf sign' - sh 'mkdir -p sign' + ansiColor('xterm') { + sh 'rm -rf sign' + sh 'mkdir -p sign' - dir(path: 'sign') { - git(url: "${RPM_SIGN_GIT_REPO}", credentialsId: "${RPM_SIGN_CREDENTIALS_ID}") - sh 'chmod 600 .gnupg/*' + dir(path: 'sign') { + git(url: "${RPM_SIGN_GIT_REPO}", credentialsId: "${RPM_SIGN_CREDENTIALS_ID}") + sh 'chmod 600 .gnupg/*' + } } - - // Change ownership of the directory we're going to mess with - sh 'docker run --rm -v $(pwd):/home/builder/package ${DOCKER_BUILD} sudo chown -R builder:builder /home/builder/package' } } stage('Build') { steps { - sh 'docker run --rm --name dpkgbuild-${BUILD_TAG} -e GOPROXY=${GOPROXY} -v $(pwd):/home/builder/package -v $(pwd)/sign/.gnupg:/home/builder/.gnupg -w /home/builder/package ${DOCKER_BUILD} bash -c "sudo chown -R builder:builder /home/builder/.gnupg && /home/builder/package/build.sh"' + ansiColor('xterm') { + sh './build mint 19-golang' + } } } stage('Publish') { diff --git a/build b/build new file mode 100755 index 0000000..64e0940 --- /dev/null +++ b/build @@ -0,0 +1,144 @@ +#!/bin/bash -e + +################################################## +# +# ./build DISTRO [DOCKER_TAG] +# +# If no docker tag, `latest` is used +# +# ie: +# ./build debian 9 +# +################################################## + +CWD=$(pwd) +CYAN='\E[1;36m' +RED='\E[1;31m' +YELLOW='\E[1;33m' +GREEN='\E[1;32m' +BLUE='\E[1;34m' +RESET='\E[0m' + +DISTRO=$1 +if [ "$DISTRO" == "" ]; then + echo -en "${RED}ERROR: ${RESET}" + echo -e "You must specify a Distro to build for: debian, mint" + echo "ie: ./build debian" + exit 1 +fi + +DOCKER_TAG=$2 +if [ "$DOCKER_TAG" == "" ]; then + DOCKER_TAG=latest +fi + +# Make sure jq exists +if ! hash jq 2>/dev/null; then + echo -en "${RED}ERROR: ${RESET}" + echo -e "jq command is required" + exit 1 +fi + +# Make sure docker exists +if ! hash docker 2>/dev/null; then + echo -en "${RED}ERROR: ${RESET}" + echo -e "docker command is required" + exit 1 +fi + +# Variables +DOCKER_IMAGE=jc21/dpkg-${DISTRO} +PACKAGE=$(cat dpkg.json | jq -r .PACKAGE) +VERSION=$(cat dpkg.json | jq -r .VERSION) +GH_USER=$(cat dpkg.json | jq -r .GH_USER) +GH_VERSION=$(cat dpkg.json | jq -r .GH_VERSION) +SIGN_KEY=$(cat dpkg.json | jq -r .SIGN_KEY) +URL= + +export PACKAGE VERSION GH_USER GH_VERSION +eval "URL=$(cat dpkg.json | jq -r .URL)" + +if [ -z "$URL" ]; then + echo -en "${RED}ERROR: ${RESET}" + echo -e "URL could not be determined from dpkg.json file" + exit 1 +fi + +echo -e "${BLUE}❯ ${GREEN}Building ${CYAN}${PACKAGE} v${VERSION} ${GREEN}for ${DISTRO}:${DOCKER_TAG}${RESET}" + +# Clean +rm -rf "${PACKAGE}-${VERSION}" + +# Download +echo -e "${BLUE}❯ ${YELLOW}Downloading${RESET}" +if [ -e "${PACKAGE}_${VERSION}.orig.tar.gz" ]; then + echo "${PACKAGE}_${VERSION}.orig.tar.gz exists, skipping..." +else + wget -nv "${URL}" -O "${PACKAGE}_${VERSION}.orig.tar.gz" +fi + +# Extract +echo -e "${BLUE}❯ ${YELLOW}Extracting${RESET}" +tar xzf "${PACKAGE}_${VERSION}.orig.tar.gz" + +# Copy over debian package info +echo -e "${BLUE}❯ ${YELLOW}Setting up ./debian folder${RESET}" +cp -pr debian "${PACKAGE}-${VERSION}"/ + +# Pull latest builder image +echo -e "${BLUE}❯ ${YELLOW}Pulling docker image: ${DOCKER_IMAGE}:${DOCKER_TAG}${RESET}" +docker pull "${DOCKER_IMAGE}:${DOCKER_TAG}" + +# Use the build to change the ownership of folders +echo -e "${BLUE}❯ ${YELLOW}Temporarily changing ownership${RESET}" +docker run --rm \ + -v "${CWD}:/home/builder/package" \ + -v "${CWD}/sign/.gnupg:/home/builder/.gnupg" \ + "${DOCKER_IMAGE}:${DOCKER_TAG}" \ + sudo chown -R builder:builder /home/builder/package /home/builder/.gnupg + +# Build +echo -e "${BLUE}❯ ${YELLOW}Building Part 1${RESET}" +set +e + +# Docker Run Part 1 +docker run --rm \ + --name "dpkgbuild-${BUILD_TAG:-dpkg-${DISTRO}-${PACKAGE}}" \ + -v "${CWD}:/home/builder/package" \ + -v "${CWD}/sign/.gnupg:/home/builder/.gnupg" \ + -e "GOPROXY=${GOPROXY}" \ + -w "/home/builder/package/${PACKAGE}-${VERSION}" \ + "${DOCKER_IMAGE}:${DOCKER_TAG}" \ + sudo mk-build-deps --install --tool='apt-get --no-install-recommends -y' debian/control + +BUILD_SUCCESS=$? + +# Docker Run Part 2 +if [ $BUILD_SUCCESS -eq 0 ]; then + echo -e "${BLUE}❯ ${YELLOW}Building Part 2${RESET}" + docker run --rm \ + --name "dpkgbuild-${BUILD_TAG:-dpkg-${DISTRO}-${PACKAGE}}" \ + -v "${CWD}:/home/builder/package" \ + -v "${CWD}/sign/.gnupg:/home/builder/.gnupg" \ + -e "GOPROXY=${GOPROXY}" \ + -e "DPKG_COLORS=always" \ + -w "/home/builder/package/${PACKAGE}-${VERSION}" \ + "${DOCKER_IMAGE}:${DOCKER_TAG}" \ + dpkg-buildpackage --sign-key="${SIGN_KEY}" --force-sign + + BUILD_SUCCESS=$? +fi + +# Change ownership back +echo -e "${BLUE}❯ ${YELLOW}Reverting ownership${RESET}" +docker run --rm \ + -v "${CWD}:/home/builder/package" \ + -v "${CWD}/sign/.gnupg:/home/builder/.gnupg" \ + "${DOCKER_IMAGE}:${DOCKER_TAG}" \ + sudo chown -R "$(id -u):$(id -g)" /home/builder/package /home/builder/.gnupg + +# do we need to exit the loop? +if [ $BUILD_SUCCESS -ne 0 ]; then + echo -e "${BLUE}❯ ${RED}Exiting due to error${RESET}" + exit ${BUILD_SUCCESS} +fi diff --git a/build.sh b/build.sh deleted file mode 100755 index fbb7f2b..0000000 --- a/build.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash -e - -sudo apt-get install -y wget make devscripts - -PACKAGE=pushover-cli -GH_USER=jc21 -VERSION=`cat version` - -# Clean -rm -rf ${PACKAGE}-${VERSION} - -# Download -if [ -e "${PACKAGE}_${VERSION}.orig.tar.gz" ]; then - echo "${PACKAGE}_${VERSION}.orig.tar.gz exists, skipping.." -else - wget -nv https://github.com/${GH_USER}/${PACKAGE}/archive/${VERSION}.tar.gz - mv -f ${VERSION}.tar.gz ${PACKAGE}_${VERSION}.orig.tar.gz -fi - -# Extract -tar xzf ${PACKAGE}_${VERSION}.orig.tar.gz - -# Copy over debian package info -cp -pr debian ${PACKAGE}-${VERSION}/ - -# Build -cd ${PACKAGE}-${VERSION} -sudo mk-build-deps --install --tool='apt-get --no-install-recommends -y' debian/control -dpkg-buildpackage --sign-key=EFD591B218E19AE3376D80D14F9E050D1DFFBB86 --force-sign diff --git a/debian/rules b/debian/rules index 5f79706..e31930e 100755 --- a/debian/rules +++ b/debian/rules @@ -19,8 +19,8 @@ # Go parameters -VERSION=$(shell cat package.json | jq -r .version) -GITCOMMIT=$(shell git rev-list -1 HEAD) +VERSION=$(shell cat ../dpkg.json | jq -r ".VERSION") +COMMIT=$(shell cat ../dpkg.json | jq -r ".COMMIT") GOCMD=go GOBUILD=$(GOCMD) build GOCLEAN=$(GOCMD) clean @@ -30,7 +30,7 @@ BINARY_NAME=pushover-cli all: build build: - GO111MODULE=on $(GOBUILD) -o bin/$(BINARY_NAME) -v -ldflags "-X main.GitCommit=$(GITCOMMIT) -X main.AppVersion=$(VERSION)" + GO111MODULE=on $(GOBUILD) -o bin/$(BINARY_NAME) -v -ldflags "-X main.GitCommit=$(COMMIT) -X main.AppVersion=$(VERSION)" clean: $(GOCLEAN) diff --git a/dpkg.json b/dpkg.json new file mode 100644 index 0000000..f65daf1 --- /dev/null +++ b/dpkg.json @@ -0,0 +1,8 @@ +{ + "PACKAGE": "pushover-cli", + "VERSION": "1.0.1", + "COMMIT": "4bfab24bb56f32da942b467c55da817269ff5cbb", + "GH_USER": "jc21", + "URL": "https://github.com/${GH_USER}/${PACKAGE}/archive/${VERSION}.tar.gz", + "SIGN_KEY": "EFD591B218E19AE3376D80D14F9E050D1DFFBB86" +} \ No newline at end of file