From 778be829a40249b2e350067b6b2f857c1728e48a Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 27 Feb 2024 16:36:35 +1100 Subject: [PATCH] gha 5 --- .circleci/config.yml | 31 +++++++-- .github/workflows/test-php.yml | 119 +++++++++++++++++++++++++++++++-- 2 files changed, 140 insertions(+), 10 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index bec2526..263b213 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -32,15 +32,31 @@ jobs: steps: - attach_workspace: at: /workspace + - checkout + - add_ssh_keys: fingerprints: - *deploy_ssh_fingerprint + - *step_setup_git - - run: composer validate --ansi --strict - - run: composer install - - run: composer lint - - run: composer test + + - run: + name: Validate composer.json + command: composer validate --ansi --strict + + - run: + name: Install dependencies + command: composer install --ansi --no-progress --no-interaction --no-suggest --prefer-dist + + - run: + name: Lint code + command: composer lint + + - run: + name: Run tests + command: composer test + - persist_to_workspace: root: /workspace paths: @@ -52,10 +68,13 @@ jobs: steps: - attach_workspace: at: /workspace + - add_ssh_keys: fingerprints: - *deploy_ssh_fingerprint + - *step_setup_git + - checkout # Test file will have a consistent name between deployments, but @@ -108,7 +127,6 @@ jobs: echo echo "See https://github.com/drevops/git-artifact-destination/blob/$DEPLOYED_BRANCH/$TEST_FILE" - # Demonstration of deployment in 'branch' mode. # Note that by design, pushing into the same branch will result in the failure # of the second push. This is because the mode is intended to create a new @@ -118,10 +136,13 @@ jobs: steps: - attach_workspace: at: /workspace + - add_ssh_keys: fingerprints: - *deploy_ssh_fingerprint + - *step_setup_git + - checkout # Test file will have a consistent name between deployments, but diff --git a/.github/workflows/test-php.yml b/.github/workflows/test-php.yml index e94227d..a0e8c7c 100644 --- a/.github/workflows/test-php.yml +++ b/.github/workflows/test-php.yml @@ -66,13 +66,14 @@ jobs: fail_ci_if_error: true token: ${{ secrets.CODECOV_TOKEN }} + # Demonstration of deployment in 'force-push' mode. deploy-force-push: needs: test-php runs-on: ubuntu-latest env: - BRANCH_NAME: ${{ github.head_ref }} + GITHUB_BRANCH: ${{ github.head_ref }} steps: - name: Checkout code @@ -114,8 +115,8 @@ jobs: # changes in the source repository. - name: Prepare test file. run: | - export TEST_FILE="test-file--force-push--gha--${BRANCH_NAME//\//-}.txt" - echo "Deployment 1 for branch $BRANCH_NAME" >> $TEST_FILE + export TEST_FILE="test-file--force-push--gha--${GITHUB_BRANCH//\//-}.txt" + echo "Deployment 1 for branch $GITHUB_BRANCH" >> $TEST_FILE date "+%Y%m%d-%H%M%S" >> $TEST_FILE - name: Deployment 1 @@ -136,8 +137,8 @@ jobs: - name: Update the test file to simulate changes in the source repository. run: | - export TEST_FILE="test-file--branch--gha--${BRANCH_NAME//\//-}.txt" - echo "Deployment 2 for branch $BRANCH_NAME" >> $TEST_FILE + export TEST_FILE="test-file--branch--gha--${GITHUB_BRANCH//\//-}.txt" + echo "Deployment 2 for branch $GITHUB_BRANCH" >> $TEST_FILE date "+%Y%m%d-%H%M%S" >> $TEST_FILE - name: Deployment 2 @@ -154,3 +155,111 @@ jobs: echo "Deployed to $DEPLOYED_BRANCH" echo echo "See https://github.com/drevops/git-artifact-destination/blob/$DEPLOYED_BRANCH/$TEST_FILE" + + # Demonstration of deployment in 'branch' mode. + # Note that by design, pushing into the same branch will result in the failure + # of the second push. This is because the mode is intended to create a new + # branch per artifact deployment. + deploy-branch: + needs: test-php + + runs-on: ubuntu-latest + + env: + GITHUB_BRANCH: ${{ github.head_ref }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + # Fetch all history for git repository. + fetch-depth: 0 + # Do not persist credentials after checkout + # to allow to use custom credentials to push to a remote repo. + persist-credentials: false + ref: ${{ github.head_ref }} + + - name: Cache Composer dependencies + uses: actions/cache@v4 + with: + path: /tmp/composer-cache + key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + + - name: Setup SSH private key + uses: webfactory/ssh-agent@v0.9.0 + with: + ssh-private-key: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }} + + - name: Configure Git + run: | + git config --global user.name "${{ secrets.DEPLOY_USER_NAME }}" + git config --global user.email "${{ secrets.DEPLOY_USER_EMAIL }}" + + - name: Install dependencies + run: composer install + + # Test file will have a consistent name between deployments, but + # the contents will be added to on each deployment to simulate + # changes in the source repository. + # Since each deployment in this mode creates a new branch, the test file + # will be pushed to a new branch each time and won't be updated in + # existing branches. + + - name: Prepare test file. + run: | + export TEST_FILE="test-file--branch--gha--${GITHUB_BRANCH//\//-}.txt" + echo "Deployment 1 for branch $GITHUB_BRANCH" >> $TEST_FILE + date "+%Y%m%d-%H%M%S" >> $TEST_FILE + + - name: Deployment 1 + run: | + vendor/bin/robo artifact \ + git@github.com:drevops/git-artifact-destination.git \ + --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i] \ + --mode=branch \ + --report=$HOME/report--mode--branch.txt \ + --push \ + --debug + + DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') + echo "Deployed to $DEPLOYED_BRANCH" + echo + echo "See https://github.com/drevops/git-artifact-destination/blob/$DEPLOYED_BRANCH/$TEST_FILE" + rm $HOME/report--mode--branch.txt + + - name: Update the test file to simulate changes in the source repository. + run: | + export TEST_FILE="test-file--branch--gha--${GITHUB_BRANCH//\//-}.txt" + echo "Deployment 2 for branch $GITHUB_BRANCH" >> $TEST_FILE + date "+%Y%m%d-%H%M%S" >> $TEST_FILE + + - name: Deployment 2 - same branch + run: | + vendor/bin/robo artifact \ + git@github.com:drevops/git-artifact-destination.git \ + --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i] \ + --mode=branch \ + --report=$HOME/report--mode--branch.txt \ + --push \ + --debug \ + && { echo "Expected to fail as repeated pushes to the same branch are not allowed, but succeeded" >&2; exit 1; } || echo "Failed as expected" + + - name: Deployment 2 - new branch + run: | + vendor/bin/robo artifact \ + git@github.com:drevops/git-artifact-destination.git \ + --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i-s] \ + --mode=branch \ + --report=$HOME/report--mode--branch.txt \ + --push \ + --debug + + DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') + echo "Deployed to $DEPLOYED_BRANCH" + echo + echo "See https://github.com/drevops/git-artifact-destination/blob/$DEPLOYED_BRANCH/$TEST_FILE"