Skip to content

Commit

Permalink
Add windows container support, simplify binary generation (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
car-roll authored May 14, 2021
1 parent 9292636 commit a502943
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 105 deletions.
4 changes: 0 additions & 4 deletions Dockerfile

This file was deleted.

8 changes: 8 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env groovy

/* `buildPlugin` step provided by: https://github.com/jenkins-infra/pipeline-library */
// tests skipped because no junit reports generated.
buildPlugin(failFast: false, tests: [skip: true], configurations: [
[ platform: "docker", jdk: "8", jenkins: null ],
[ platform: "windock", jdk: "8", jenkins: null ],
])
94 changes: 38 additions & 56 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.18</version>
<groupId>org.jenkins-ci</groupId>
<artifactId>jenkins</artifactId>
<version>1.63</version>
<relativePath />
</parent>
<groupId>io.jenkins.plugins</groupId>
<artifactId>lib-durable-task</artifactId>
<packaging>jar</packaging>
<version>${revision}${changelist}</version>
<name>Durable Task Library</name>
<description>Generates and packages the binaries used by Durable Task Plugin</description>
<url>http://maven.apache.org</url>
<url>https://github.com/jenkinsci/lib-durable-task</url>
<licenses>
<license>
<name>MIT License</name>
Expand Down Expand Up @@ -72,71 +71,54 @@
<tag>${scmTag}</tag>
</scm>

<dependencies>
</dependencies>
<profiles>
<profile>
<id>bash</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<binary.generation.script>docker-generate-binaries.sh</binary.generation.script>
</properties>
</profile>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<binary.generation.script>docker-generate-binaries.bat</binary.generation.script>
</properties>
</profile>
</profiles>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.35.0</version>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<images>
<image>
<alias>durable-task-binary-generator</alias>
<name>jenkins-ci.durable-task/binary-generator:${project.version}</name>
<run>
<volumes>
<bind>
<volume>${project.basedir}/src:/durabletask</volume>
</bind>
</volumes>
<log>
<date>default</date>
</log>
<wait>
<log>binary generation complete.</log>
<time>300000</time>
</wait>
</run>
</image>
</images>
<executable>${project.basedir}/src/${binary.generation.script}</executable>
<workingDirectory>${project.basedir}/src</workingDirectory>
<arguments>
<argument>${revision}</argument>
<argument>${project.build.directory}/classes</argument>
</arguments>
</configuration>
<executions>
<execution>
<id>build-docker-image</id>
<phase>initialize</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>compile-in-docker</id>
<id>docker-generate-binaries</id>
<phase>compile</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>docker-cleanup</id>
<phase>package</phase>
<goals>
<goal>stop</goal>
<goal>remove</goal>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src</directory>
<includes>
<include>durable_task_monitor_*_darwin_*</include>
<include>durable_task_monitor_*_unix_*</include>
</includes>
</resource>
</resources>
</build>

</project>
30 changes: 30 additions & 0 deletions src/Dockerfile.linux
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ARG BASE_DIR=/durabletask
ARG NAME=durable_task_monitor
ARG VERSION=0.0

FROM golang:1.16.4-buster AS builder
ARG BASE_DIR
ARG NAME
ARG VERSION
COPY cmd $BASE_DIR/cmd
COPY pkg $BASE_DIR/pkg
WORKDIR $BASE_DIR/pkg/common
RUN go mod tidy
RUN go test -v
WORKDIR $BASE_DIR/cmd/bash
RUN go mod tidy
RUN go test -v
RUN CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -a -o ${NAME}_${VERSION}_darwin_amd_64
RUN CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -a -o ${NAME}_${VERSION}_darwin_arm_64
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o ${NAME}_${VERSION}_linux_64
RUN CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -a -o ${NAME}_${VERSION}_linux_32
# TODO WINDOWS
# WORKDIR $BASE_DIR/cmd/windows
# RUN go mod tidy
# RUN go test -v

FROM scratch AS export-stage
ARG BASE_DIR
ARG NAME
ARG VERSION
COPY --from=builder $BASE_DIR/cmd/bash/${NAME}_${VERSION}_* /
39 changes: 39 additions & 0 deletions src/Dockerfile.windows
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
ARG BASE_DIR=/durabletask
ARG NAME=durable_task_monitor
ARG VERSION=0.0

FROM golang:1.16.4-nanoserver AS builder
ARG BASE_DIR
ARG NAME
ARG VERSION
COPY cmd $BASE_DIR/cmd
COPY pkg $BASE_DIR/pkg
WORKDIR $BASE_DIR/pkg/common
RUN go test -v
WORKDIR $BASE_DIR/cmd/bash
# go mod tidy fails in windows CI due to permissions of module cache. Each dependency needs to be fetched with -modcacherw flag
# see golang.org/issue/31481
# RUN go mod tidy
RUN go get -modcacherw jenkinsci.org/plugins/durabletask/common
RUN go get -modcacherw golang.org/x/sys
# can't test bash on windows
RUN set CGO_ENABLED=0& set GOOS=darwin& set GOARCH=amd64& go build -a -o %NAME%_%VERSION%_darwin_amd_64
RUN set CGO_ENABLED=0& set GOOS=darwin& set GOARCH=arm64& go build -a -o %NAME%_%VERSION%_darwin_arm_64
RUN set CGO_ENABLED=0& set GOOS=linux& set GOARCH=amd64& go build -a -o %NAME%_%VERSION%_linux_64
RUN set CGO_ENABLED=0& set GOOS=linux& set GOARCH=386& go build -a -o %NAME%_%VERSION%_linux_32
# TODO: WINDOWS
# WORKDIR $BASE_DIR/cmd/windows
# RUN go mod tidy
# RUN go test -v

# TODO: uncomment once docker build --output (i.e. BuildKit) is available for windows containers
# see: https://github.com/microsoft/Windows-Containers/issues/34
# FROM mcr.microsoft.com/windows/nanoserver:20H2 as export-stage
# ARG BASE_DIR
# ARG NAME
# ARG VERSION
# WORKDIR $BASE_DIR
# COPY --from=builder $BASE_DIR/cmd/bash/${NAME}_${VERSION}_darwin_amd_64 $BASE_DIR
# COPY --from=builder $BASE_DIR/cmd/bash/${NAME}_${VERSION}_darwin_arm_64 $BASE_DIR
# COPY --from=builder $BASE_DIR/cmd/bash/${NAME}_${VERSION}_linux_64 $BASE_DIR
# COPY --from=builder $BASE_DIR/cmd/bash/${NAME}_${VERSION}_linux_32 $BASE_DIR
2 changes: 1 addition & 1 deletion src/cmd/bash/durable_task_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestLauncher(t *testing.T) {
wg.Wait()

launchOutput := launchBuffer.String()
launchLoggerExp := regexp.MustCompile(`^args 0: sh\nargs 1: -xe\nargs 2: .*\/src\/cmd\/bash\/test-script.sh\nlaunched \d*\nscript exit code: 0`)
launchLoggerExp := regexp.MustCompile(`^args 0: sh\nargs 1: -xe\nargs 2: .*\/cmd\/bash\/test-script.sh\nlaunched \d*\nscript exit code: 0`)
if !launchLoggerExp.MatchString(launchOutput) {
t.Errorf("launch output incorrect:\n%v", launchOutput)
}
Expand Down
5 changes: 2 additions & 3 deletions src/cmd/bash/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module jenkinsci.org/plugins/durabletask/bash

go 1.14
go 1.16

replace jenkinsci.org/plugins/durabletask/common => ../../pkg/common

require (
// pin x/sys to 1.14 by manually running: go get golang.org/x/[email protected]
golang.org/x/sys v0.0.0-20200201011859-915c9c3d4ccf
golang.org/x/sys v0.0.0-20210507161434-a76c4d0a0096
jenkinsci.org/plugins/durabletask/common v0.0.0-00010101000000-000000000000
)
4 changes: 2 additions & 2 deletions src/cmd/bash/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
golang.org/x/sys v0.0.0-20200201011859-915c9c3d4ccf h1:+4j7oujXP478CVb/AFvHJmVX5+Pczx2NGts5yirA0oY=
golang.org/x/sys v0.0.0-20200201011859-915c9c3d4ccf/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210507161434-a76c4d0a0096 h1:5PbJGn5Sp3GEUjJ61aYbUP6RIo3Z3r2E4Tv9y2z8UHo=
golang.org/x/sys v0.0.0-20210507161434-a76c4d0a0096/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
18 changes: 0 additions & 18 deletions src/compile-binaries.sh

This file was deleted.

26 changes: 26 additions & 0 deletions src/docker-generate-binaries.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@echo off
setlocal

rem docker build --output requires BuildKit, not available for windows containers
rem see https://github.com/microsoft/Windows-Containers/issues/34
rem instead, create a temporary writeable container layer to copy out the binaries

rem maven plugin version
set VER=%1
rem output directory of binaries
set DEST=%2
set IMG_NAME=durable-task-binary-generator
set BINARY_NAME=durable_task_monitor
set OUTPUT_DIR=/durabletask/cmd/bash
mkdir "%DEST%"
docker build --build-arg VERSION=%VER% -f Dockerfile.windows -t %IMG_NAME%:%VER% .
docker create -ti --name scratch %IMG_NAME%:%VER%
docker cp scratch:%OUTPUT_DIR%/%BINARY_NAME%_%VER%_darwin_amd_64 %DEST%
docker cp scratch:%OUTPUT_DIR%/%BINARY_NAME%_%VER%_darwin_arm_64 %DEST%
docker cp scratch:%OUTPUT_DIR%/%BINARY_NAME%_%VER%_linux_64 %DEST%
docker cp scratch:%OUTPUT_DIR%/%BINARY_NAME%_%VER%_linux_32 %DEST%
docker rm -f scratch
docker rmi %IMG_NAME%:%VER%

endlocal
@echo on
8 changes: 8 additions & 0 deletions src/docker-generate-binaries.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/sh
set -ex
# maven plugin version
VER=$1
# output directory of binaries
DEST=$2
export DOCKER_BUILDKIT=1
docker build --build-arg VERSION=$VER -o $DEST -f Dockerfile.linux .
2 changes: 1 addition & 1 deletion src/pkg/common/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module jenkinsci.org/plugins/durabletask/common

go 1.14
go 1.16
20 changes: 0 additions & 20 deletions src/rebuild.sh

This file was deleted.

0 comments on commit a502943

Please sign in to comment.