diff --git a/Jenkinsfile b/Jenkinsfile index 3116e4b..434d5f2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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 { @@ -57,7 +88,8 @@ pipeline { } } steps { - unstash "build" + unstash "build-x64" + unstash "build-arm64" sh "zip -r release.zip *" archiveArtifacts "release.zip" } @@ -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' diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d769f44 --- /dev/null +++ b/Makefile @@ -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 diff --git a/build.sh b/build.sh deleted file mode 100755 index 3b6b508..0000000 --- a/build.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash -set -e -rm -rf ./tmp -mkdir ./tmp -git clone --branch v1.5.0 https://github.com/storj/uplink-c.git ./tmp/uplink-c -cd ./tmp/uplink-c -make build -cd ../.. -mkdir -p build -cat ./tmp/uplink-c/.build/uplink/uplink_definitions.h \ - ./tmp/uplink-c/.build/uplink/uplink.h \ - > build/uplink-php.h -cat ./tmp/uplink-c/.build/libuplink.so > build/libuplink.so - -## 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 diff --git a/docker/go-docker/Dockerfile b/docker/go-docker/Dockerfile new file mode 100644 index 0000000..03a5c68 --- /dev/null +++ b/docker/go-docker/Dockerfile @@ -0,0 +1,4 @@ +FROM golang:1.17.2 + +RUN apt update +RUN apt install -y docker.io diff --git a/Dockerfile b/docker/phpunit/Dockerfile similarity index 100% rename from Dockerfile rename to docker/phpunit/Dockerfile diff --git a/src/Uplink.php b/src/Uplink.php index 1d28f30..65b0305 100644 --- a/src/Uplink.php +++ b/src/Uplink.php @@ -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);