From 1c89044be4bfdcddfaf1bd49b5a86594c9a6f1c6 Mon Sep 17 00:00:00 2001 From: Hao Liu Date: Wed, 25 Oct 2023 13:40:32 -0400 Subject: [PATCH] automate awx-operator publishing on operatorhub --- .github/workflows/publish-operator-hub.yaml | 66 +++++++++++++++ Makefile | 5 ++ hack/publish-to-operator-hub.sh | 90 +++++++++++---------- 3 files changed, 118 insertions(+), 43 deletions(-) create mode 100644 .github/workflows/publish-operator-hub.yaml diff --git a/.github/workflows/publish-operator-hub.yaml b/.github/workflows/publish-operator-hub.yaml new file mode 100644 index 00000000..d0ea5c29 --- /dev/null +++ b/.github/workflows/publish-operator-hub.yaml @@ -0,0 +1,66 @@ +name: Publish AWX Operator on operator-hub +on: + release: + types: [published] + workflow_dispatch: + inputs: + tag_name: + description: 'Name for the tag of the release.' + required: true + operator_hub_fork: + description: 'Fork of operator-hub where the PR will be created from. default: awx-auto' + required: true + default: 'awx-auto' + image_registry: + description: 'Image registry where the image is published to. default: quay.io' + required: true + default: 'quay.io' + image_registry_organization: + description: 'Image registry organization where the image is published to. default: ansible' + required: true + default: 'ansible' + community_operator_github_org: + description: 'Github organization for community-opeartor project. default: k8s-operatorhub' + required: true + default: 'k8s-operatorhub' + community_operator_prod_github_org: + description: 'GitHub organization for community-operator-prod project. default: redhat-openshift-ecosystem' + required: true + default: 'redhat-openshift-ecosystem' + +jobs: + promote: + runs-on: ubuntu-latest + steps: + - name: Set TAG_NAME from workflow_dispatch event + if: ${{ github.event_name == 'workflow_dispatch' }} + run: | + echo "TAG_NAME=${{ github.event.inputs.tag_name }}" >> $GITHUB_ENV + + - name: Set TAG_NAME for release event + if: ${{ github.event_name == 'release' }} + run: | + echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV + + - name: Log in to image registry + run: | + echo ${{ secrets.QUAY_TOKEN }} | docker login ${{github.event.inputs.image_registry}} -u ${{ secrets.QUAY_USER }} --password-stdin + + - name: Checkout awx-opearator at TAG_NAME + uses: actions/checkout@v4 + with: + fetch-tags: true + ref: ${{ env.TAG_NAME }} + path: awx-operator-${{ env.TAG_NAME }} + + - name: Build and publish bundle to operator-hub + working-directory: awx-operator-${{ env.TAG_NAME }} + env: + IMG_REPOSITORY: ${{ github.event.inputs.image_registry }}/${{ github.event.inputs.image_registry_organization }} + GITHUB_TOKEN: ${{ secrets.AWX_AUTO_GITHUB_TOKEN }} + COMMUNITY_OPERATOR_GITHUB_ORG: ${{ github.event.inputs.community_operator_github_org }} + COMMUNITY_OPERATOR_PROD_GITHUB_ORG: ${{ github.event.inputs.community_operator_prod_github_org }} + run: | + git config --global user.email "awx-automation@redhat.com" + git config --global user.name "AWX Automation" + ./hack/publish-to-operator-hub.sh diff --git a/Makefile b/Makefile index 4ff8dca7..25334a75 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ # - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2) # - use environment variables to overwrite this value (e.g export VERSION=0.0.2) VERSION ?= $(shell git describe --tags) +PREV_VERSION ?= $(git describe --abbrev=0 --tags $(git rev-list --tags --skip=1 --max-count=1)) CONTAINER_CMD ?= docker @@ -88,6 +89,10 @@ all: docker-build help: ## Display this help. @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) +.PHONY: print-% +print-%: ## Print any variable from the Makefile. Use as `make print-VARIABLE` + @echo $($*) + ##@ Build .PHONY: run diff --git a/hack/publish-to-operator-hub.sh b/hack/publish-to-operator-hub.sh index b2badb1b..52626edb 100755 --- a/hack/publish-to-operator-hub.sh +++ b/hack/publish-to-operator-hub.sh @@ -19,34 +19,33 @@ # # Remember to change update the VERSION and PREV_VERSION before running!!! - set -e -VERSION=${VERSION:-blah2} -PREV_VERSION=${PREV_VERSION:-blah1} +VERSION=${VERSION:-$(make print-VERSION)} +PREV_VERSION=${PREV_VERSION:-$(make print-PREV_VERSION)} + BRANCH=publish-awx-operator-$VERSION -FORK=${FORK:-fork} +FORK=${FORK:-awx-auto} +GITHUB_TOKEN=${GITHUB_TOKEN:-$AWX_AUTO_GITHUB_TOKEN} -IMG=quay.io/ansible/awx-operator:$VERSION -CATALOG_IMG=quay.io/ansible/awx-operator-catalog:$VERSION -BUNDLE_IMG=quay.io/ansible/awx-operator-bundle:$VERSION +IMG_REPOSITORY=${IMG_REPOSITORY:-quay.io/ansible} -# Set path variables -OPERATOR_PATH=${OPERATOR_PATH:-~/awx-operator} +OPERATOR_IMG=$IMG_REPOSITORY/awx-operator:$VERSION +CATALOG_IMG=$IMG_REPOSITORY/awx-operator-catalog:$VERSION +BUNDLE_IMG=$IMG_REPOSITORY/awx-operator-bundle:$VERSION -# Build & Push Operator Image # Not needed because it is done as part of the GHA release automation -# make docker-build docker-push IMG=$IMG +COMMUNITY_OPERATOR_GITHUB_ORG=${COMMUNITY_OPERATOR_GITHUB_ORG:-k8s-operatorhub} +COMMUNITY_OPERATOR_PROD_GITHUB_ORG=${COMMUNITY_OPERATOR_PROD_GITHUB_ORG:-redhat-openshift-ecosystem} # Build bundle directory -rm -rf bundle/ -make bundle IMG=$IMG +make bundle IMG=$OPERATOR_IMG # Build bundle and catalog images -make bundle-build bundle-push BUNDLE_IMG=$BUNDLE_IMG IMG=$IMG -make catalog-build catalog-push CATALOG_IMG=$CATALOG_IMG BUNDLE_IMGS=$BUNDLE_IMG BUNDLE_IMG=$BUNDLE_IMG IMG=$IMG +make bundle-build bundle-push BUNDLE_IMG=$BUNDLE_IMG IMG=$OPERATOR_IMG +make catalog-build catalog-push CATALOG_IMG=$CATALOG_IMG BUNDLE_IMGS=$BUNDLE_IMG BUNDLE_IMG=$BUNDLE_IMG IMG=$OPERATOR_IMG # Set containerImage & namespace variables in CSV -sed -i.bak -e "s|containerImage: quay.io/ansible/awx-operator:devel|containerImage: quay.io/ansible/awx-operator:${VERSION}|g" bundle/manifests/awx-operator.clusterserviceversion.yaml +sed -i.bak -e "s|containerImage: quay.io/ansible/awx-operator:devel|containerImage: ${OPERATOR_IMG}|g" bundle/manifests/awx-operator.clusterserviceversion.yaml sed -i.bak -e "s|namespace: placeholder|namespace: awx|g" bundle/manifests/awx-operator.clusterserviceversion.yaml # Add replaces to dependency graph for upgrade path @@ -67,17 +66,13 @@ fi # Remove .bak files from bundle result from sed commands find bundle -name "*.bak" -type f -delete -# -- Put up community-operators PR -cd $OPERATOR_PATH -git clone git@github.com:k8s-operatorhub/community-operators.git +echo "-- Create branch on community-operators fork --" +git clone https://github.com/$COMMUNITY_OPERATOR_GITHUB_ORG/community-operators.git mkdir -p community-operators/operators/awx-operator/$VERSION/ cp -r bundle/* community-operators/operators/awx-operator/$VERSION/ -cd community-operators/operators/awx-operator/$VERSION/ -pwd -ls -la +pushd community-operators/operators/awx-operator/$VERSION/ -# Commit and push PR git checkout -b $BRANCH git add ./ git status @@ -86,26 +81,26 @@ message='operator [N] [CI] awx-operator' commitMessage="${message} ${VERSION}" git commit -m "$commitMessage" -s -git remote add upstream git@github.com:$FORK/community-operators.git +git remote add upstream https://$GITHUB_TOKEN@github.com/$FORK/community-operators.git + +git push upstream --delete $BRANCH || true git push upstream $BRANCH +gh pr create \ + --title "operator awx-operator (${VERSION})" \ + --body "operator awx-operator (${VERSION})" \ + --base main \ + --head $FORK:$BRANCH \ + --repo $COMMUNITY_OPERATOR_GITHUB_ORG/community-operators +popd -# -- Put up community-operators-prod PR -# Reset directory -cd $OPERATOR_PATH - -pwd - -git clone git@github.com:redhat-openshift-ecosystem/community-operators-prod.git +echo "-- Create branch on community-operators-prod fork --" +git clone https://github.com/$COMMUNITY_OPERATOR_PROD_GITHUB_ORG/community-operators-prod.git mkdir -p community-operators-prod/operators/awx-operator/$VERSION/ cp -r bundle/* community-operators-prod/operators/awx-operator/$VERSION/ -cd community-operators-prod/operators/awx-operator/$VERSION/ +pushd community-operators-prod/operators/awx-operator/$VERSION/ -pwd -ls -la - -# Commit and push PR git checkout -b $BRANCH git add ./ git status @@ -114,15 +109,24 @@ message='operator [N] [CI] awx-operator' commitMessage="${message} ${VERSION}" git commit -m "$commitMessage" -s -git remote add upstream git@github.com:$FORK/community-operators-prod.git +git remote add upstream https://$GITHUB_TOKEN@github.com/$FORK/community-operators-prod.git + +git push upstream --delete $BRANCH || true git push upstream $BRANCH +gh pr create \ + --title "operator awx-operator (${VERSION})" \ + --body "operator awx-operator (${VERSION})" \ + --base main \ + --head $FORK:$BRANCH \ + --repo $COMMUNITY_OPERATOR_PROD_GITHUB_ORG/community-operators-prod +popd -# -- Print Links to Branches -echo "Commnity Operators: https://github.com/$FORK/community-operators/pull/new/$BRANCH" -echo "Commnity Operators Prod: https://github.com/$FORK/community-operators-prod/pull/new/$BRANCH" +# # -- Print Links to Branches +# echo "Commnity Operators: https://github.com/$FORK/community-operators/pull/new/$BRANCH" +# echo "Commnity Operators Prod: https://github.com/$FORK/community-operators-prod/pull/new/$BRANCH" # -- Cleanup - -rm -rf $OPERATOR_PATH/community-operators -rm -rf $OPERATOR_PATH/community-operators-prod +# rm -rf community-operators +# rm -rf community-operators-prod +# rm -rf bundle