Skip to content

Commit f89ad06

Browse files
authored
Use docker export for artifact extraction and upload build log (#407)
1 parent a8f591b commit f89ad06

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
lines changed

.github/actions/docker-build-artifacts/action.yml

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ inputs:
1515
description: Package maintainer
1616
WORKING_DIRECTORY:
1717
required: true
18-
default: '.'
18+
default: "."
1919
description: Working directory
2020
ARTIFACTS_PATTERN:
2121
required: false
2222
default: '.*\.(deb|rpm)$'
2323
description: Regexp that matches artifacts
2424
ARTIFACTS_DIR:
2525
required: false
26-
default: 'BUILD'
26+
default: "BUILD"
2727
description: Output directory for artifacts
2828
BUILD_LOG_FILENAME:
2929
required: false
30-
default: 'build.log'
30+
default: "build.log"
3131
description: Build log filename
3232

3333
runs:
@@ -57,45 +57,59 @@ runs:
5757
REPO_PASSWORD: ${{ env.REPO_PASSWORD }}
5858
DEPLOYMENT_TOKEN: ${{ env.DEPLOYMENT_TOKEN }}
5959
run: |
60-
docker build \
61-
--build-arg BUILD_NUMBER="${GITHUB_RUN_ID}" \
62-
--build-arg GIT_SHA="$(echo ${GITHUB_SHA} | cut -c1-10)" \
63-
--build-arg MAINTAINER="${{ inputs.MAINTAINER }}" \
64-
--build-arg REPO_DOMAIN="${{ inputs.REPO_DOMAIN }}" \
65-
--build-arg REPO_USERNAME="${{ env.REPO_USERNAME }}" \
66-
--file "${{ inputs.DOCKERFILE }}" \
67-
--platform linux/${{ inputs.PLATFORM }} \
68-
--progress=plain \
69-
--secret id=REPO_PASSWORD,env=REPO_PASSWORD \
70-
--secret id=DEPLOYMENT_TOKEN,env=DEPLOYMENT_TOKEN \
71-
--tag artifacts-${GITHUB_RUN_ID}:${GITHUB_SHA} \
72-
--ulimit nofile=1024000:1024000 \
73-
--force-rm \
74-
--pull \
75-
. 2>&1 | tee -a ${{ inputs.BUILD_LOG_FILENAME }}
60+
BUILD_ARGS=(
61+
--build-arg BUILD_NUMBER="${GITHUB_RUN_ID}"
62+
--build-arg GIT_SHA="$(echo ${GITHUB_SHA} | cut -c1-10)"
63+
--build-arg MAINTAINER="${{ inputs.MAINTAINER }}"
64+
--build-arg REPO_DOMAIN="${{ inputs.REPO_DOMAIN }}"
65+
--build-arg REPO_USERNAME="${{ env.REPO_USERNAME }}"
66+
--file "${{ inputs.DOCKERFILE }}"
67+
--platform linux/${{ inputs.PLATFORM }}
68+
--progress=plain
69+
--secret id=REPO_PASSWORD,env=REPO_PASSWORD
70+
--secret id=DEPLOYMENT_TOKEN,env=DEPLOYMENT_TOKEN
71+
--tag artifacts-${GITHUB_RUN_ID}:${GITHUB_SHA}
72+
--ulimit nofile=1024000:1024000
73+
--pull
74+
)
75+
76+
if docker buildx version >/dev/null 2>&1; then
77+
docker buildx create --use --name larger_log \
78+
--driver-opt env.BUILDKIT_STEP_LOG_MAX_SIZE=50000000 || true
79+
80+
docker buildx build "${BUILD_ARGS[@]}" --load \
81+
. 2>&1 | tee -a ${{ inputs.BUILD_LOG_FILENAME }}
82+
83+
docker buildx rm larger_log || true
84+
else
85+
docker build "${BUILD_ARGS[@]}" --force-rm \
86+
. 2>&1 | tee -a ${{ inputs.BUILD_LOG_FILENAME }}
87+
fi
88+
89+
- name: Upload build log
90+
if: failure()
91+
uses: actions/upload-artifact@v6
92+
with:
93+
name: build-log-${{ github.run_id }}-${{ github.sha }}
94+
path: ${{ inputs.WORKING_DIRECTORY }}/${{ inputs.BUILD_LOG_FILENAME }}
95+
retention-days: 7
96+
if-no-files-found: ignore
7697

7798
- name: Extract artifacts from image
7899
shell: bash
79100
working-directory: ${{ inputs.WORKING_DIRECTORY }}
80101
run: |
81102
set -euo pipefail
82103
83-
export TEMP_DIR=$(mktemp -d)
84-
85-
# dump Docker image blobs
86-
docker save artifacts-${GITHUB_RUN_ID}:${GITHUB_SHA} --output "${TEMP_DIR}/artifacts-${GITHUB_RUN_ID}-${GITHUB_SHA}.tar" && \
87-
tar -xf "${TEMP_DIR}/artifacts-${GITHUB_RUN_ID}-${GITHUB_SHA}.tar" -C "${TEMP_DIR}" && \
88-
rm -f "${TEMP_DIR}/artifacts-${GITHUB_RUN_ID}-${GITHUB_SHA}.tar"
89-
90-
# extract blobs content
91-
mkdir -p "${{ inputs.ARTIFACTS_DIR }}" && find "${TEMP_DIR}/" -type f -exec file {} + \
92-
| grep -E ":.*tar archive" \
93-
| cut -d: -f1 \
94-
| xargs -rI{} tar --keep-newer-files -xf {} -C "${{ inputs.ARTIFACTS_DIR }}"
104+
# create container from image and export filesystem
105+
container_id=$(docker create artifacts-${GITHUB_RUN_ID}:${GITHUB_SHA} sh)
106+
mkdir -p "${{ inputs.ARTIFACTS_DIR }}"
107+
docker export "$container_id" | tar -xf - -C "${{ inputs.ARTIFACTS_DIR }}"
108+
find "${{ inputs.ARTIFACTS_DIR }}" -type d -empty -delete
95109
96110
# cleanup
97-
docker image rm artifacts-${GITHUB_RUN_ID}:${GITHUB_SHA} && \
98-
rm -rf "${TEMP_DIR}"
111+
docker rm "$container_id"
112+
docker image rm artifacts-${GITHUB_RUN_ID}:${GITHUB_SHA}
99113
100114
if [ "$(find "${{ inputs.ARTIFACTS_DIR }}" -type f | wc -l)" -lt 1 ]; then
101115
echo "No files found in ${{ inputs.ARTIFACTS_DIR }}."

.github/workflows/cicd-docker-build-and-distribute.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,21 @@ jobs:
145145
steps:
146146
- name: Checkout code
147147
if: ${{ inputs.REF == '' }}
148-
uses: actions/checkout@v4
148+
uses: actions/checkout@v6
149149
with:
150150
fetch-depth: 0
151151
path: ${{ inputs.CODE_WORKING_DIRECTORY }}
152152

153153
- name: Checkout code by REF
154154
if: ${{ inputs.REF != '' }}
155-
uses: actions/checkout@v4
155+
uses: actions/checkout@v6
156156
with:
157157
ref: ${{ inputs.REF }}
158158
fetch-depth: 0
159159
path: ${{ inputs.CODE_WORKING_DIRECTORY }}
160160

161161
- name: Checkout reusable actions
162-
uses: actions/checkout@v4
162+
uses: actions/checkout@v6
163163
with:
164164
repository: signalwire/actions-template
165165
ref: main
@@ -188,7 +188,7 @@ jobs:
188188
DEPLOYMENT_TOKEN: ${{ secrets.DEPLOYMENT_TOKEN }}
189189

190190
- name: Upload build logs
191-
uses: actions/upload-artifact@v4
191+
uses: actions/upload-artifact@v6
192192
with:
193193
name: ${{ inputs.TARGET_ARTIFACT_NAME }}.log
194194
path: ${{ inputs.CODE_WORKING_DIRECTORY }}/artifacts-*.log

0 commit comments

Comments
 (0)