Skip to content

Commit

Permalink
chore: Enable Go cache for Docker builds
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI committed Nov 7, 2024
1 parent 90c6ba1 commit 5a2dc4f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,19 @@ jobs:
contents: read
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a
with:
path: |
~/.go-alpine
key: alpine-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
alpine-go-
- name: test
env:
CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
run: |
( cd assets/docker && ./test.sh alpine )
export DOCKER_GOCACHE="$HOME/.go-alpine"
./assets/docker/test.sh alpine
test-archlinux:
needs: changes
if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
Expand All @@ -100,11 +108,19 @@ jobs:
contents: read
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a
with:
path: |
~/.go-archlinux
key: archlinux-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
archlinux-go-
- name: test
env:
CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
run: |
( cd assets/docker && ./test.sh archlinux )
export DOCKER_GOCACHE="$HOME/.go-archlinux"
./assets/docker/test.sh archlinux
test-macos:
needs: changes
if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
Expand Down
7 changes: 7 additions & 0 deletions assets/docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ git config --global --add safe.directory /chezmoi

GO=${GO:-go}

if [ -d "/go-cache" ]; then
echo "Set GOCACHE to /go-cache"
export GOCACHE="/go-cache/cache"
echo "Set GOMODCACHE to /go-cache/modcache"
export GOMODCACHE="/go-cache/modcache"
fi

cd /chezmoi
${GO} run . doctor || true
${GO} test ./...
Expand Down
34 changes: 25 additions & 9 deletions assets/docker/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

set -eufo pipefail

cd ../..
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
REPO_ROOT=$( cd "${SCRIPT_DIR}/../.." &> /dev/null && pwd )
cd "${REPO_ROOT}"

if [ "$#" -eq 0 ]; then
echo "Usage: $0 distribution1 [distribution2 ... distributionN]"
exit 1
fi

for distribution in "$@"; do
echo "${distribution}"
dockerfile="assets/docker/${distribution}.Dockerfile"
Expand All @@ -11,12 +19,20 @@ for distribution in "$@"; do
exit 1
fi
image="$(docker build . -f "assets/docker/${distribution}.Dockerfile" -q)"
docker run \
--env "CHEZMOI_GITHUB_ACCESS_TOKEN=${CHEZMOI_GITHUB_ACCESS_TOKEN-}" \
--env "CHEZMOI_GITHUB_TOKEN=${CHEZMOI_GITHUB_TOKEN-}" \
--env "GITHUB_ACCESS_TOKEN=${GITHUB_ACCESS_TOKEN-}" \
--env "GITHUB_TOKEN=${GITHUB_TOKEN-}" \
--rm \
--volume "${PWD}:/chezmoi" \
"${image}"
docker_command=(
docker run
--env "CHEZMOI_GITHUB_ACCESS_TOKEN=${CHEZMOI_GITHUB_ACCESS_TOKEN-}"
--env "CHEZMOI_GITHUB_TOKEN=${CHEZMOI_GITHUB_TOKEN-}"
--env "GITHUB_ACCESS_TOKEN=${GITHUB_ACCESS_TOKEN-}"
--env "GITHUB_TOKEN=${GITHUB_TOKEN-}"
--rm
--volume "${PWD}:/chezmoi"
)
if [ -n "${DOCKER_GOCACHE-}" ]; then
mkdir -p "${DOCKER_GOCACHE}"
docker_command+=(--volume "${DOCKER_GOCACHE}:/go-cache")
fi
docker_command+=("${image}")
# Run docker
"${docker_command[@]}"
done

0 comments on commit 5a2dc4f

Please sign in to comment.