Skip to content

Commit

Permalink
CI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Curnow committed Jul 8, 2019
1 parent 1810ec7 commit 43c086a
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ DEPS
BUILD
BUILDROOT
*.swp
sign
pushover-cli*
24 changes: 11 additions & 13 deletions Jenkinsfile.debian9
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
24 changes: 11 additions & 13 deletions Jenkinsfile.mint19
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
144 changes: 144 additions & 0 deletions build
Original file line number Diff line number Diff line change
@@ -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
29 changes: 0 additions & 29 deletions build.sh

This file was deleted.

6 changes: 3 additions & 3 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions dpkg.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 43c086a

Please sign in to comment.