Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jenkins: fix cleanup permission issue #19

Merged
merged 3 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

post code isn't guaranteed to run due to issues within Jenkins

Copy link
Collaborator Author

@Erikvv Erikvv Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stefan showed me that cleanup is done by https://plugins.jenkins.io/resource-disposer/ but this fails and he has to do it manually, so therefor this PR exists.

What do you suggest? I don't see harm in leaving cleanWs() here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

storj/storj does a delete before running: https://github.com/storj/storj/blob/main/Jenkinsfile.public#L23

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

storj/storj is forced to do that, because it leaves behind files in the workspace with root owner, which can only be removed by the root user.

After this PR uplink-php doesn't have that problem. Normal cleanup should work.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanup failed due to a timeout / jenkins crash not due to the permissions part.

}
}
}
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