|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - release-* |
| 8 | + pull_request: {} |
| 9 | + workflow_dispatch: |
| 10 | + inputs: |
| 11 | + version: |
| 12 | + description: Package version (e.g. v0.1.0) |
| 13 | + required: false |
| 14 | + |
| 15 | +env: |
| 16 | + # Common versions |
| 17 | + GO_VERSION: '1.21.3' |
| 18 | + GOLANGCI_VERSION: 'v1.54.2' |
| 19 | + DOCKER_BUILDX_VERSION: 'v0.11.2' |
| 20 | + |
| 21 | + # These environment variables are important to the Crossplane CLI install.sh |
| 22 | + # script. They determine what version it installs. |
| 23 | + XP_CHANNEL: master # TODO(negz): Pin to stable once v1.14 is released. |
| 24 | + XP_VERSION: current # TODO(negz): Pin to a version once v1.14 is released. |
| 25 | + |
| 26 | + # This CI job will automatically push new builds to xpkg.upbound.io if the |
| 27 | + # XPKG_ACCESS_ID and XPKG_TOKEN secrets are set in the GitHub respository (or |
| 28 | + # organization) settings. Create a token at https://accounts.upbound.io. |
| 29 | + XPKG_ACCESS_ID: ${{ secrets.XPKG_ACCESS_ID }} |
| 30 | + |
| 31 | + # The package to push, without a version tag. The default matches GitHub. For |
| 32 | + # example xpkg.upbound.io/crossplane/function-template-go. |
| 33 | + XPKG: xpkg.upbound.io/${{ github.repository}} |
| 34 | + |
| 35 | + # The package version to push. The default is 0.0.0-gitsha. |
| 36 | + XPKG_VERSION: ${{ inputs.version }} |
| 37 | + |
| 38 | +jobs: |
| 39 | + lint: |
| 40 | + runs-on: ubuntu-22.04 |
| 41 | + steps: |
| 42 | + - name: Checkout |
| 43 | + uses: actions/checkout@v4 |
| 44 | + |
| 45 | + - name: Setup Go |
| 46 | + uses: actions/setup-go@v4 |
| 47 | + with: |
| 48 | + go-version: ${{ env.GO_VERSION }} |
| 49 | + cache: false # The golangci-lint action does its own caching. |
| 50 | + |
| 51 | + - name: Lint |
| 52 | + uses: golangci/golangci-lint-action@v3 |
| 53 | + with: |
| 54 | + version: ${{ env.GOLANGCI_VERSION }} |
| 55 | + |
| 56 | + unit-test: |
| 57 | + runs-on: ubuntu-22.04 |
| 58 | + steps: |
| 59 | + - name: Checkout |
| 60 | + uses: actions/checkout@v4 |
| 61 | + |
| 62 | + - name: Setup Go |
| 63 | + uses: actions/setup-go@v4 |
| 64 | + with: |
| 65 | + go-version: ${{ env.GO_VERSION }} |
| 66 | + |
| 67 | + - name: Run Unit Tests |
| 68 | + run: go test -v -cover ./... |
| 69 | + |
| 70 | + # We want to build most packages for the amd64 and arm64 architectures. To |
| 71 | + # speed this up we build single-platform packages in parallel. We then upload |
| 72 | + # those packages to GitHub as a build artifact. The push job downloads those |
| 73 | + # artifacts and pushes them as a single multi-platform package. |
| 74 | + build: |
| 75 | + runs-on: ubuntu-22.04 |
| 76 | + strategy: |
| 77 | + fail-fast: true |
| 78 | + matrix: |
| 79 | + arch: |
| 80 | + - amd64 |
| 81 | + - arm64 |
| 82 | + steps: |
| 83 | + - name: Setup QEMU |
| 84 | + uses: docker/setup-qemu-action@v3 |
| 85 | + with: |
| 86 | + platforms: all |
| 87 | + |
| 88 | + - name: Setup Docker Buildx |
| 89 | + uses: docker/setup-buildx-action@v3 |
| 90 | + with: |
| 91 | + version: ${{ env.DOCKER_BUILDX_VERSION }} |
| 92 | + install: true |
| 93 | + |
| 94 | + - name: Checkout |
| 95 | + uses: actions/checkout@v4 |
| 96 | + |
| 97 | + # We ask Docker to use GitHub Action's native caching support to speed up |
| 98 | + # the build, per https://docs.docker.com/build/cache/backends/gha/. |
| 99 | + - name: Build Runtime |
| 100 | + id: image |
| 101 | + uses: docker/build-push-action@v5 |
| 102 | + with: |
| 103 | + context: . |
| 104 | + platforms: linux/${{ matrix.arch }} |
| 105 | + cache-from: type=gha |
| 106 | + cache-to: type=gha,mode=max |
| 107 | + target: image |
| 108 | + build-args: |
| 109 | + GO_VERSION=${{ env.GO_VERSION }} |
| 110 | + outputs: type=docker,dest=runtime-${{ matrix.arch }}.tar |
| 111 | + |
| 112 | + - name: Setup the Crossplane CLI |
| 113 | + run: "curl -sL https://raw.githubusercontent.com/crossplane/crossplane/master/install.sh | sh" |
| 114 | + |
| 115 | + - name: Build Package |
| 116 | + run: ./crossplane xpkg build --package-file=${{ matrix.arch }}.xpkg --package-root=package/ --embed-runtime-image-tarball=runtime-${{ matrix.arch }}.tar |
| 117 | + |
| 118 | + - name: Upload Single-Platform Package |
| 119 | + uses: actions/upload-artifact@v3 |
| 120 | + with: |
| 121 | + name: packages |
| 122 | + path: "*.xpkg" |
| 123 | + if-no-files-found: error |
| 124 | + retention-days: 1 |
| 125 | + |
| 126 | + # This job downloads the single-platform packages built by the build job, and |
| 127 | + # pushes them as a multi-platform package. We only push the package it the |
| 128 | + # XPKG_ACCESS_ID and XPKG_TOKEN secrets were provided. |
| 129 | + push: |
| 130 | + runs-on: ubuntu-22.04 |
| 131 | + needs: |
| 132 | + - build |
| 133 | + steps: |
| 134 | + - name: Checkout |
| 135 | + uses: actions/checkout@v4 |
| 136 | + |
| 137 | + - name: Download Single-Platform Packages |
| 138 | + uses: actions/download-artifact@v3 |
| 139 | + with: |
| 140 | + name: packages |
| 141 | + path: . |
| 142 | + |
| 143 | + - name: Setup the Crossplane CLI |
| 144 | + run: "curl -sL https://raw.githubusercontent.com/crossplane/crossplane/master/install.sh | sh" |
| 145 | + |
| 146 | + - name: Login to Upbound |
| 147 | + uses: docker/login-action@v3 |
| 148 | + if: env.XPKG_ACCESS_ID != '' |
| 149 | + with: |
| 150 | + registry: xpkg.upbound.io |
| 151 | + username: ${{ secrets.XPKG_ACCESS_ID }} |
| 152 | + password: ${{ secrets.XPKG_TOKEN }} |
| 153 | + |
| 154 | + # If a version wasn't explicitly passed as a workflow_dispatch input we |
| 155 | + # default to version v0.0.0-<git-commit-date>-<git-short-sha>, for example |
| 156 | + # v0.0.0-20231101115142-1091066df799. This is a simple implementation of |
| 157 | + # Go's pseudo-versions: https://go.dev/ref/mod#pseudo-versions. |
| 158 | + - name: Set Default Multi-Platform Package Version |
| 159 | + if: env.XPKG_VERSION == '' |
| 160 | + run: echo "XPKG_VERSION=v0.0.0-$(date -d@$(git show -s --format=%ct) +%Y%m%d%H%M%S)-$(git rev-parse --short=12 HEAD)" >> $GITHUB_ENV |
| 161 | + |
| 162 | + - name: Push Multi-Platform Package to Upbound |
| 163 | + if: env.XPKG_ACCESS_ID != '' |
| 164 | + run: "./crossplane --verbose xpkg push --package-files $(echo *.xpkg|tr ' ' ,) ${{ env.XPKG }}:${{ env.XPKG_VERSION }}" |
0 commit comments