From 70672b366d7c199b4f163319eb653e0703c598b8 Mon Sep 17 00:00:00 2001 From: s3rj1k Date: Wed, 10 Apr 2024 21:30:23 +0200 Subject: [PATCH] Extract meta-repo code into `meta-repo-content-push`. --- .github/actions/meta-repo/action.yml | 75 +++++++++++++++++++ .../cicd-docker-build-and-distribute.yml | 60 ++++----------- .github/workflows/meta-repo-content.yml | 68 +++++++++++++++++ 3 files changed, 158 insertions(+), 45 deletions(-) create mode 100644 .github/actions/meta-repo/action.yml create mode 100644 .github/workflows/meta-repo-content.yml diff --git a/.github/actions/meta-repo/action.yml b/.github/actions/meta-repo/action.yml new file mode 100644 index 00000000..9c0b934f --- /dev/null +++ b/.github/actions/meta-repo/action.yml @@ -0,0 +1,75 @@ +name: Push data to meta-repo + +inputs: + META_CONTENT: + required: true + default: '' + description: Data to put into `metafile.txt` + META_REPO: + required: true + default: '' + description: Target meta repo to sync metadata changes + META_REPO_BRANCH: + required: true + default: '' + description: Target meta repo branch name + META_REPO_DEFAULT_BRANCH: + required: false + default: 'main' + description: Target meta repo default branch name + +runs: + using: "composite" + steps: + + - name: Encode '${{ inputs.META_REPO_BRANCH }}' and write to ENV + id: encode-meta-repo-branch + shell: bash + run: | + echo "META_REPO_BRANCH_B64=$(echo -n ${{ inputs.META_REPO_BRANCH }} | base64)" | tee -a "${GITHUB_ENV}" + echo "meta-repo-branch-b64=$(echo -n ${{ inputs.META_REPO_BRANCH }} | base64)" | tee -a "${GITHUB_OUTPUT}" + + - name: Checkout metadata repo (${{ inputs.META_REPO_BRANCH }}) + uses: actions/checkout@v4 + id: meta-branch-exists + continue-on-error: true + with: + repository: ${{ inputs.META_REPO }} + ref: ${{ inputs.META_REPO_BRANCH }} + token: ${{ env.GH_BOT_DEPLOY_TOKEN || github.token }} + fetch-depth: 1 + path: ${{ steps.encode-meta-repo-branch.outputs.meta-repo-branch-b64 }} + + - name: Checkout metadata repo (${{ inputs.META_REPO_BRANCH }}) + uses: actions/checkout@v4 + if: steps.meta-branch-exists.outcome != 'success' + with: + repository: ${{ inputs.META_REPO }} + ref: ${{ inputs.META_REPO_DEFAULT_BRANCH }} + token: ${{ env.GH_BOT_DEPLOY_TOKEN || github.token }} + fetch-depth: 1 + path: ${{ steps.encode-meta-repo-branch.outputs.meta-repo-branch-b64 }} + clean: true + + - name: Create remote branch (${{ inputs.META_REPO_BRANCH }}) + if: steps.meta-branch-exists.outcome != 'success' + shell: bash + working-directory: ${{ steps.encode-meta-repo-branch.outputs.meta-repo-branch-b64 }} + run: | + git checkout -b ${{ inputs.META_REPO_BRANCH }} + git push --set-upstream origin ${{ inputs.META_REPO_BRANCH }} + + - name: Push changes + shell: bash + working-directory: ${{ steps.encode-meta-repo-branch.outputs.meta-repo-branch-b64 }} + run: | + git config --global user.email "github-actions@github.com" && \ + git config --global user.name "github-actions" + + echo "${{ inputs.META_CONTENT }}" | tee metafile.txt + + git status --porcelain | grep -q . || exit 0 + + git add -v metafile.txt && \ + git commit --branch -m "update metadata" && \ + git push --atomic -v diff --git a/.github/workflows/cicd-docker-build-and-distribute.yml b/.github/workflows/cicd-docker-build-and-distribute.yml index 15d58af1..930c8cae 100644 --- a/.github/workflows/cicd-docker-build-and-distribute.yml +++ b/.github/workflows/cicd-docker-build-and-distribute.yml @@ -183,53 +183,23 @@ jobs: environment: ${{ inputs.ENVIRONMENT }} steps: - - name: Encode '${{ inputs.META_REPO_BRANCH }}' and write to ENV - id: encode-meta-repo-branch - shell: bash - run: | - echo "META_REPO_BRANCH_B64=$(echo -n ${{ inputs.META_REPO_BRANCH }} | base64)" | tee -a "${GITHUB_ENV}" - echo "meta-repo-branch-b64=$(echo -n ${{ inputs.META_REPO_BRANCH }} | base64)" | tee -a "${GITHUB_OUTPUT}" - - - name: Checkout metadata repo (${{ inputs.META_REPO_BRANCH }}) + - name: Checkout reusable actions uses: actions/checkout@v4 - id: meta-branch-exists - continue-on-error: true with: - repository: ${{ inputs.META_REPO }} - ref: ${{ inputs.META_REPO_BRANCH }} - token: ${{ secrets.GH_BOT_DEPLOY_TOKEN || github.token }} + repository: signalwire/actions-template + ref: master fetch-depth: 1 - path: ${{ steps.encode-meta-repo-branch.outputs.meta-repo-branch-b64 }} + path: actions + sparse-checkout: | + .github/actions/meta-repo/action.yml + sparse-checkout-cone-mode: false - - name: Checkout metadata repo (${{ inputs.META_REPO_BRANCH }}) - uses: actions/checkout@v4 - if: steps.meta-branch-exists.outcome != 'success' + - name: Push build specific data to meta-repo + uses: ./actions/.github/actions/meta-repo with: - repository: ${{ inputs.META_REPO }} - ref: ${{ inputs.META_REPO_DEFAULT_BRANCH }} - token: ${{ secrets.GH_BOT_DEPLOY_TOKEN || github.token }} - fetch-depth: 1 - path: ${{ steps.encode-meta-repo-branch.outputs.meta-repo-branch-b64 }} - clean: true - - - name: Create remote branch (${{ inputs.META_REPO_BRANCH }}) - if: steps.meta-branch-exists.outcome != 'success' - working-directory: ${{ steps.encode-meta-repo-branch.outputs.meta-repo-branch-b64 }} - run: | - git checkout -b ${{ inputs.META_REPO_BRANCH }} - git push --set-upstream origin ${{ inputs.META_REPO_BRANCH }} - - - name: Push changes - shell: bash - working-directory: ${{ steps.encode-meta-repo-branch.outputs.meta-repo-branch-b64 }} - run: | - git config --global user.email "github-actions@github.com" && \ - git config --global user.name "github-actions" - - echo "${{ inputs.META_FILE_PATH_PREFIX }}" | tee metafile.txt - - git status --porcelain | grep -q . || exit 0 - - git add -v metafile.txt && \ - git commit --branch -m "update metadata" && \ - git push --atomic -v + META_CONTENT: ${{ inputs.META_FILE_PATH_PREFIX }} + META_REPO: ${{ inputs.META_REPO }} + META_REPO_BRANCH: ${{ inputs.META_REPO_BRANCH }} + META_REPO_DEFAULT_BRANCH: ${{ inputs.META_REPO_DEFAULT_BRANCH }} + env: + GH_BOT_DEPLOY_TOKEN: ${{ secrets.GH_BOT_DEPLOY_TOKEN }} diff --git a/.github/workflows/meta-repo-content.yml b/.github/workflows/meta-repo-content.yml new file mode 100644 index 00000000..24d1c5be --- /dev/null +++ b/.github/workflows/meta-repo-content.yml @@ -0,0 +1,68 @@ +name: Push data to meta-repo + +on: + workflow_call: + inputs: + ENVIRONMENT: + required: false + type: string + description: Environment where the variables and secrets are scoped to + RUNNER: + required: false + default: ubuntu-latest + description: A GitHub runner type + type: string + META_CONTENT: + required: true + type: string + default: '' + description: Data to put into `metafile.txt` + META_REPO: + required: true + type: string + default: '' + description: Target meta repo to sync metadata changes + META_REPO_BRANCH: + required: true + type: string + default: '' + description: Target meta repo branch name + META_REPO_DEFAULT_BRANCH: + required: false + type: string + default: 'main' + description: Target meta repo default branch name + + secrets: + GH_BOT_DEPLOY_TOKEN: + required: true + +jobs: + meta-repo-content-push: + runs-on: ${{ inputs.RUNNER }} + permissions: + contents: read + id-token: write + environment: ${{ inputs.ENVIRONMENT }} + steps: + + - name: Checkout reusable actions + uses: actions/checkout@v4 + with: + repository: signalwire/actions-template + ref: master + fetch-depth: 1 + path: actions + sparse-checkout: | + .github/actions/meta-repo/action.yml + sparse-checkout-cone-mode: false + + - name: Push build specific data to meta-repo + uses: ./actions/.github/actions/meta-repo + with: + META_CONTENT: ${{ inputs.META_CONTENT }} + META_REPO: ${{ inputs.META_REPO }} + META_REPO_BRANCH: ${{ inputs.META_REPO_BRANCH }} + META_REPO_DEFAULT_BRANCH: ${{ inputs.META_REPO_DEFAULT_BRANCH }} + env: + GH_BOT_DEPLOY_TOKEN: ${{ secrets.GH_BOT_DEPLOY_TOKEN }}