Modify ARG value in Windows docker container - windows

I am trying to build a docker container with the atlassian-build-runner but make it configurable to specify either latest version (w/o suffix) or latest version using a suffix using the following snippet:
ARG BASE_IMAGE=mcr.microsoft.com/dotnet/framework/sdk:4.8
FROM $BASE_IMAGE
...
# Install Atlassian build, specify --build-arg=-x.y to use different version than latest release
ARG BUILDER_VERSION=""
ADD https://product-downloads.atlassian.com/software/bitbucket/pipelines/atlassian-bitbucket-pipelines-runner$BUILDER_VERSION.zip bitbucket-pipelines-runner.zip
RUN powershell -Command Expand-Archive bitbucket-pipelines-runner.zip C:\bitbucket-pipelines-runner
I would like the user to not have to specify a leading dash (-) in the build argument. Any pointers on how to do this on a Windows container?
I already tried modifying the environment variable in PowerShell (default shell for the Windows base image) to no avail:
ARG BUILDER_VERSION=""
ENV BUILDER_VERSION=$BUILDER_VERSION
ENV if ([string]::IsNullOrEmpty($Env:BUILDER_VERSION)) { return "-" + $Env:BUILDER_VERSION}
But it keeps failing the download since the dash is missing when a version build argument is supplied. Any pointers?

Related

How to build an image from Dockerfile using Earthly target?

I would like to build and image from Dockerfile using Earthly.
You might be wondering why do I want that, because I can describe images right inside of Earthfile, but I have 2 reasons for using external Dockerfile:
ADD command (which I need to download file by URL) is not supported by Earthly yet
I would like to use a heredoc syntax for embedding file's content into container right from Dockerfile. This requires # syntax=docker/dockerfile:1.4, which is again not available in Earthfile
So, here is what I tried to do.
My approximate Dockerfile looks like:
# syntax=docker/dockerfile:1.4
FROM gcr.io/distroless/java17:nonroot
WORKDIR /opt/app
ADD --chown=nonroot https://github.com/microsoft/ApplicationInsights-Java/releases/download/3.4.7/applicationinsights-agent-3.4.7.jar agent.jar
COPY <<EOF /opt/app/applicationinsights.json
{
"instrumentation": {}
}
EOF
And this is how I try to build it with Earthly:
base-image:
FROM earthly/dind:alpine
WORKDIR /build
ENV DOCKER_BUILDKIT=1 # <---- required to support heredoc syntax
COPY distroless-runtime-17.Dockerfile Dockerfile
WITH DOCKER --allow-privileged
RUN docker build . -t base-17-image
END
While the WITH DOCKER RUN part gets executed successfully, I do not know how to use the result of base-image target in other targets to package my app using the resulting base image. The FROM base-17-image just fails as if it does not exist (and this tag really does not exist - docker run base-17-image fails with the same reason).
It turned out to be very easy and natively supported:
The whole recipe is just 2 lines of code:
base-image:
FROM DOCKERFILE -f distroless-runtime-17.Dockerfile .
and the result can of the above step can be reused to package your application as: FROM +base-image

How can I switch between env files based on the build command using godotenv?

I'm planning to use godotenv to setup different environments for my project but I am not sure how to switch between files like dev.env, uat.env, prod.env
I want to be able to just pass a value in my Docker command like RUN go build -o my-project --prod . and have godotenv pickup the relative env file - in this case prod.env (assuming this is the correct way.
Also, how can I make sure that the other env files don't get included in the build of a particular env.
I will advice you use the -X flag as suggested by Go Documentation on Command Line
-X importpath.name=value
Set the value of the string variable in importpath named name to value.
This is only effective if the variable is declared in the source code either uninitialized or initialized to a constant string expression. -X will not work if the initializer makes a function call or refers to other variables.
Note that before Go 1.5 this option took two separate arguments.
Such that you can then call any of your .env file referencing it location.
E.g. go build -ldflags="-X 'package_path.variable_name=new_value'"
That is
go build -ldflags "-X 'my/main/config.Version=v1.0.0'" -o $(MY_BIN) $(MY_SRC)
Hard coding your environment at the build stage seems odd to me. You don't want to build a diff image per env, that's wasteful.
The module documentation suggests a better approach:
Existing envs take precedence of envs that are loaded later.
The convention for managing multiple environments (i.e. development, test, production) is to create an env named {YOURAPP}_ENV and load envs in this order:
env := os.Getenv("FOO_ENV")
if "" == env {
env = "development"
}
godotenv.Load(".env." + env + ".local")
if "test" != env {
godotenv.Load(".env.local")
}
godotenv.Load(".env." + env)
godotenv.Load() // The Original .env

Can Jenkins pipeline use variables in agent stage?

Problem
I'm using Jenkins pipeline and testing python with tox.
This combination explodes when tox creates a python virtualenv with a pip
whose shebang line exceeds the hard coded system imposed maximum of 127 characters.
To set a shorter workspace location,I want to add:
agent {
node {
label 'debian-slave'
customWorkspace "workspace/${env.GIT_BRANCH}"
}
}
but env.GIT_BRANCH is not yet defined. environment has not yet been calculated when the agent block runs.
How can I add the branch number or something similar to the workdir definition?
I don't want to lose the per-branch unique workspaces.
What Jenkins variables exist at the "agent" stage?

Wireshark rpm packaging - setting the release number

I'm running the configure.ac on RHEL 7.2, I'm wondering if there's a way to set the Release number (which is defined om the spec file) as a variable like the Version number which is being generated by the configure.ac and it's written to the config.h file , I'd like to set a kind of BUILD_NUMBER variable somewhere, and it'll take the value of the exported variable during the execution.
The release number for an RPM package is set by the Release: tag in the spec-file. Some spec-files are generated, e.g., using autoconf to substitute values such as the release number in a template, e.g., mypackage.spec.in, to obtain mypackage.spec
A quick check of wireshark's source shows that it uses this scheme, but its template hardcodes the release number as 1. You could modify the configure script and template to add your own option.
For example, adapting the style of --with-XXX options used in the wireshark 2.0.1 configure.ac, you would add a chunk like this (untested):
AC_ARG_WITH([release],
AC_HELP_STRING( [--with-release=#<:#1/no/4/5#:>#],
[set release-number in package #<:#default=1#:>#]),
with_release="$withval", with_release="unspecified")
case "x$with_release" in
x[[1-9]]*)
RELEASE="$with_release"
;;
*)
AC_MSG_ERROR(release is not a number: $with_release)
;;
esac
AC_SUBST(RELEASE)
and use the RELEASE variable in packaging/rpm/SPECS/wireshark.spec.in, as you see the VERSION value used:
Release: #RELEASE#
Alternatively, if you are using the wireshark source without modifying it directly, your build script could
unpack the sources,
update the spec-file,
repack the tarball,
deploy the updated tarball to your build area
Either way, you would have to do some work.

where dch (from devscripts) take a string for version bump?

Debian's devscripts suite has dch tool which allow to add new version to debian/changelog file.
When I add new version I make this:
package (1.0.2-1myname1-ubuntu0) UNRELEASED; urgency=medium
*
-- signature and date
package (1.0.2-1myname1) unstable; urgency=medium
* old changes
-- signature and date
If version ends on 'ubuntu' it bumped properly (ubuntu1, ubuntu2, etc), and when I use my own suite, it just append 'ubuntu'.
Where dch take sting 'ubuntu' to add to version?
That string is coming from the dpkg-vendor command. You can control it at least with the DEB_VENDOR environment variable, via the DEBCHANGE_VENDOR devscripts configuration option or the dch/debchange command-line --vendor option.
For more information please check the respective man pages.

Resources