From d22aebaa0a4cb7331ab4102945585dbec096742e Mon Sep 17 00:00:00 2001 From: Jordan Barrett Date: Wed, 18 Oct 2023 12:47:02 +0700 Subject: [PATCH] Fix CI - upload charm to temporary branch for testing - cross-release to edge on push - Add separate CI job to select Charmhub channels - Upload the charm in a separate job, outside of the integration tests. - Rename the `bootstrap` job to `integration`. - Disable CI runs on `pull_request` - Introduce a `CHARM_NAME` variable, which can be set on a fork to upload the charm to a different URL. - Allow overriding the charm name in the `charmcraft_tokengen.sh` script --- .github/charmcraft-tokengen.sh | 3 +- .github/workflows/ci.yml | 144 +++++++++++++++++++++------------ 2 files changed, 94 insertions(+), 53 deletions(-) diff --git a/.github/charmcraft-tokengen.sh b/.github/charmcraft-tokengen.sh index a6b7e23..06b82ab 100755 --- a/.github/charmcraft-tokengen.sh +++ b/.github/charmcraft-tokengen.sh @@ -2,8 +2,9 @@ # Generate a Charmcraft token to use in the CI pipeline # The token will be outputted to the file 'charmcraft_token' # It should be added as a GitHub secret under the name 'CHARMCRAFT_AUTH' +CHARM_NAME=${CHARM_NAME:-juju-controller} charmcraft login --export=charmcraft_token \ - --charm=juju-qa-controller \ + --charm="$CHARM_NAME" \ --permission=package-manage-releases \ --permission=package-manage-revisions \ --permission=package-view \ diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 64d7d1a..db6bf4f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,8 +1,13 @@ name: "CI" +# We don't run this workflow on 'pull_request', because we require secrets to +# upload the charm to Charmhub, and pull_request runs can't access secrets. +# PRs should be opened from a branch on the main juju/juju-controller repo, +# not from a fork. on: push: - pull_request: workflow_dispatch: +env: + CHARM_NAME: ${{ vars.CHARM_NAME }} jobs: @@ -33,18 +38,77 @@ jobs: with: artifact-name: charm-packed - bootstrap: - name: "Bootstrap" + + channel: + name: Select Charmhub channel + runs-on: ubuntu-latest + outputs: + test: ${{ steps.channel.outputs.test }} + release: ${{ steps.channel.outputs.release }} + + steps: + - name: Select Charmhub channel + id: channel + shell: bash + run: | + set -eux + case ${{ github.ref_name }} in + 3.* | 4.*) + TRACK="${{ github.ref_name }}" + DO_RELEASE=true + ;; + main) + TRACK="latest" + DO_RELEASE=true + ;; + *) + TRACK="latest" + DO_RELEASE=false # Don't release feature branches + ;; + esac + + # Feature branches will be released to the 'latest' track, so we need + # to include the branch name to disambiguate. + BRANCH="${{ github.ref_name }}-${{ github.sha }}" + + echo "test=$TRACK/edge/$BRANCH" >> "$GITHUB_OUTPUT" + if [[ "$DO_RELEASE" == 'true' ]]; then + echo "release=$TRACK/edge" >> "$GITHUB_OUTPUT" + fi + + + upload: + name: Upload to Charmhub + needs: [build, channel] + runs-on: ubuntu-latest + + steps: + - name: Download packed charm + id: download + uses: actions/download-artifact@v3 + with: + name: ${{ needs.build.outputs.artifact-name }} + + - name: Upload charm to Charmhub + env: + CHARMCRAFT_AUTH: ${{ secrets.CHARMCRAFT_AUTH }} + run: | + sudo snap install charmcraft --classic + charmcraft upload ${{ steps.download.outputs.download-path }}/*.charm \ + --name $CHARM_NAME \ + --release ${{ needs.channel.outputs.test }} + + + integration: + name: "Integration tests" runs-on: ubuntu-latest - needs: build + needs: [build, upload, channel] strategy: fail-fast: false matrix: cloud: ["lxd", "microk8s"] env: LOCAL_CHARM_PATH: ${{ github.workspace }}/controller.charm - CHARMHUB_NAME: juju-qa-controller - CHARMHUB_CHANNEL: latest/edge/${{ github.run_id }} steps: - name: Download packed charm @@ -58,17 +122,6 @@ jobs: mv ${{ steps.download.outputs.download-path }}/*.charm \ $LOCAL_CHARM_PATH - # Currently the only way to get charms on k8s is via Charmhub. - - name: Upload charm to Charmhub - id: charmcraft - if: matrix.cloud == 'microk8s' - env: - CHARMCRAFT_AUTH: ${{ secrets.CHARMCRAFT_AUTH }} - run: | - sudo snap install charmcraft --classic - charmcraft upload $LOCAL_CHARM_PATH \ - --name $CHARMHUB_NAME --release $CHARMHUB_CHANNEL - - name: Save charmcraft logs as artifact if: always() && steps.charmcraft.outcome != 'skipped' uses: actions/upload-artifact@v3 @@ -103,8 +156,8 @@ jobs: run: | sg snap_microk8s <> "$GITHUB_OUTPUT" - - if [[ -z $TRACK ]]; then - echo "upload=false" >> "$GITHUB_OUTPUT" - else - echo "upload=true" >> "$GITHUB_OUTPUT" - fi - + TRACK=$(echo $CHANNEL | cut -d '/' -f 1) + REVISION=$(charmcraft status $CHARM_NAME --format json | + jq ".[] | select(.track == \"$TRACK\") | .mappings[0].releases[] | select(.channel == \"$CHANNEL\") | .revision") + echo "revision=$REVISION" >> "$GITHUB_OUTPUT" - - name: Upload to Charmhub - if: steps.channel.outputs.upload == 'true' - env: - CHARMCRAFT_AUTH: ${{ secrets.CHARMCRAFT_AUTH }} + - name: Release to edge + if: github.event_name == 'push' && needs.channel.outputs.release != '' run: | - sudo snap install charmcraft --classic - charmcraft upload ${{ steps.download.outputs.download-path }}/*.charm \ - --release ${{ steps.channel.outputs.track }}/edge + charmcraft release $CHARM_NAME \ + --revision=${{ steps.revision.outputs.revision }} \ + --channel=${{ needs.channel.outputs.release }}