From 9969a2f3aac68e42d608a1c2bc6ccdaa61918f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Sat, 23 Oct 2021 11:56:11 +0200 Subject: [PATCH] task: split jobs into reusable actions --- .github/actions/build_site/action.yaml | 14 ++ .github/actions/check_milestone/action.yaml | 73 +++++++++++ .github/actions/deploy_site/action.yaml | 17 +++ .github/actions/release_milestone/action.yaml | 64 +++++++++ .github/workflows/build.yaml | 20 +++ .github/workflows/cicd.yaml | 33 ----- .github/workflows/deploy.yaml | 25 ++++ .github/workflows/release.yaml | 122 +++--------------- 8 files changed, 234 insertions(+), 134 deletions(-) create mode 100644 .github/actions/build_site/action.yaml create mode 100644 .github/actions/check_milestone/action.yaml create mode 100644 .github/actions/deploy_site/action.yaml create mode 100644 .github/actions/release_milestone/action.yaml create mode 100644 .github/workflows/build.yaml delete mode 100644 .github/workflows/cicd.yaml create mode 100644 .github/workflows/deploy.yaml diff --git a/.github/actions/build_site/action.yaml b/.github/actions/build_site/action.yaml new file mode 100644 index 0000000000..74efc99c0e --- /dev/null +++ b/.github/actions/build_site/action.yaml @@ -0,0 +1,14 @@ +name: Build site +description: Builds the Hugo exampleSite for later deploy on GitHub-Pages +runs: + using: composite + steps: + - name: Setup Hugo + uses: peaceiris/actions-hugo@v2 + with: + hugo-version: 'latest' + + - name: Build site + shell: bash + run: | + hugo --minify --source ${GITHUB_WORKSPACE}/exampleSite --destination ${GITHUB_WORKSPACE}/../public --cleanDestinationDir --gc --baseURL https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/ --theme ${GITHUB_WORKSPACE} diff --git a/.github/actions/check_milestone/action.yaml b/.github/actions/check_milestone/action.yaml new file mode 100644 index 0000000000..2dda0ec430 --- /dev/null +++ b/.github/actions/check_milestone/action.yaml @@ -0,0 +1,73 @@ +name: Check milestone +description: Checks if the given milestone and its according tag are valid to be released +inputs: + milestone: + description: Milestone for this release + required: true + github_token: + description: Secret GitHub token + required: true +outputs: + outcome: + description: Result of the check, success or failure + value: ${{ steps.outcome.outputs.outcome }} +runs: + using: composite + steps: + - name: Get tag uniqueness + id: unique_tag + uses: mukunku/tag-exists-action@v1.0.0 + with: + tag: ${{ env.MILESTONE }} + env: + MILESTONE: ${{ inputs.milestone }} + GITHUB_TOKEN: ${{ inputs.github_token }} + + - name: Get closed issues for milestone + id: closed_issues + uses: octokit/graphql-action@v2.x + with: + query: | + query { + search(first: 1, type: ISSUE, query: "user:${{ github.repository_owner }} repo:${{ github.event.repository.name }} milestone:${{ env.MILESTONE }} state:closed") { + issueCount + } + } + env: + MILESTONE: ${{ inputs.milestone }} + GITHUB_TOKEN: ${{ inputs.github_token }} + + - name: Get open issues for milestone + id: open_issues + uses: octokit/graphql-action@v2.x + with: + query: | + query { + search(first: 1, type: ISSUE, query: "user:${{ github.repository_owner }} repo:${{ github.event.repository.name }} milestone:${{ env.MILESTONE }} state:open") { + issueCount + } + } + env: + MILESTONE: ${{ inputs.milestone }} + GITHUB_TOKEN: ${{ inputs.github_token }} + + - name: Set outcome + id: outcome + shell: bash + run: | + if [ "${{ fromJSON(steps.closed_issues.outputs.data).search.issueCount > 0 && fromJSON(steps.open_issues.outputs.data).search.issueCount == 0 && steps.unique_tag.outputs.exists == 'false' }}" = "true" ]; then + echo "::set-output name=outcome::success" + else + echo "::set-output name=outcome::failure" + fi + + - name: Log results and exit + shell: bash + run: | + echo outcome : ${{ steps.outcome.outputs.outcome }} + echo has unique tag : ${{ steps.unique_tag.outputs.exists == 'false' }} + echo has closed issues: ${{ fromJSON(steps.closed_issues.outputs.data).search.issueCount > 0 }} + echo has open issues : ${{ fromJSON(steps.open_issues.outputs.data).search.issueCount > 0 }} + if [ "${{ steps.outcome.outputs.outcome }}" = "failure" ]; then + exit 1 + fi diff --git a/.github/actions/deploy_site/action.yaml b/.github/actions/deploy_site/action.yaml new file mode 100644 index 0000000000..b03cae0508 --- /dev/null +++ b/.github/actions/deploy_site/action.yaml @@ -0,0 +1,17 @@ +name: Deploy site +description: Deploys a built site on GitHub-Pages +inputs: + github_token: + description: Secret GitHub token + required: true +runs: + using: composite + steps: + - name: Deploy site + uses: peaceiris/actions-gh-pages@v3 + with: + github_token: ${{ env.GITHUB_TOKEN }} + publish_dir: ${{ env.GITHUB_WORKSPACE }}/../public + env: + GITHUB_TOKEN: ${{ inputs.github_token }} + GITHUB_WORKSPACE: ${{ github.workspace }} diff --git a/.github/actions/release_milestone/action.yaml b/.github/actions/release_milestone/action.yaml new file mode 100644 index 0000000000..a16e9fa05f --- /dev/null +++ b/.github/actions/release_milestone/action.yaml @@ -0,0 +1,64 @@ +name: Release milestone +description: Creates a release with changelog and tag out of a given milestone +inputs: + milestone: + description: Milestone for this release + required: true + github_token: + description: Secret GitHub token + required: true +runs: + using: composite + steps: + - name: Setup node + uses: actions/setup-node@v2 + with: + node-version: '16' + + - name: Close milestone + uses: Akkjon/close-milestone@v2 + with: + milestone_name: ${{ env.MILESTONE }} + env: + MILESTONE: ${{ inputs.milestone }} + GITHUB_TOKEN: ${{ inputs.github_token }} + + - name: Create provisionary tag + shell: bash + env: + MILESTONE: ${{ inputs.milestone }} + GITHUB_TOKEN: ${{ inputs.github_token }} + run: | + git config user.name "GitHub Actions Bot" + git config user.email "<>" + git tag --message "" "$MILESTONE" + git push origin "$MILESTONE" + + - name: Update changelog + shell: bash + env: + MILESTONE: ${{ inputs.milestone }} + GITHUB_TOKEN: ${{ inputs.github_token }} + GREN_GITHUB_TOKEN: ${{ inputs.github_token }} + run: | + npx github-release-notes@0.17.1 changelog --generate --override --tags=all + git add * + git commit --message "Ship tag $MILESTONE" + git push origin main + + - name: Create final tag + shell: bash + env: + MILESTONE: ${{ inputs.milestone }} + GITHUB_TOKEN: ${{ inputs.github_token }} + run: | + git tag --force --message "" "$MILESTONE" + git push --force origin "$MILESTONE" + + - name: Publish release + shell: bash + env: + MILESTONE: ${{ inputs.milestone }} + GREN_GITHUB_TOKEN: ${{ inputs.github_token }} + run: | + npx github-release-notes@0.17.1 release --tags "$MILESTONE" diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000000..fd9ce5cb0c --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,20 @@ +name: Build + +on: + push: # Build on all pushes but only deploy for main branch + pull_request: # Build on all PRs regardless what branch + workflow_dispatch: # Allow this task to be manually started (you'll never know) + +jobs: + ci: + name: Run build + runs-on: ubuntu-latest + if: github.event_name != 'push' || (github.event_name == 'push' && github.ref != 'refs/heads/main') + steps: + - name: Checkout repo + uses: actions/checkout@v2 + with: + submodules: true # Fetch Hugo themes (true OR recursive) + + - name: Build site + uses: ./.github/actions/build_site diff --git a/.github/workflows/cicd.yaml b/.github/workflows/cicd.yaml deleted file mode 100644 index 3f2c409825..0000000000 --- a/.github/workflows/cicd.yaml +++ /dev/null @@ -1,33 +0,0 @@ -name: CI/CD - -on: - push: # Build on all pushes but only deploy for main branch - pull_request: # Build on all PRs regardless what branch - workflow_dispatch: # Allow this task to be manually started (you'll never know) - -jobs: - ci: - name: Run CI/CD - runs-on: ubuntu-latest - steps: - - name: Checkout repo - uses: actions/checkout@v2 - with: - submodules: true # Fetch Hugo themes (true OR recursive) - fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod - - - name: Setup Hugo - uses: peaceiris/actions-hugo@v2 - with: - hugo-version: 'latest' - # extended: true - - - name: Build site - run: hugo --minify --source exampleSite --destination ../public --cleanDestinationDir --gc --baseURL https://mcshelby.github.io/hugo-theme-relearn/ - - - name: Deploy site - if: github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && github.ref == 'refs/heads/main') - uses: peaceiris/actions-gh-pages@v3 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - publish_dir: ./public diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml new file mode 100644 index 0000000000..2434748434 --- /dev/null +++ b/.github/workflows/deploy.yaml @@ -0,0 +1,25 @@ +name: Deploy + +on: + push: # Build on all pushes but only deploy for main branch + workflow_dispatch: # Allow this task to be manually started (you'll never know) + +jobs: + deploy: + name: Run deploy + runs-on: ubuntu-latest + if: github.event_name != 'push' || (github.event_name == 'push' && github.ref == 'refs/heads/main') + steps: + - name: Checkout repo + uses: actions/checkout@v2 + with: + submodules: true # Fetch Hugo themes (true OR recursive) + fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod + + - name: Build site + uses: ./.github/actions/build_site + + - name: Deploy site + uses: ./.github/actions/deploy_site + with: + github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 72ffac74c3..1d32b597c2 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -8,115 +8,35 @@ on: required: true jobs: - release_state: - name: Get tag and milestone state - runs-on: ubuntu-latest - env: - MILESTONE: ${{ github.event.inputs.milestone }} # To avoid code injection - outputs: - has_closed_issues: ${{ fromJSON(steps.closed_issues.outputs.data).search.issueCount > 0 }} - has_open_issues: ${{ fromJSON(steps.open_issues.outputs.data).search.issueCount > 0 }} - has_unique_tag: ${{ steps.unique_tag.outcome == 'failure' }} - steps: - - name: Checkout repo - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - - name: Get closed issues for milestone - id: closed_issues - uses: octokit/graphql-action@v2.x - with: - query: | - query { - search(first: 1, type: ISSUE, query: "user:${{ github.repository_owner }} repo:${{ github.event.repository.name }} milestone:${{ env.MILESTONE }} state:closed") { - issueCount - } - } - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get open issues for milestone - id: open_issues - uses: octokit/graphql-action@v2.x - with: - query: | - query { - search(first: 1, type: ISSUE, query: "user:${{ github.repository_owner }} repo:${{ github.event.repository.name }} milestone:${{ env.MILESTONE }} state:open") { - issueCount - } - } - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get tag uniqueness - id: unique_tag - continue-on-error: true - run: git rev-parse "$MILESTONE" -- - - release_log: - name: Log tag and milestone state - needs: release_state - runs-on: ubuntu-latest - env: - MILESTONE: ${{ github.event.inputs.milestone }} # To avoid code injection - steps: - - run: "echo has unique tag : ${{ needs.release_state.outputs.has_unique_tag }}" - - run: "echo has closed issues: ${{ needs.release_state.outputs.has_closed_issues }}" - - run: "echo has open issues : ${{ needs.release_state.outputs.has_open_issues }}" - release: - name: Create release - needs: release_state - if: needs.release_state.outputs.has_closed_issues == 'true' && needs.release_state.outputs.has_open_issues == 'false' && needs.release_state.outputs.has_unique_tag == 'true' + name: Run release runs-on: ubuntu-latest - env: - MILESTONE: ${{ github.event.inputs.milestone }} # To avoid code injection - GREN_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: - name: Checkout repo uses: actions/checkout@v2 - - - name: Setup node - uses: actions/setup-node@v2 with: - node-version: '16' + submodules: true # Fetch Hugo themes (true OR recursive) + fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod - - name: Setup git env - run: | - git config user.name "GitHub Actions Bot" - git config user.email "<>" - - - name: Close milestone - uses: Akkjon/close-milestone@v2 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Check milestone + id: check + uses: ./.github/actions/check_milestone with: - milestone_name: ${{ env.MILESTONE }} + milestone: ${{ github.event.inputs.milestone }} + github_token: ${{ secrets.GITHUB_TOKEN }} - - name: Create provisionary tag - run: git tag --message "" "$MILESTONE" + - name: Create release + if: ${{ steps.check.outputs.outcome == 'success' }} + uses: ./.github/actions/release_milestone + with: + milestone: ${{ github.event.inputs.milestone }} + github_token: ${{ secrets.GITHUB_TOKEN }} - - name: Push provisionary tag - run: git push origin "$MILESTONE" + # We need to deploy the site again to show the updated changelog + - name: Build site + uses: ./.github/actions/build_site - - name: Update changelog - run: npx github-release-notes@0.17.1 changelog --generate --override --tags=all - - - name: Stage changelog - run: git add * - - - name: Commit changelog - run: git commit --message "Ship tag $MILESTONE" - - - name: Push changelog - run: git push origin main - - - name: Create final tag - run: git tag --force --message "" "$MILESTONE" - - - name: Push final tag - run: git push --force origin "$MILESTONE" - - - name: Publish release - run: npx github-release-notes@0.17.1 release --tags "$MILESTONE" + - name: Deploy site + uses: ./.github/actions/deploy_site + with: + github_token: ${{ secrets.GITHUB_TOKEN }}