Skip to content

Commit

Permalink
PMM-12899 Use go mod cache in actions (#3026)
Browse files Browse the repository at this point in the history
* PMM-12899 move devcontainer's
Dockerfile

* PMM-12899 save Go mod and build cache to an image

* PMM-12899 clean up the code

* PMM-12899 do not fail in case of error

* PMM-12899 free up some disk space

* PMM-12899 only pull the platform specific for GH environment

* PMM-12899 clean up the code

* PMM-12899 clean up the code

* PMM-12899 clean up the code
  • Loading branch information
ademidoff committed Jun 24, 2024
1 parent 1062f5a commit f97f194
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 78 deletions.
File renamed without changes.
5 changes: 3 additions & 2 deletions .github/workflows/devcontainer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:

jobs:
devcontainer:
name: Build
name: Devcontainer
runs-on: ubuntu-22.04
timeout-minutes: 15
permissions:
Expand Down Expand Up @@ -53,7 +53,8 @@ jobs:
- name: Build and push to registries
uses: docker/build-push-action@v5
with:
file: ./devcontainer.Dockerfile
file: ./.devcontainer/Dockerfile
context: .
push: true
tags: |
${{ env.GH_DEVCONTAINER_IMAGE }}
Expand Down
9 changes: 3 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,14 @@ jobs:
- name: Run go-consistent
env:
COMMAND: 'bin/go-consistent -pedantic -exclude "tests" ./...'
REDIRECT: "| bin/reviewdog -f=go-consistent -reporter=github-pr-review -fail-on-error=true"
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.ROBOT_TOKEN || secrets.GITHUB_TOKEN }}
run: |
if out=$( ${{ env.COMMAND }} ); exit_code=$?; [ $exit_code -ne 0 ]; then
if [ $exit_code -gt 1 ] || ${{ github.event.pull_request == null }}; then
echo "$out"
exit $exit_code
else
echo "$out" ${{ env.REDIRECT }}
echo "$out" | bin/reviewdog -f=go-consistent -reporter=github-pr-review -fail-on-error=true
fi
else
echo "$out"
Expand All @@ -113,10 +112,8 @@ jobs:
- name: Run debug commands on failure
if: ${{ failure() }}
run: |
env
go version
go env
pwd
env | sort
go env | sort
git status
merge-gatekeeper:
Expand Down
111 changes: 53 additions & 58 deletions .github/workflows/managed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,82 +21,56 @@ on:

jobs:
test:
name: Tests
name: Managed tests
runs-on: ubuntu-22.04
timeout-minutes: 30
permissions:
packages: write

env:
PMM_SERVER_IMAGE: perconalab/pmm-server:3-dev-latest
PMM_CACHE_IMAGE: ghcr.io/percona/pmm:dev-cache
AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }}
AWS_SECRET_KEY: ${{ secrets.AWS_SECRET_KEY }}
PMM_DEV_OAUTH_CLIENT_ID: ${{ secrets.OAUTH_PMM_CLIENT_ID }}
PMM_DEV_OAUTH_CLIENT_SECRET: ${{ secrets.OAUTH_PMM_CLIENT_SECRET }}
DEVCONTAINER_CACHE_ENABLED: false
BUILD_CACHE: ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'v3' }} # change to `main` once v3 goes GA

steps:
- name: Check out code
uses: actions/checkout@v4

- name: Enable Go build cache
if: ${{ fromJSON(env.DEVCONTAINER_CACHE_ENABLED) }}
uses: actions/cache@v4
with:
path: ~/.cache/go-build
key: ${{ runner.os }}-go-build-${{ github.ref }}-${{ hashFiles('**') }}
restore-keys: |
${{ runner.os }}-go-build-${{ github.ref }}-
${{ runner.os }}-go-build-
- name: Enable Go modules cache
if: ${{ fromJSON(env.DEVCONTAINER_CACHE_ENABLED) }}
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-modules-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-go-modules-

- name: Download tools
if: ${{ fromJSON(env.DEVCONTAINER_CACHE_ENABLED) }}
run: |
pushd tools && go mod download -x
popd && go mod download -x
- name: Launch PMM Server (see docker-compose.yml)
run: |
# Note: launching the container with --wait fails for an unknown reason.
# A temporary workaround is to run it manually. To be reverted once the issue is resolved.
# make env-compose-up # the container workdir is /root/go/src/github.com/percona/pmm
docker compose --profile pmm up -d
sleep 100s
make env-compose-up # the container workdir is /root/go/src/github.com/percona/pmm
docker logs pmm-server
- name: Restore Go build cache
if: ${{ fromJSON(env.DEVCONTAINER_CACHE_ENABLED) }}
continue-on-error: true
run: docker cp ~/.cache/go-build pmm-server:/root/.cache/go-build

- name: Restore Go modules cache
if: ${{ fromJSON(env.DEVCONTAINER_CACHE_ENABLED) }}
continue-on-error: true
run: docker cp ~/go/pkg/mod pmm-server:/root/go/pkg/mod
- name: Mark the root directory of pmm as safe
run: docker exec -i pmm-server git config --global --add safe.directory /root/go/src/github.com/percona/pmm

- name: Update binaries
- name: Pull the cache image and inject cache to PMM Server
continue-on-error: true
run: |
docker exec -i pmm-server make run-managed-ci run-agent run-vmproxy
if docker pull --platform linux/amd64 ${{ env.PMM_CACHE_IMAGE }}; then
docker run --rm --volumes-from pmm-server ${{ env.PMM_CACHE_IMAGE }} sh -c "cp -rf /mod-cache/* /root/go/pkg/mod; cp -rf /build-cache/* /root/.cache/go-build"
docker exec -t pmm-server du -sh /root/.cache/go-build
docker exec -t pmm-server du -sh /root/go/pkg/mod
fi
- name: Remove the cache image
continue-on-error: true
run: docker image rm ${{ env.PMM_CACHE_IMAGE }}

- name: Rebuild the binaries
run: docker exec -i pmm-server make run-managed-ci run-agent run-vmproxy run-qan

- name: Check the status of components
run: docker exec -t pmm-server supervisorctl status || true
continue-on-error: true
run: docker exec -t pmm-server supervisorctl status

- name: Run tests
run: docker exec -i pmm-server make -C managed test-cover

- name: Run PMM server update test
run: |
echo "PMM Server update test will be refactored once we have a new update mechanism."
# docker exec -i pmm-server make -C managed test-update
- name: Upload coverage results
uses: codecov/codecov-action@v4
with:
Expand All @@ -106,17 +80,38 @@ jobs:
fail_ci_if_error: false
token: ${{ secrets.CODECOV_TOKEN }}

- name: Cache
if: ${{ fromJSON(env.DEVCONTAINER_CACHE_ENABLED) }}
- name: Run PMM Server update test
run: |
echo "PMM Server update test will be refactored once we have a new update mechanism."
# docker exec -i pmm-server make -C managed test-update
- name: Save PMM Go module and build cache as an image
if: ${{ fromJSON(env.BUILD_CACHE) }}
run: |
docker exec pmm-server go clean -testcache
docker exec pmm-server find ./managed -type d -name fuzzdata -exec rm -r {} +
rm -fr ~/.cache/go-build
mkdir -p ~/.cache
docker cp pmm-server:/root/.cache/go-build ~/.cache/go-build
- name: Run debug commands on failure
if: ${{ failure() }}
df -h | grep -A 1 Filesystem
# We need to free up some space, see more https://github.com/actions/runner-images/issues/2840
sudo rm -rf /usr/share/dotnet
sudo rm -rf /opt/ghc
sudo rm -rf /usr/local/share/boost
df -h | grep -A 1 Filesystem
docker run --name pmm-cache --volumes-from pmm-server busybox sh -c "mkdir /mod-cache /build-cache; cp -r /root/go/pkg/mod/* /mod-cache; cp -r /root/.cache/go-build/* /build-cache"
docker commit pmm-cache ${{ env.PMM_CACHE_IMAGE }}
docker rm -v pmm-cache
- name: Login to ghcr.io registry
if: ${{ fromJSON(env.BUILD_CACHE) }}
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Push the image to ghcr.io
if: ${{ fromJSON(env.BUILD_CACHE) }}
run: docker push ${{ env.PMM_CACHE_IMAGE }}

- name: Run debug commands
if: ${{ always() }}
run: |
env | sort
go env | sort
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ endif

env-up: ## Start devcontainer
COMPOSE_PROFILES=$(PROFILES) \
docker compose up -d
docker compose up -d --wait --wait-timeout 100

env-up-rebuild: env-update-image ## Rebuild and start devcontainer. Useful for custom $PMM_SERVER_IMAGE
COMPOSE_PROFILES=$(PROFILES) \
Expand Down
9 changes: 9 additions & 0 deletions Makefile.devcontainer
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ release-dev-agent: ## Build pmm-agent
release-vmproxy: ## Build vmproxy
make -C vmproxy release

release-qan: ## Build QAN
make -C qan-api2 release

# used by host Makefile
_bash:
/bin/bash
Expand Down Expand Up @@ -38,6 +41,12 @@ run-vmproxy: release-vmproxy
cp $(PMM_RELEASE_PATH)/vmproxy /usr/sbin/vmproxy
supervisorctl start vmproxy

run-qan: release-qan
supervisorctl stop qan-api2
cp $(PMM_RELEASE_PATH)/qan-api2 /usr/sbin/percona-qan-api2
echo -n > /srv/logs/qan-api2.log
supervisorctl start qan-api2

run-all: run-agent run-managed ## Run pmm-managed and pmm-agent

run: ## Deprecated
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
# context: .
# args:
# PMM_SERVER_IMAGE: ${PMM_SERVER_IMAGE:-perconalab/pmm-server:3-dev-latest}
# dockerfile: devcontainer.Dockerfile
# dockerfile: ./.devcontainer/Dockerfile
container_name: pmm-server
hostname: pmm-server
networks:
Expand Down
18 changes: 8 additions & 10 deletions managed/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ PMM_LD_FLAGS = -ldflags " \
-X 'github.com/percona/pmm/version.Branch=$(PMM_RELEASE_BRANCH)' \
"

IS_ARM := $(filter arm64,$(shell uname -m))
PMM_RACE_FLAG ?= $(if $(IS_ARM),,-race)

PMM_TEST_FLAGS ?= -timeout=180s
PMM_TEST_RUN_UPDATE ?= 0
PMM_TEST_FILES ?= ./...

gen: clean ## Generate files
go generate ./...

Expand All @@ -36,17 +43,8 @@ release-starlark:
env CGO_ENABLED=0 go build -v $(PMM_LD_FLAGS) -o $(PMM_RELEASE_PATH)/ ./cmd/pmm-managed-starlark/...
$(PMM_RELEASE_PATH)/pmm-managed-starlark --version

ARCH=$(shell uname -m)
release-dev: ## Build pmm-managed binaries for development
if [ $(ARCH) = "aarch64" ]; then \
go build -gcflags="all=-N -l" -v $(PMM_LD_FLAGS) -o $(PMM_RELEASE_PATH)/ ./cmd/... ; \
else \
go build -race -gcflags="all=-N -l" -v $(PMM_LD_FLAGS) -o $(PMM_RELEASE_PATH)/ ./cmd/... ; \
fi

PMM_TEST_FLAGS ?= -timeout=180s
PMM_TEST_RUN_UPDATE ?= 0
PMM_TEST_FILES ?= ./...
go build $(PMM_RACE_FLAG) -gcflags="all=-N -l" -v $(PMM_LD_FLAGS) -o $(PMM_RELEASE_PATH)/ ./cmd/...

test: ## Run tests
go test $(PMM_TEST_FLAGS) -p 1 -race $(PMM_TEST_FILES)
Expand Down

0 comments on commit f97f194

Please sign in to comment.