|
| 1 | +name: Test |
| 2 | + |
| 3 | +on: [ push ] |
| 4 | + |
| 5 | +jobs: |
| 6 | + # This job builds the Docker image that will be used in the following test job. |
| 7 | + # It also publishes the image to the GitHub Container Registry, so every PR can |
| 8 | + # have an up-to-date image by the name of ghcr.io/GITHUB_USERNAME/PROJECT_NAME:BRANCH_NAME |
| 9 | + build-image: |
| 10 | + runs-on: ubuntu-24.04 |
| 11 | + env: |
| 12 | + BUILT_IMAGE_NAME: ghcr.io/urbanmachine/node_helpers |
| 13 | + PROJECT_NAME: node_helpers |
| 14 | + |
| 15 | + defaults: |
| 16 | + run: |
| 17 | + shell: bash |
| 18 | + steps: |
| 19 | + - name: Create A Unique Tag For This Image Based On Branch Name |
| 20 | + id: prep |
| 21 | + run: | |
| 22 | + if [[ "${GITHUB_REF}" == "refs/heads/"* ]] |
| 23 | + then |
| 24 | + # This is a build on a branch |
| 25 | + TAG=${GITHUB_REF#refs/heads/} |
| 26 | + elif [[ "${GITHUB_REF}" == "refs/pull/"* ]] |
| 27 | + then |
| 28 | + # This is a PR build |
| 29 | + TAG=${GITHUB_REF#refs/pull/} |
| 30 | + elif [[ "${GITHUB_REF}" == "refs/tags/"* ]] |
| 31 | + then |
| 32 | + # This is a tagged build |
| 33 | + TAG=${GITHUB_REF#refs/tags/} |
| 34 | + else |
| 35 | + echo "Unexpected reference format ${GITHUB_REF}" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + |
| 39 | + # Remove slashes, since they're not allowed in Docker tags |
| 40 | + TAG=$(echo "${TAG}" | sed 's#/#-#g') |
| 41 | + echo "tagged_image=${BUILT_IMAGE_NAME}:${TAG}" >> $GITHUB_OUTPUT |
| 42 | + - name: Set up Docker Buildx |
| 43 | + uses: docker/setup-buildx-action@v2 |
| 44 | + - name: Load Previous Docker Layers Cache |
| 45 | + uses: actions/cache@v4 |
| 46 | + with: |
| 47 | + path: /tmp/.buildx-cache |
| 48 | + # Key is named differently to avoid collision |
| 49 | + key: v1-${{ env.PROJECT_NAME }}-${{ runner.os }}-multi-buildx-${{ github.sha }} |
| 50 | + restore-keys: v1-${{ env.PROJECT_NAME }}-${{ runner.os }}-multi-buildx |
| 51 | + - name: Log in to GitHub Container Registry |
| 52 | + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin |
| 53 | + - name: Checkout |
| 54 | + uses: actions/checkout@v4 |
| 55 | + with: |
| 56 | + lfs: true |
| 57 | + - name: Get Base Image Name |
| 58 | + id: get_base_image |
| 59 | + run: | |
| 60 | + source .env |
| 61 | + echo "base_image=${BASE_IMAGE}" >> $GITHUB_OUTPUT |
| 62 | + - name: Build and push |
| 63 | + uses: docker/build-push-action@v5 |
| 64 | + with: |
| 65 | + context: . |
| 66 | + file: docker/Dockerfile |
| 67 | + push: true |
| 68 | + tags: ${{ steps.prep.outputs.tagged_image }} |
| 69 | + build-args: | |
| 70 | + BASE_IMAGE=${{ steps.get_base_image.outputs.base_image }} |
| 71 | + cache-from: type=local,src=/tmp/.buildx-cache |
| 72 | + # Note the mode=max here |
| 73 | + # More: https://github.com/moby/buildkit#--export-cache-options |
| 74 | + # And: https://github.com/docker/buildx#--cache-tonametypetypekeyvalue |
| 75 | + cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new |
| 76 | + # This is done to avoid storing outdated build steps in the cache, since |
| 77 | + # BuildKit never clears stuff out of the cache itself |
| 78 | + - name: Move cache |
| 79 | + run: | |
| 80 | + rm -rf /tmp/.buildx-cache |
| 81 | + mv /tmp/.buildx-cache-new /tmp/.buildx-cache |
| 82 | + outputs: |
| 83 | + tagged_image: ${{ steps.prep.outputs.tagged_image }} |
| 84 | + |
| 85 | + # This job runs the colcon tests for the project |
| 86 | + ros-tests: |
| 87 | + needs: build-image |
| 88 | + runs-on: ubuntu-24.04 |
| 89 | + defaults: |
| 90 | + run: |
| 91 | + shell: bash |
| 92 | + timeout-minutes: 40 |
| 93 | + container: |
| 94 | + image: ${{needs.build-image.outputs.tagged_image}} |
| 95 | + credentials: |
| 96 | + username: ${{ github.actor }} |
| 97 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 98 | + env: |
| 99 | + GITHUB_WORKSPACE: /repo/ |
| 100 | + steps: |
| 101 | + - name: Install Git LFS |
| 102 | + run: sudo apt-get update && sudo apt-get install git-lfs |
| 103 | + - name: Checkout |
| 104 | + uses: actions/checkout@v4 |
| 105 | + with: |
| 106 | + lfs: true |
| 107 | + - name: Fix git error in the following line |
| 108 | + # Caused by an update to git that dislikes when users of different ID's touch |
| 109 | + # the same git directory, which happens when using a docker runner in CI |
| 110 | + # This fixes test reporting in the `Generate Test Report` section |
| 111 | + # https://github.blog/2022-04-12-git-security-vulnerability-announced/ |
| 112 | + # https://github.com/actions/checkout/issues/760 |
| 113 | + run: git config --global --add safe.directory ${GITHUB_WORKSPACE} |
| 114 | + - name: Copy Python dependencies to github workspace |
| 115 | + run: cp -r /robot/install install |
| 116 | + - name: Copy Build output to github workspace |
| 117 | + run: cp -r /robot/install build |
| 118 | + - name: Move launch-profile files to /robot/launch-profiles so hardcoded paths work |
| 119 | + run: cp -r ${GITHUB_WORKSPACE}/launch-profiles /robot/ |
| 120 | + - name: Run tests |
| 121 | + run: | |
| 122 | + enter-workspaces colcon test \ |
| 123 | + --base-paths pkgs \ |
| 124 | + --pytest-with-coverage \ |
| 125 | + --test-result-base test-results \ |
| 126 | + --pytest-args \ |
| 127 | + --durations=50 |
| 128 | + - name: Generate Test Report |
| 129 | + uses: dorny/test-reporter@v1 |
| 130 | + if: always() |
| 131 | + with: |
| 132 | + name: "Test report" |
| 133 | + path: test-results/**/*.xml |
| 134 | + reporter: java-junit |
| 135 | + - name: Report Codecov Coverage |
| 136 | + uses: codecov/codecov-action@v3.1.0 |
| 137 | + with: |
| 138 | + token: ${{ secrets.CODECOV_TOKEN }} |
0 commit comments