Skip to content

Commit 369f5c2

Browse files
authored
Merge pull request #7 from k7hpn/release/1.2.0
Update to support docker-lock
2 parents e93205e + 9f9aebb commit 369f5c2

File tree

4 files changed

+90
-49
lines changed

4 files changed

+90
-49
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [1.2.0] - 2022-12-13
11+
12+
### Added
13+
14+
- Ability to use docker-lock for Dockerfile pinning
15+
1016
## [1.1.0] - 2021-12-02
1117

1218
### Added
@@ -30,7 +36,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3036
- `azure-pipelines-yml` to show how to call the script from Azure
3137
- GitHub workflow `build.yml` to show how to call the script as a GitHub Action
3238

33-
[unreleased]: https://github.com/mcld/buildscript/compare/v1.1.0...HEAD
39+
[unreleased]: https://github.com/mcld/buildscript/compare/v1.2.0...HEAD
40+
[1.2.0]: https://github.com/mcld/buildscript/compare/v1.1.0...v1.2.0
3441
[1.1.0]: https://github.com/mcld/buildscript/compare/v1.0.1...v1.1.0
3542
[1.0.1]: https://github.com/mcld/buildscript/compare/v1.0.0...v1.0.1
3643
[1.0.0]: https://github.com/mcld/buildscript/releases/tag/v1.0.0

Dockerfile

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# prepare base image
2-
FROM mcr.microsoft.com/dotnet/runtime:7.0@sha256:bc86158b6c02a0983e3377be0a71b17982ca5ccb00840b0c44abc4184f6326a7 AS base
2+
FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base
33
WORKDIR /app
44

55
# get build image
6-
FROM mcr.microsoft.com/dotnet/sdk:7.0@sha256:a320a69c64e425e7eb42f8841d034fc3a4bb7a925ebb834c13680925c85e282c AS build
6+
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
77
WORKDIR /src
88

99
# run dotnet restore
@@ -31,21 +31,21 @@ ARG IMAGE_VERSION=unknown
3131

3232
# Configure image labels
3333
LABEL branch=$branch \
34-
maintainer="Maricopa County Library District developers <development@mcldaz.org>" \
35-
org.opencontainers.image.authors="Maricopa County Library District developers <development@mcldaz.org>" \
36-
org.opencontainers.image.created=$IMAGE_CREATED \
37-
org.opencontainers.image.description="Build script test project" \
38-
org.opencontainers.image.licenses="MIT" \
39-
org.opencontainers.image.revision=$IMAGE_REVISION \
40-
org.opencontainers.image.source="https://github.com/MCLD/buildscript" \
41-
org.opencontainers.image.title="Build script test project" \
42-
org.opencontainers.image.vendor="Maricopa County Library District" \
43-
org.opencontainers.image.version=$IMAGE_VERSION
34+
maintainer="Maricopa County Library District developers <development@mcldaz.org>" \
35+
org.opencontainers.image.authors="Maricopa County Library District developers <development@mcldaz.org>" \
36+
org.opencontainers.image.created=$IMAGE_CREATED \
37+
org.opencontainers.image.description="Build script test project" \
38+
org.opencontainers.image.licenses="MIT" \
39+
org.opencontainers.image.revision=$IMAGE_REVISION \
40+
org.opencontainers.image.source="https://github.com/MCLD/buildscript" \
41+
org.opencontainers.image.title="Build script test project" \
42+
org.opencontainers.image.vendor="Maricopa County Library District" \
43+
org.opencontainers.image.version=$IMAGE_VERSION
4444

4545
# Default image environment variable settings
4646
ENV org.opencontainers.image.created=$IMAGE_CREATED \
47-
org.opencontainers.image.revision=$IMAGE_REVISION \
48-
org.opencontainers.image.version=$IMAGE_VERSION
47+
org.opencontainers.image.revision=$IMAGE_REVISION \
48+
org.opencontainers.image.version=$IMAGE_VERSION
4949

5050
COPY --from=publish /app/publish .
5151
ENTRYPOINT ["dotnet", "buildscript.dll"]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The `build.bash` script helps build Docker images and upload them to container r
99
- Other branches go through the `build` stage in the `Dockerfile` but not further (and no image is uploaded).
1010
- Build environment information is brought in as container labels (Git commit id, build date, version) and you can easily add more.
1111
- Can push to a configured container registry (Docker Hub by default) as well as the GitHub Container registry.
12+
- Can utilize [docker-lock](https://github.com/safe-waters/docker-lock) to handle pinning of images in each `Dockerfile`
1213

1314
**You only need `build.bash`, the rest of this project is testing and examples.**
1415

@@ -33,6 +34,7 @@ Environment variables (all are optional):
3334
- `CR_OWNER` - owner of the container registry
3435
- `CR_PASSWORD` - password to log into the container registry
3536
- `CR_USER` - username to log in to the container registry
37+
- `DOCKER_LOCK_VERSION` - a version of docker-lock to use (e.g. 0.8.10), can also be specified in `docker-lock-version.txt`
3638
- `GHCR_OWNER` - owner of the GitHub Container Registry (defaults to `GHCR_USER`)
3739
- `GHCR_PAT` - GitHub Container Registry Personal Access Token
3840
- `GHCR_USER` - username to log in to the GitHub Container Registry
@@ -64,6 +66,7 @@ See [this sample Dockerfile](https://github.com/mcld/buildscript/blob/main/Docke
6466
- `CR_OWNER` - container registry repository (username or organization).
6567
- `CR_PASSWORD` - container registration password or Personal Access Token.
6668
- `CR_USER` - container registry username for authentication.
69+
- `DOCKER_LOCK_VERSION` - a version of docker-lock to use (e.g. 0.8.10), can also be specified in `docker-lock-version.txt`
6770
- `GHCR_OWNER` - GitHub Container Registry repository (username or organization name).
6871
- `GHCR_PAT` - GitHub Personal Access Token.
6972
- `GHCR_USER` - username associated with the GitHub Personal Access Token (for authentication).

build.bash

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
set -Eeuo pipefail
1313
trap cleanup SIGINT SIGTERM ERR EXIT
1414

15-
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
16-
1715
usage() {
1816
cat <<EOF
1917
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-df Dockerfile] [-p] [Docker tag]
@@ -34,11 +32,13 @@ Environment variables:
3432
- CR_OWNER - optional - owner of the container registry
3533
- CR_PASSWORD - optional - password to log into the container registry
3634
- CR_USER - optional - username to log in to the container registry
35+
- DOCKER_LOCK_VERSION - optional - a version of docker-lock to use (e.g. 0.8.10), can also be
36+
specified in docker-lock-version.txt
3737
- GHCR_OWNER - optional - owner of the GitHub Container Registry (defaults to GHCR_USER)
3838
- GHCR_PAT - optional - GitHub Container Registry Personal Access Token
3939
- GHCR_USER - optional - username to log in to the GitHub Container Registry
4040
41-
Version 1.1.0 released 2021-12-02
41+
Version 1.2.0 released 2022-12-13
4242
EOF
4343
exit
4444
}
@@ -70,27 +70,27 @@ die() {
7070
parse_params() {
7171
dockerfile=''
7272
publish=0
73-
73+
7474
while :; do
7575
case "${1-}" in
76-
-h | --help) usage ;;
77-
-v | --verbose) set -x ;;
78-
--no-color) NO_COLOR=1 ;;
79-
-df | --dockerfile)
80-
dockerfile="${2-}"
81-
shift
76+
-h | --help) usage ;;
77+
-v | --verbose) set -x ;;
78+
--no-color) NO_COLOR=1 ;;
79+
-df | --dockerfile)
80+
dockerfile="${2-}"
81+
shift
8282
;;
83-
-p | --publish)
84-
publish=1
83+
-p | --publish)
84+
publish=1
8585
;;
86-
-?*) die "Unknown option: $1" ;;
87-
*) break ;;
86+
-?*) die "Unknown option: $1" ;;
87+
*) break ;;
8888
esac
8989
shift
9090
done
91-
91+
9292
readonly dockertag="${1-}"
93-
93+
9494
return 0
9595
}
9696

@@ -110,6 +110,37 @@ BLD_RELEASE_VERSION=''
110110

111111
readonly BLD_STARTAT=$SECONDS
112112

113+
SYSARCH=$(arch)
114+
readonly SYSARCH
115+
116+
if [[ ${SYSARCH} = "i386" ]]; then
117+
readonly ARCH="x86_32"
118+
elif [[ ${SYSARCH} = "aarch64" ]]; then
119+
readonly ARCH="armv7"
120+
else
121+
readonly ARCH=${SYSARCH}
122+
fi
123+
124+
OS=$(uname -s)
125+
readonly OS
126+
127+
if [[ -z ${DOCKER_LOCK_VERSION-} && -f "docker-lock-version.txt" ]]; then
128+
BLD_DOCKER_LOCK_VERSION=$(cat docker-lock-version.txt)
129+
readonly BLD_DOCKER_LOCK_VERSION
130+
else
131+
BLD_DOCKER_LOCK_VERSION=${DOCKER_LOCK_VERSION-}
132+
readonly BLD_DOCKER_LOCK_VERSION
133+
fi
134+
135+
if [[ -n ${BLD_DOCKER_LOCK_VERSION-} && -f "docker-lock.json" ]]; then
136+
readonly DOCKER_LOCK_URL="https://github.com/safe-waters/docker-lock/releases/download/v${BLD_DOCKER_LOCK_VERSION}/docker-lock_${BLD_DOCKER_LOCK_VERSION}_${OS}_${ARCH}.tar.gz"
137+
msg "${BLUE}===${NOFORMAT} Using docker-lock.json version ${BLD_DOCKER_LOCK_VERSION} to pin Dockerfile(s)"
138+
mkdir -p ".docker/cli-plugins"
139+
curl -fsSL "${DOCKER_LOCK_URL}" | tar -xz -C ".docker/cli-plugins" "docker-lock"
140+
chmod +x ".docker/cli-plugins/docker-lock"
141+
.docker/cli-plugins/docker-lock lock rewrite
142+
fi
143+
113144
# Try getting branch from Azure DevOps
114145
readonly AZURE_BRANCH=${BUILD_SOURCEBRANCH-}
115146
BLD_BRANCH=''
@@ -128,13 +159,13 @@ if [[ -z ${BLD_BRANCH} ]]; then
128159
fi
129160

130161
if [[ $BLD_BRANCH = "develop"
131-
|| $BLD_BRANCH = "main"
132-
|| $BLD_BRANCH = "master"
133-
|| $BLD_BRANCH = "test" ]]; then
162+
|| $BLD_BRANCH = "main"
163+
|| $BLD_BRANCH = "master"
164+
|| $BLD_BRANCH = "test" ]]; then
134165
BLD_DOCKER_TAG=$BLD_BRANCH
135166
BLD_VERSION=${BLD_BRANCH}-${BLD_VERSION_DATE}
136167
BLD_PUSH=true
137-
elif [[ "$BLD_BRANCH" =~ release/([0-9]+\.[0-9]+\.[0-9]+.*) ]]; then
168+
elif [[ "$BLD_BRANCH" =~ release/([0-9]+\.[0-9]+\.[0-9]+.*) ]]; then
138169
BLD_RELEASE_VERSION=${BASH_REMATCH[1]}
139170
BLD_DOCKER_TAG=v${BLD_RELEASE_VERSION}
140171
BLD_VERSION=v${BLD_RELEASE_VERSION}
@@ -201,12 +232,12 @@ if [[ $BLD_PUSH = true ]]; then
201232
--build-arg IMAGE_CREATED="$BLD_VERSION_DATE" \
202233
--build-arg IMAGE_REVISION="$BLD_COMMIT" \
203234
--build-arg IMAGE_VERSION="$BLD_VERSION" .
204-
235+
205236
msg "${GREEN}===${NOFORMAT} Docker image built"
206-
237+
207238
dockeruser=${CR_USER-}
208239
dockerpass=${CR_PASSWORD-}
209-
240+
210241
if [[ -z $dockeruser || -z $dockerpass ]]; then
211242
msg "${ORANGE}===${NOFORMAT} Not pushing Docker image: username or password not specified"
212243
else
@@ -218,28 +249,28 @@ if [[ $BLD_PUSH = true ]]; then
218249
echo "$dockerpass" | \
219250
docker login -u "$dockeruser" --password-stdin "${CR_HOST-}" || exit $?
220251
fi
221-
252+
222253
msg "${BLUE}===${NOFORMAT} Pushing image $BLD_FULL_DOCKER_IMAGE"
223254
docker push "$BLD_FULL_DOCKER_IMAGE"
224-
255+
225256
if [[ $BLD_RELEASE = "true" ]]; then
226257
msg "${BLUE}===${NOFORMAT} Tagging and pushing $BLD_FULL_DOCKER_LATEST"
227258
docker tag "$BLD_FULL_DOCKER_IMAGE" "$BLD_FULL_DOCKER_LATEST"
228259
docker push "$BLD_FULL_DOCKER_LATEST"
229260
fi
230-
261+
231262
msg "${GREEN}===${NOFORMAT} Docker image pushed"
232-
263+
233264
msg "${BLUE}===${NOFORMAT} Executing logout"
234265
if [[ -z ${CR_HOST-} ]]; then
235266
docker logout
236267
else
237268
docker logout "${CR_HOST-}"
238269
fi
239270
fi
240-
271+
241272
ghcruser=${GHCR_USER-}
242-
273+
243274
if [[ -n $ghcruser ]]; then
244275
ghcrowner=${GHCR_OWNER-}
245276
if [[ -z $ghcrowner ]]; then
@@ -250,20 +281,20 @@ if [[ $BLD_PUSH = true ]]; then
250281
echo "$GHCR_PAT" | \
251282
docker login ghcr.io -u "${ghcruser}" --password-stdin || exit $?
252283
docker push "ghcr.io/${ghcrowner}/${BLD_DOCKER_IMAGE}"
253-
284+
254285
if [[ $BLD_RELEASE = "true" ]]; then
255286
msg "${BLUE}===${NOFORMAT} Tagging and pushing ghcr.io/${ghcrowner}/${BLD_DOCKER_LATEST}"
256287
docker tag "${BLD_FULL_DOCKER_IMAGE}" "ghcr.io/${ghcrowner}/${BLD_DOCKER_LATEST}"
257288
docker push "ghcr.io/${ghcrowner}/${BLD_DOCKER_LATEST}"
258289
fi
259-
290+
260291
docker logout ghcr.io
261-
292+
262293
msg "${GREEN}===${NOFORMAT} Docker image pushed to ghcr.io"
263294
fi
264-
295+
265296
# Perform release publish in the Docker machine if configuration is present
266-
297+
267298
if [[ $BLD_RELEASE = "true" && -f "release-publish.bash" && publish -eq 1 ]]; then
268299
msg "${BLUE}===${NOFORMAT} Publishing release package for $BLD_RELEASE_VERSION"
269300
mkdir -p publish

0 commit comments

Comments
 (0)