diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 26a0e894..e8efcb1e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,7 +15,7 @@ env: jobs: build-and-push-image: name: Build and push image - runs-on: self-hosted + runs-on: [self-hosted, build] outputs: version: ${{ steps.build.version.outputs.version }} permissions: @@ -35,6 +35,18 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 + - name: Cache Docker layers + uses: actions/cache@v4 + with: + path: | + /tmp/.buildx-cache/cache/test + /tmp/.buildx-cache/cache/latest + /tmp/.buildx-cache/cache-new/test + /tmp/.buildx-cache/cache-new/latest + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + - name: Log in to the Container registry uses: docker/login-action@v2 with: @@ -42,15 +54,6 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Create cache directory - run: | - mkdir -p ./.buildx-cache/cache - mkdir ./.buildx-cache/cache-new - mkdir ./.buildx-cache/cache/test - mkdir ./.buildx-cache/cache/latest - mkdir ./.buildx-cache/cache-new/test - mkdir ./.buildx-cache/cache-new/latest - - name: Test build if: github.event_name == 'pull_request' uses: docker/build-push-action@v3 @@ -59,8 +62,8 @@ jobs: file: ./build/docker/agent/Dockerfile load: true tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}:test - cache-from: type=local,src=./.buildx-cache/cache/test/ - cache-to: type=local,dest=./.buildx-cache/cache-new/test/,mode=max + cache-from: type=local,src=/tmp/.buildx-cache/cache/test/ + cache-to: type=local,dest=/tmp/.buildx-cache/cache-new/test/,mode=max build-args: | USERNAME=paf USER_UID=1000 @@ -76,31 +79,51 @@ jobs: push: true # tag 'latest' and version on push to main, otherwise use the commit hash tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest - cache-from: type=local,src=./.buildx-cache/cache/latest/ - cache-to: type=local,dest=./.buildx-cache/cache-new/latest/,mode=max + cache-from: type=local,src=/tmp/.buildx-cache/cache/latest/ + cache-to: type=local,dest=/tmp/.buildx-cache/cache-new/latest/,mode=max build-args: | USERNAME=paf USER_UID=1000 USER_GID=1000 - - name: Save PR ID + - name: Save pull request artifact if: github.event_name == 'pull_request' env: PR_ID: ${{ github.event.number }} run: | - mkdir -p ./pr - echo $PR_ID > ./pr/pr_id + mkdir -p ./artifact + printf '{ + "is_pr": true, + "pr_id": $PR_ID + }' >> ./artifact/artifact.json - - name: Upload PR ID - if: github.event_name == 'pull_request' + - name: Save merge artifact + if: github.event_name != 'pull_request' + run: | + mkdir -p ./artifact + printf '{ + "is_pr": false, + "pr_id": -1 + }' >> ./artifact/artifact.json + + - name: Upload artifact uses: actions/upload-artifact@v4 with: - name: pr_id - path: pr/ + name: artifact.json + path: ./artifact/ retention-days: 1 - - name: Clean up cache + - name: Clean up PR cache + if: github.event_name == 'pull_request' + run: | + rm -rf /tmp/.buildx-cache/cache/test + mv /tmp/.buildx-cache/cache-new/test /tmp/.buildx-cache/cache/test + + - name: Clean up merge cache + if: github.event_name != 'pull_request' run: | - rm -rf ./.buildx-cache/cache/test - rm -rf ./.buildx-cache/cache/latest - mv ./.buildx-cache/cache-new ./.buildx-cache/cache \ No newline at end of file + rm -rf /tmp/.buildx-cache/cache/latest + mv /tmp/.buildx-cache/cache-new/latest /tmp/.buildx-cache/cache/latest + + - name: Prune all images older than 1 days from self-hosted runner + run: docker image prune -a -f --filter "until=24h" \ No newline at end of file diff --git a/.github/workflows/drive.yml b/.github/workflows/drive.yml index 933fb9a9..8b53cd59 100644 --- a/.github/workflows/drive.yml +++ b/.github/workflows/drive.yml @@ -8,7 +8,7 @@ on: jobs: drive: - runs-on: self-hosted + runs-on: [self-hosted, drive] env: AGENT_VERSION: latest COMPOSE_FILE: ./build/docker-compose.cicd.yaml @@ -20,8 +20,7 @@ jobs: run: | echo "AGENT_VERSION=${AGENT_VERSION}" echo "COMPOSE_FILE=${COMPOSE_FILE}" - - name: 'Download artifact (PR ID)' - if: github.event_name == 'pull_request' + - name: Download artifact uses: actions/github-script@v6 with: script: | @@ -31,10 +30,10 @@ jobs: run_id: context.payload.workflow_run.id, }); let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { - return artifact.name == "pr_id" + return artifact.name == "artifact.json" })[0]; if (!matchArtifact) { - core.setFailed('No pr_id artifact found from the build workflow'); + core.setFailed('No artifact found from the build workflow'); return; } let download = await github.rest.actions.downloadArtifact({ @@ -44,23 +43,20 @@ jobs: archive_format: 'zip', }); let fs = require('fs'); - fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr_id.zip`, Buffer.from(download.data)); + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/artifact.zip`, Buffer.from(download.data)); - - name: 'Unzip artifact (PR ID)' - if: github.event_name == 'pull_request' + - name: Unzip artifact run: | - unzip pr_id.zip + unzip artifact.zip - - name: Check PR ID - if: github.event_name == 'pull_request' + - name: Return artifact JSON + id: return-artifact-json uses: actions/github-script@v6 with: script: | - let issue_number = fs.readFileSync('./pr_id'); - if (!issue_number || isNaN(Number(issue_number))) { - core.setFailed(`Invalid PR ID: ${prIdContent}`); - return; - } + let fs = require('fs'); + let data = JSON.parse(fs.readFileSync('./artifacts/artifacts.json')); + return data; - name: Run docker-compose run: | @@ -73,12 +69,10 @@ jobs: if: always() run: docker compose down -v # add rendered JSON as comment to the pull request - - name: Add simulation results as comment - if: github.event_name == 'pull_request' + - name: Create simulation results table + id: simulation-results uses: actions/github-script@v6 with: - github-token: ${{ secrets.GITHUB_TOKEN }} - # this script reads the simulation_results.json and creates a comment on the pull request with the results. script: | const fs = require('fs'); // read the simulation results @@ -94,13 +88,26 @@ jobs: let resultsTableDivider = `| --- | --- |`; // add everything to the resultsTable resultsTable = resultsTableHeader + '\n' + resultsTableDivider + '\n' + resultsTable.join('\n'); + return resultsTable; + result-encoding: string + - name: Print simulation results + run: | + echo "Simulation results\n" + echo "${{ steps.simulation-results.outputs.result }}" + - name: Add simulation results as comment + if: ${{ steps.return-artifact-json.outputs.result.is_pr }} + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + # this script reads the simulation_results.json and creates a comment on the pull request with the results. + script: | + let issue_number = Number(${{ steps.return-artifact-json.outputs.pr_id }}); // add the results as a comment to the pull request - let issue_number = Number(fs.readFileSync('./pr_id')); github.rest.issues.createComment({ issue_number: issue_number, owner: context.repo.owner, repo: context.repo.repo, - body: "## Simulation results\n" + resultsTable + body: "## Simulation results\n" + ${{ steps.simulation-results.outputs.result }} }); - - name: Prune all images older than 30 days from self-hosted runner - run: docker image prune -a --force --filter "until=720h" \ No newline at end of file + - name: Prune all images older than 1 days from self-hosted runner + run: docker image prune -a --filter "until=24h" \ No newline at end of file