From b5a8b9cf1341cae10165e497e566cafa61dbbf48 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 27 Feb 2024 08:38:32 +1100 Subject: [PATCH] Fixed resulting branch in CI examples. --- .circleci/config.yml | 193 +++++++++++++++++++++++++++------ .github/workflows/test-php.yml | 179 +++++++++++++++++++++++++----- 2 files changed, 311 insertions(+), 61 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9273688..263b213 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -14,14 +14,14 @@ aliases: run: name: Setup git command: | - mkdir -p "${HOME}/.ssh/" - echo -e "Host *\n\tStrictHostKeyChecking no\n" > "${HOME}/.ssh/config" + mkdir -p "$HOME/.ssh/" + echo -e "Host *\n\tStrictHostKeyChecking no\n" > "$HOME/.ssh/config" DEPLOY_SSH_FILE="${DEPLOY_SSH_FINGERPRINT//:}" - DEPLOY_SSH_FILE="${HOME}/.ssh/id_rsa_${DEPLOY_SSH_FILE//\"}" - if [ -f "${DEPLOY_SSH_FILE}" ]; then - echo "Found Deploy SSH key file ${DEPLOY_SSH_FILE}" + DEPLOY_SSH_FILE="$HOME/.ssh/id_rsa_${DEPLOY_SSH_FILE//\"}" + if [ -f "$DEPLOY_SSH_FILE" ]; then + echo "Found Deploy SSH key file $DEPLOY_SSH_FILE" ssh-add -D > /dev/null - ssh-add "${DEPLOY_SSH_FILE}" + ssh-add "$DEPLOY_SSH_FILE" fi git config --global user.name "$DEPLOY_USER_NAME" git config --global user.email "$DEPLOY_USER_EMAIL" @@ -32,71 +32,192 @@ 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: - code - deploy: + # Demonstration of deployment in 'force-push' mode. + deploy-force-push: <<: *container_config 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 + # the contents will be added to on each deployment to simulate + # changes in the source repository. - run: - name: Demonstration of deployment in 'force-push' mode. + name: Prepare test file. command: | - TEST_FILE="test-file-force-push-circleci-$(date "+%Y%m%d-%H%M%S").txt" - touch $TEST_FILE + export TEST_FILE="test-file--force-push--circleci--${CIRCLE_BRANCH//\//-}.txt" + echo "Deployment 1 for branch $CIRCLE_BRANCH" >> $TEST_FILE + date "+%Y%m%d-%H%M%S" >> $TEST_FILE + - run: + name: Deployment 1 + command: | vendor/bin/robo artifact \ - git@github.com:drevops/git-artifact-destination.git \ - --branch=mode-force-push-circleci \ - --mode=force-push \ - --report=$HOME/report-mode-force-push.txt \ - --push - - DEPLOY_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report-mode-force-push.txt | sed 's/ //g') - echo "Deployed to $DEPLOY_BRANCH" + git@github.com:drevops/git-artifact-destination.git \ + --branch=mode--force-push--circleci--[branch] \ + --mode=force-push \ + --report=$HOME/report--mode--force-push.txt \ + --push \ + --debug + + DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') + echo "Deployed to $DEPLOYED_BRANCH" echo - echo "See https://github.com/drevops/git-artifact-destination/blob/$DEPLOY_BRANCH/$TEST_FILE" + echo "See https://github.com/drevops/git-artifact-destination/blob/$DEPLOYED_BRANCH/$TEST_FILE" + rm $HOME/report--mode--force-push.txt - run: - name: Demonstration of deployment in 'branch' mode. + name: Update the test file to simulate changes in the source repository. command: | - TEST_FILE="test-file-branch-circleci-$(date "+%Y%m%d-%H%M%S").txt" - touch $TEST_FILE + export TEST_FILE="test-file--branch--circleci--${CIRCLE_BRANCH//\//-}.txt" + echo "Deployment 2 for branch $CIRCLE_BRANCH" >> $TEST_FILE + date "+%Y%m%d-%H%M%S" >> $TEST_FILE + - run: + name: Deployment 2 + command: | vendor/bin/robo artifact \ - git@github.com:drevops/git-artifact-destination.git \ - --branch=mode-branch-circleci-[timestamp:Y-m-d_H-i-s] \ - --mode=branch \ - --report=$HOME/report-mode-branch.txt \ - --push - - DEPLOY_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report-mode-branch.txt | sed 's/ //g') - echo "Deployed to $DEPLOY_BRANCH" + git@github.com:drevops/git-artifact-destination.git \ + --branch=mode--force-push--circleci--[branch] \ + --mode=force-push \ + --report=$HOME/report--mode--force-push.txt \ + --push \ + --debug + + DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') + echo "Deployed to $DEPLOYED_BRANCH" echo - echo "See https://github.com/drevops/git-artifact-destination/blob/$DEPLOY_BRANCH/$TEST_FILE" + 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: + <<: *container_config + 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 + # 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. + - run: + name: Prepare test file. + command: | + export TEST_FILE="test-file--branch--circleci--${CIRCLE_BRANCH//\//-}.txt" + echo "Deployment 1 for branch $CIRCLE_BRANCH" >> $TEST_FILE + date "+%Y%m%d-%H%M%S" >> $TEST_FILE + + - run: + name: Deployment 1 + command: | + vendor/bin/robo artifact \ + git@github.com:drevops/git-artifact-destination.git \ + --branch=mode--branch--circleci--[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 + + - run: + name: Update the test file to simulate changes in the source repository. + command: | + export TEST_FILE="test-file--branch--circleci--${CIRCLE_BRANCH//\//-}.txt" + echo "Deployment 2 for branch $CIRCLE_BRANCH" >> $TEST_FILE + date "+%Y%m%d-%H%M%S" >> $TEST_FILE + + - run: + name: Deployment 2 - same branch + command: | + vendor/bin/robo artifact \ + git@github.com:drevops/git-artifact-destination.git \ + --branch=mode--branch--circleci--[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" + + - run: + name: Deployment 2 - new branch + command: | + vendor/bin/robo artifact \ + git@github.com:drevops/git-artifact-destination.git \ + --branch=mode--branch--circleci--[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" workflows: version: 2 main: jobs: - build - - deploy: + - deploy-force-push: + requires: + - build + - deploy-branch: requires: - build diff --git a/.github/workflows/test-php.yml b/.github/workflows/test-php.yml index 364b32b..a0e8c7c 100644 --- a/.github/workflows/test-php.yml +++ b/.github/workflows/test-php.yml @@ -66,16 +66,118 @@ jobs: fail_ci_if_error: true token: ${{ secrets.CODECOV_TOKEN }} - deploy: + # Demonstration of deployment in 'force-push' mode. + deploy-force-push: 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. + - name: Prepare test file. + run: | + 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 + run: | + vendor/bin/robo artifact \ + git@github.com:drevops/git-artifact-destination.git \ + --branch=mode--force-push--gha--[branch] \ + --mode=force-push \ + --report=$HOME/report--mode--force-push.txt \ + --push \ + --debug + + DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.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--force-push.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 + run: | + vendor/bin/robo artifact \ + git@github.com:drevops/git-artifact-destination.git \ + --branch=mode--force-push--gha--[branch] \ + --mode=force-push \ + --report=$HOME/report--mode--force-push.txt \ + --push \ + --debug + + DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') + 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 @@ -101,36 +203,63 @@ jobs: - name: Install dependencies run: composer install - - name: Demonstration of deployment in 'force-push' mode. + # 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: | - TEST_FILE="test-file-force-push-github-$(date "+%Y%m%d-%H%M%S").txt" - touch $TEST_FILE + 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-force-push-github \ - --mode=force-push \ - --report=$HOME/report-mode-force-push.txt \ - --push - - DEPLOY_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report-mode-force-push.txt | sed 's/ //g') - echo "Deployed to $DEPLOY_BRANCH" + 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/$DEPLOY_BRANCH/$TEST_FILE" + echo "See https://github.com/drevops/git-artifact-destination/blob/$DEPLOYED_BRANCH/$TEST_FILE" + rm $HOME/report--mode--branch.txt - - name: Demonstration of deployment in 'branch' mode. + - name: Update the test file to simulate changes in the source repository. run: | - TEST_FILE="test-file-branch-github-$(date "+%Y%m%d-%H%M%S").txt" - touch $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 - same branch + run: | vendor/bin/robo artifact \ - git@github.com:drevops/git-artifact-destination.git \ - --branch=mode-branch-github-[timestamp:Y-m-d_H-i-s] \ - --mode=branch \ - --report=$HOME/report-mode-branch.txt \ - --push - - DEPLOY_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report-mode-branch.txt | sed 's/ //g') - echo "Deployed to $DEPLOY_BRANCH" + 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/$DEPLOY_BRANCH/$TEST_FILE" + echo "See https://github.com/drevops/git-artifact-destination/blob/$DEPLOYED_BRANCH/$TEST_FILE"