Skip to content

Commit

Permalink
jenkins: fix cleanup permission issue (#19)
Browse files Browse the repository at this point in the history
* arm64 support

Replace build.sh script by Makefile (god help us) so part of it can be
run on a different platform or container.

The new release contains 2 binaries, one for each of the supported
platforms.

I tested this on a Dragonboard 410c.

* jenkins: fix cleanup permission
  • Loading branch information
Erikvv committed Nov 29, 2021
1 parent e391944 commit f2a23b7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 30 deletions.
47 changes: 40 additions & 7 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,48 @@ pipeline {
// }
// }
// }
stage('Go build') {
stage('Go build x64') {
agent {
docker {
image docker.build("storj-ci", "--pull https://github.com/storj/ci.git#main").id
args '--user root:root --volume "/tmp/gomod":/go/pkg/mod '
image 'golang:1.17.2'
args "--volume /tmp/gomod:/go/pkg/mod --user root:root"
}
}
steps {
script {
sh './build.sh'
stash(name: "build", includes: "build/")
// get owner UID of working directory to run commands as that user
def userId = sh(script: "stat -c '%u' .", returnStdout: true).trim()
sh "useradd --create-home --uid ${userId} jenkins"

sh 'su jenkins -c "make build-x64"'
stash(name: "build-x64", includes: "build/")
}
}
post {
always {
cleanWs()
}
}
}
stage('Go build arm64') {
agent {
dockerfile {
dir 'docker/go-docker'
args '--volume /var/run/docker.sock:/var/run/docker.sock --volume /tmp/gomod:/go/pkg/mod --user root:root'
}
}
steps {
script {
// get owner UID of working directory to run commands as that user
def dockerGroupId = sh(script: "stat -c '%g' /var/run/docker.sock", returnStdout: true).trim()
def userId = sh(script: "stat -c '%u' .", returnStdout: true).trim()
// set group id of docker group to that of the host so we may access /var/run/docker.sock
sh "groupmod --gid ${dockerGroupId} docker"
sh "useradd --create-home --gid ${dockerGroupId} --uid ${userId} jenkins"
sh "newgrp docker"

sh 'su jenkins -c "make build-arm64"'
stash(name: "build-arm64", includes: "build/libuplink-aarch64-linux.so")
}
}
post {
Expand All @@ -57,7 +88,8 @@ pipeline {
}
}
steps {
unstash "build"
unstash "build-x64"
unstash "build-arm64"
sh "zip -r release.zip *"
archiveArtifacts "release.zip"
}
Expand Down Expand Up @@ -106,12 +138,13 @@ pipeline {
stage('PHPUnit') {
agent {
dockerfile {
dir 'docker/phpunit'
args '--user root:root '
}
}
steps {
unstash "vendor"
unstash "build"
unstash "build-x64"
sh 'service postgresql start'
sh '''su -s /bin/bash -c "psql -U postgres -c 'create database teststorj;'" postgres'''
sh 'PATH="/root/go/bin:$PATH" && storj-sim network setup --postgres=postgres://postgres@localhost/teststorj?sslmode=disable'
Expand Down
55 changes: 55 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
SHELL = /bin/bash

ifndef GOMODCACHE
$(eval GOMODCACHE=$(shell go env | grep GOMODCACHE | sed -E 's/GOMODCACHE="(.*)"/\1/'))
endif

UID := $(shell id -u)

tmp/uplink-c:
mkdir -p tmp
git clone --branch v1.5.0 https://github.com/storj/uplink-c.git ./tmp/uplink-c

.PHONY: clean
clean:
rm -rf build tmp

build/libuplink-x86_64-linux.so tmp/uplink-c/.build/uplink/uplink.h tmp/uplink-c/.build/uplink/uplink_definitions.h: tmp/uplink-c
cd tmp/uplink-c && make build
mkdir -p build
cat tmp/uplink-c/.build/libuplink.so > build/libuplink-x86_64-linux.so

build/libuplink-aarch64-linux.so: tmp/uplink-c
docker run --rm \
-v $(GOMODCACHE):/go/pkg/mod \
-v $(PWD)/tmp:$(PWD)/tmp \
--workdir $(PWD)/tmp/uplink-c \
-e CGO_ENABLED=1 \
docker.elastic.co/beats-dev/golang-crossbuild:1.17.1-arm \
--build-cmd "useradd --create-home --uid $(UID) jenkins && su jenkins -c 'PATH=\$$PATH:/go/bin:/usr/local/go/bin make build'" \
-p "linux/arm64"
mkdir -p build
cat ./tmp/uplink-c/.build/libuplink.so > build/libuplink-aarch64-linux.so

build/uplink-php.h: tmp/uplink-c/.build/uplink/uplink.h tmp/uplink-c/.build/uplink/uplink_definitions.h
## create C header file
cat ./tmp/uplink-c/.build/uplink/uplink_definitions.h \
./tmp/uplink-c/.build/uplink/uplink.h \
> build/uplink-php.h
## remove stuff PHP can't handle
sed -i 's/typedef __SIZE_TYPE__ GoUintptr;//g' build/uplink-php.h
sed -i 's/typedef float _Complex GoComplex64;//g' build/uplink-php.h
sed -i 's/typedef double _Complex GoComplex128;//g' build/uplink-php.h
sed -i 's/#ifdef __cplusplus//g' build/uplink-php.h
sed -i 's/extern "C" {//g' build/uplink-php.h
sed -i 's/#endif//g' build/uplink-php.h
sed -zi 's/}\n//g' build/uplink-php.h

.PHONY:
build: build-x64 build-arm64

.PHONY:
build-x64: build/libuplink-x86_64-linux.so build/uplink-php.h

.PHONY:
build-arm64: build/libuplink-aarch64-linux.so build/uplink-php.h
22 changes: 0 additions & 22 deletions build.sh

This file was deleted.

4 changes: 4 additions & 0 deletions docker/go-docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM golang:1.17.2

RUN apt update
RUN apt install -y docker.io
File renamed without changes.
5 changes: 4 additions & 1 deletion src/Uplink.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ public static function create(): self
{
$root = realpath(__DIR__ . '/..');

$arch = php_uname('m');
$os = strtolower(php_uname('s'));

$ffi = FFI::cdef(
file_get_contents($root . '/build/uplink-php.h'),
$root . '/build/libuplink.so'
"{$root}/build/libuplink-{$arch}-{$os}.so"
);

return new self($ffi);
Expand Down

0 comments on commit f2a23b7

Please sign in to comment.