From cabad538b7b2c66b80108776ca22acd89fcd1aaa Mon Sep 17 00:00:00 2001 From: Maximiliano Garcia Silva <54607954+mrfussion@users.noreply.github.com> Date: Wed, 28 Aug 2024 11:24:44 -0300 Subject: [PATCH] Create action --- .github/worflows/create_test_env.yaml | 47 -------------------------- .github/workflows/create_test_env.yaml | 47 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 47 deletions(-) delete mode 100644 .github/worflows/create_test_env.yaml create mode 100644 .github/workflows/create_test_env.yaml diff --git a/.github/worflows/create_test_env.yaml b/.github/worflows/create_test_env.yaml deleted file mode 100644 index 8a33c71..0000000 --- a/.github/worflows/create_test_env.yaml +++ /dev/null @@ -1,47 +0,0 @@ ---- -name: Create test environment from PR comment - -on: - pull_request_review_comment: - types: [created] - -jobs: - execute_script: - runs-on: ubuntu-latest - environment: DEV - - steps: - - name: Check if comment contains "create test env" - if: contains(github.event.comment.body, 'create test env') - run: echo "Comment contains 'create test env'" - - - name: Extract branch variable from comment - if: contains(github.event.comment.body, 'create test env') - run: | - echo "${{ github.event.comment.body }}" | grep -oP 'brand:\s*\K\w+' > branch.txt - id: extract_branch - - - name: Read extracted branch - if: steps.extract_brand.outputs.branch - run: | - branch=$(cat branch.txt) - echo "Branch is: $branch" - id: read_branch - - - name: Execute Script on Remote Server - if: contains(github.event.comment.body, 'create test env') - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.TEST_ENV.SSH_HOST }} - username: ${{ secrets.TEST_ENV.SSH_USER }} - key: ${{ secrets.TEST_ENV.SSH_KEY }} - script: | - BRANCH=${{ steps.read_branch.outputs.branch }} - HOST=${{ secrets.TEST_ENV.SSH_HOST }} - USER=${{ secrets.TEST_ENV.SSH_USER }} - KEY=${{ secrets.TEST_ENV.SSH_KEY }} - echo "branch: $BRANCH - HOST: $HOST - USER: $USER - KEY: $KEY" - #sudo BRANCH=${{ steps.read_branch.outputs.brand }} /path/to/pepe.sh diff --git a/.github/workflows/create_test_env.yaml b/.github/workflows/create_test_env.yaml new file mode 100644 index 0000000..0ed8c86 --- /dev/null +++ b/.github/workflows/create_test_env.yaml @@ -0,0 +1,47 @@ +--- +name: Create test environment from PR comment + +on: + issue_comment: + types: [created] + +jobs: + execute_script: + runs-on: ubuntu-latest + environment: DEV + timeout-minutes: 10 + + steps: + - name: Extract and set branch variable from comment + if: contains(github.event.comment.body, 'create testenv ') && github.event.comment.body != 'create testenv' + run: | + echo "branch=$(echo '${{ github.event.comment.body }}' | grep -oP '(?<=create testenv ).*')" >> $GITHUB_ENV + + - name: Execute Script on Remote Server + if: ${{ env.branch != '' }} + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.TEST_ENV_SSH_HOST }} + username: ${{ secrets.TEST_ENV_SSH_USER }} + key: ${{ secrets.TEST_ENV_SSH_KEY }} + script: | + eval "$(ssh-agent -s)" + ssh-add ~/.ssh/actions_keys/actions@assek + export TERM=xterm-256color + BRANCH=${{ env.branch }} + SUDO_PWD=${{ secrets.TEST_ENV_SUDO_PWD }} + echo $SUDO_PWD | sudo -S -E /srv/anet_testing/odoo/tools/test_environment/deploy.sh create $BRANCH | tee script_output.log + + - name: Extract last 5 lines of output + run: | + tail -n 5 script_output.log > last_lines.txt + echo "LAST_LINES=$(cat last_lines.txt)" >> $GITHUB_ENV + + - name: Comment on the PR + if: success() + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + curl -X POST -H "Authorization: token $GITHUB_TOKEN" \ + -d '{"body": "Action completed successfully. Here are the last 5 lines of output:\n```\n'${LAST_LINES}'\n```"}' \ + ${{ github.event.issue.url }}/comments