From d6af2764a009026f73e77b1a1153c34820842b5b Mon Sep 17 00:00:00 2001 From: Javier Bullrich Date: Fri, 29 Sep 2023 14:53:47 +0200 Subject: [PATCH] created release file --- .github/workflows/release.yml | 118 ++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..b4d8cb9 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,118 @@ +name: Publish package to GitHub Packages +on: + push: + branches: + - main + pull_request: + +env: + IMAGE_NAME: action + +jobs: + test-image: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3.3.0 + - name: Check that the image builds + run: docker build . --file Dockerfile + validate-action: + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' + steps: + - uses: actions/checkout@v3.3.0 + # This checks that .github/workflows/review-bot.yml is pointing towards the main branch + # as, during development, we change this to use the code from the test branch and + # we may forget to set it back to main + - name: Validate that action points to main branch + run: | + BRANCH=$(yq '.jobs.review-approvals.steps[1].uses' $FILE_NAME | cut -d "@" -f2) + # If the branch is not the main branch + if [ "$BRANCH" != "$GITHUB_BASE_REF" ]; then + echo "Action points to $BRANCH. It has to point to $GITHUB_BASE_REF instead!" + exit 1 + else + echo "Action is correctly pointing to $GITHUB_BASE_REF" + fi + env: + FILE_NAME: ".github/workflows/review-bot.yml" + + test-versions: + runs-on: ubuntu-latest + outputs: + version: ${{ steps.verification.outputs.VERSION }} + steps: + - uses: actions/checkout@v3.3.0 + - name: Extract package.json version + id: package_version + run: echo "VERSION=$(jq '.version' -r package.json)" >> $GITHUB_OUTPUT + - name: Extract action.yml version + uses: mikefarah/yq@master + id: action_image + with: + cmd: yq '.runs.image' 'action.yml' + - name: Parse action.yml version + id: action_version + run: | + echo "IMAGE_VERSION=$(echo $IMAGE_URL | cut -d: -f3)" >> $GITHUB_OUTPUT + env: + IMAGE_URL: ${{ steps.action_image.outputs.result }} + - name: Compare versions + id: verification + if: steps.action_version.outputs.IMAGE_VERSION != 'Dockerfile' + run: | + echo "Verifying that $IMAGE_VERSION from action.yml is the same as $PACKAGE_VERSION from package.json" + [[ $IMAGE_VERSION == $PACKAGE_VERSION ]] + echo VERSION=v$VERSION >> $GITHUB_OUTPUT + env: + IMAGE_VERSION: ${{ steps.action_version.outputs.IMAGE_VERSION }} + PACKAGE_VERSION: ${{ steps.package_version.outputs.VERSION }} + + tag: + if: github.event_name == 'push' + needs: [test-image, test-versions] + runs-on: ubuntu-latest + permissions: + contents: write + outputs: + tagcreated: ${{ steps.autotag.outputs.tagcreated }} + tagname: ${{ steps.autotag.outputs.tagname }} + steps: + - uses: mukunku/tag-exists-action@v1.4.0 + id: checkTag + with: + tag: ${{ needs.test-versions.outputs.version }} + - run: echo ${{ steps.checkTag.outputs.exists }} + - name: Tag version and create release + if: steps.checkTag.outputs.exists == 'false' + run: gh release create $VERSION --generate-notes + env: + VERSION: ${{ needs.test-versions.outputs.version }} + publish: + runs-on: ubuntu-latest + permissions: + packages: write + needs: [tag, test-versions] + if: needs.tag.outputs.tagname != '' + steps: + - uses: actions/checkout@v3 + - name: Build image + run: docker build . --file Dockerfile --tag $IMAGE_NAME + - name: Log into registry + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin + - name: Push image + run: | + IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME + # Change all uppercase to lowercase + IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') + # Strip git ref prefix from version + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + # Strip "v" prefix from tag name + [[ ! -z $TAG ]] && VERSION=$(echo $TAG | sed -e 's/^v//') + # Use Docker `latest` tag convention + [ "$VERSION" == "main" ] && VERSION=latest + echo IMAGE_ID=$IMAGE_ID + echo VERSION=$VERSION + docker tag $IMAGE_NAME $IMAGE_ID:$VERSION + docker push $IMAGE_ID:$VERSION + env: + TAG: ${{ needs.test-versions.outputs.version }}