Skip to content

Commit 82a0bfa

Browse files
authored
Merge pull request #390 from awesomemotive/update/gh-actions-2.3.5
Add test & deployment build gh actions
2 parents f8cdb9f + 6056965 commit 82a0bfa

File tree

3 files changed

+259
-0
lines changed

3 files changed

+259
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Workflow name
2+
name: Auto-testing-builds & Deployments
3+
4+
# This workflow is triggered on every push to the repository.
5+
on:
6+
push:
7+
8+
# Defines the jobs that will be executed as part of this workflow.
9+
jobs:
10+
build:
11+
# Specifies the runner environment for the job. In this case, it\'s Ubuntu 22.04.
12+
runs-on: ubuntu-22.04
13+
14+
# Defines the sequence of steps that make up the \'build\' job.
15+
steps:
16+
# This action exposes GitHub environment variables, making them available in subsequent steps.
17+
- name: GitHub Environment Variables Action
18+
uses: FranzDiebold/[email protected]
19+
20+
# Sets a project name based on the GitHub repository name.
21+
# It extracts the repository name, converts it to lowercase, and saves it as an environment variable PROJECT_NAME.
22+
- name: Set Project Name
23+
run: |
24+
repo_full_name="${{ github.repository }}"
25+
repo_short_name="${repo_full_name##*/}"
26+
echo "PROJECT_NAME=${repo_short_name,,}" >> $GITHUB_ENV
27+
28+
# Checks out the repository\'s code so the workflow can access it.
29+
- name: Checkout code
30+
uses: actions/checkout@v2
31+
32+
# Sets up PHP version 8.0 and installs Composer.
33+
- name: Setup PHP 8.0
34+
uses: shivammathur/setup-php@v2
35+
with:
36+
php-version: 8.0
37+
tools: composer
38+
39+
# Installs Node.js version 21.
40+
- name: Install Node.js
41+
uses: actions/setup-node@v2
42+
with:
43+
node-version: 21
44+
45+
# Installs PHP dependencies for a production environment using Composer.
46+
# --no-dev: Skips development dependencies.
47+
# --ignore-platform-reqs: Ignores PHP version and extension requirements.
48+
# --prefer-dist: Downloads and unpacks archives instead of cloning from version control.
49+
# --no-scripts: Prevents execution of scripts defined in composer.json.
50+
# --optimize-autoloader: Converts PSR-0/4 rules into classmap rules for faster class loading.
51+
- name: Install dependencies
52+
run: composer install --no-dev --ignore-platform-reqs --prefer-dist --no-scripts --optimize-autoloader
53+
54+
# Installs Node.js dependencies using npm.
55+
- name: Install nodejs dependencies
56+
run: npm install
57+
58+
# Compiles and bundles assets for production.
59+
- name: Generate production build
60+
run: npm run production
61+
62+
# Generates translation files for the project.
63+
- name: Generate translation files
64+
run: npm run translate
65+
66+
# Removes unnecessary files and directories to create a clean build for deployment.
67+
# This includes development-related files, version control directories, and source assets.
68+
- name: Clean unneeded files
69+
run: |
70+
[ -f .gitattributes ] && rm .gitattributes
71+
[ -d .github ] && rm -r .github
72+
[ -d .git ] && rm -r .git
73+
[ -f .gitignore ] && rm .gitignore
74+
[ -f phpcs.xml ] && rm phpcs.xml
75+
[ -f wpgulp.config.js ] && rm wpgulp.config.js
76+
[ -f gulpfile.babel.js ] && rm gulpfile.babel.js
77+
[ -f composer.json ] && rm composer.json
78+
[ -f composer.lock ] && rm composer.lock
79+
[ -f package.json ] && rm package.json
80+
[ -f package-lock.json ] && rm package-lock.json
81+
[ -f readme.md ] && rm readme.md
82+
[ -f playwright.config.js ] && rm playwright.config.js
83+
[ -d assets/sass ] && rm -r assets/sass
84+
[ -d assets/js/src ] && rm -r assets/js/src
85+
[ -d node_modules ] && rm -r node_modules
86+
[ -d e2etests ] && rm -r e2etests
87+
88+
# Creates a new directory named after the project to hold the build files.
89+
- name: Create directory for the build
90+
run: mkdir ${{ env.PROJECT_NAME }}
91+
92+
# Moves all the cleaned project files into the newly created build directory.
93+
- name: Move files to the build directory
94+
run: rsync -av --progress --exclude=${{ env.PROJECT_NAME }} . ${{ env.PROJECT_NAME }}/
95+
96+
# Creates a zip archive of the build directory.
97+
- name: Create Artifact
98+
run: zip -qq -r "${{ env.PROJECT_NAME }}.zip" ${{ env.PROJECT_NAME }}/
99+
100+
# The following steps are for uploading the build to hub.wpforms.com.
101+
# Sets up an SSH key to securely connect to the deployment server.
102+
# The private key is stored as a secret.
103+
- name: Setup SSH key for hub.wpforms.com upload
104+
run: |
105+
mkdir -p ~/.ssh
106+
echo "${{ secrets.WPFORMS_TEST_DOCKER_SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
107+
chmod 600 ~/.ssh/id_ed25519
108+
chmod 700 ~/.ssh
109+
110+
# Creates the necessary directory structure on the local runner before uploading.
111+
- name: Create builds directory structure
112+
run: |
113+
mkdir -p builds/${{ env.PROJECT_NAME }}/${{ env.CI_REF_NAME_SLUG }}
114+
cp ${{ env.PROJECT_NAME }}.zip builds/${{ env.PROJECT_NAME }}/${{ env.CI_REF_NAME_SLUG }}/
115+
116+
# Uploads the build to the deployment server using rsync over SSH.
117+
# --rsh: Specifies the remote shell to use (ssh with specific options).
118+
# -o StrictHostKeyChecking=no: Disables host key checking.
119+
# -p18765: Specifies the port for the SSH connection.
120+
# --ignore-times: Doesn\'t skip files that have the same size and modification time.
121+
# --archive: Archive mode, equivalent to -rlptgoD.
122+
# --verbose: Increases verbosity.
123+
# --compress: Compresses file data during the transfer.
124+
# --human-readable: Outputs numbers in a human-readable format.
125+
# --progress: Shows progress during transfer.
126+
# --whole-file: Copies files whole (without delta-xfer algorithm).
127+
# --remove-source-files: Removes files from the source directory after they are transferred.
128+
- name: Upload to hub.wpforms.com
129+
run: |
130+
rsync --rsh="ssh -o StrictHostKeyChecking=no -p18765" \
131+
--ignore-times \
132+
--archive \
133+
--verbose \
134+
--compress \
135+
--human-readable \
136+
--progress \
137+
--whole-file \
138+
--remove-source-files \
139+
builds/ \
140+
[email protected]:/home/staging/shared/hub.wpforms.com/builds/athemes/
141+
142+
# Uploads the generated zip file as a workflow artifact.
143+
# This allows the build to be downloaded from the GitHub Actions run page.
144+
- name: Upload file list as artifact
145+
uses: actions/upload-artifact@v4
146+
with:
147+
name: ${{ env.PROJECT_NAME }}.zip
148+
path: ${{ env.PROJECT_NAME }}.zip
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Workflow name
2+
name: Auto-builds PR Cleanup
3+
4+
# This workflow is triggered when a pull request is closed (either merged or closed without merging).
5+
on:
6+
pull_request:
7+
types: [ closed ]
8+
9+
jobs:
10+
build:
11+
# The type of runner that the job will run on.
12+
runs-on: ubuntu-22.04
13+
14+
steps:
15+
# This action exposes GitHub environment variables to be used in subsequent steps.
16+
- name: GitHub Environment Variables Action
17+
uses: FranzDiebold/[email protected]
18+
19+
# This step sets a project name environment variable based on the repository name.
20+
# It takes the full repository name (e.g., "owner/repo"), extracts the repository name part,
21+
# converts it to lowercase, and saves it as PROJECT_NAME in the GitHub environment.
22+
- name: Set Project Name
23+
run: |
24+
repo_full_name="${{ github.repository }}"
25+
repo_short_name="${repo_full_name##*/}"
26+
echo "PROJECT_NAME=${repo_short_name,,}" >> $GITHUB_ENV
27+
28+
# This step sets up an SSH key to allow the runner to authenticate with the staging server.
29+
# It creates the .ssh directory, writes the private key from secrets, and sets the correct permissions.
30+
- name: Setup SSH key for hub.wpforms.com upload
31+
run: |
32+
mkdir -p ~/.ssh
33+
echo "${{ secrets.WPFORMS_TEST_DOCKER_SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
34+
chmod 600 ~/.ssh/id_ed25519
35+
chmod 700 ~/.ssh
36+
37+
# This step connects to the staging server via SSH and deletes the build directory
38+
# associated with the closed pull request's branch.
39+
- name: Delete builds from merged branch
40+
run: ssh -p18765 [email protected] "cd /home/staging/shared/hub.wpforms.com/builds/athemes/${{ env.PROJECT_NAME }} && rm -rf ${{ env.CI_REF_NAME_SLUG }}"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Auto-builds Daily Cleanup
2+
3+
# This workflow runs daily, gets all the branches from the repository,
4+
# gets all the auto-builds from the staging server,
5+
# and deletes any auto-builds that don't have a corresponding branch.
6+
# This is to ensure that the staging server doesn't get filled up with old, unused builds.
7+
8+
'on':
9+
workflow_dispatch: # Allows the workflow to be triggered manually from the GitHub UI.
10+
schedule:
11+
- cron: '1 0 * * *' # Runs the workflow every day at 00:01.
12+
13+
jobs:
14+
builds:
15+
name: Delete builds for non existing branches
16+
runs-on: ubuntu-22.04
17+
18+
steps:
19+
# This action exposes GitHub environment variables to be used in subsequent steps.
20+
- name: GitHub Environment Variables Action
21+
uses: FranzDiebold/[email protected]
22+
23+
# This step extracts the repository name from the full repository path and sets it as a PROJECT_NAME environment variable.
24+
# e.g. if the repository is aThemes/merchant, the project name will be "merchant".
25+
- name: Set Project Name
26+
run: |
27+
repo_full_name="${{ github.repository }}"
28+
repo_short_name="${repo_full_name##*/}"
29+
echo "PROJECT_NAME=${repo_short_name,,}" >> $GITHUB_ENV
30+
31+
# This step sets up an SSH key to allow the workflow to connect to the staging server.
32+
# The private key is stored as a secret in the GitHub repository.
33+
- name: Setup SSH key for hub.wpforms.com upload
34+
run: |
35+
mkdir -p ~/.ssh
36+
echo "${{ secrets.WPFORMS_TEST_DOCKER_SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
37+
chmod 600 ~/.ssh/id_ed25519
38+
chmod 700 ~/.ssh
39+
40+
# This step fetches the latest list of remote branches, formats the branch names, and saves them to a file named branches.txt.
41+
# The formatting removes the "origin/" prefix and replaces slashes and dots with hyphens.
42+
- name: Prepare list of branches
43+
run: |
44+
git remote update --prune
45+
git branch -r > branches.txt
46+
sed -i 's/ origin\///g' branches.txt
47+
sed -i 's/\//-/g' branches.txt
48+
sed -i 's/\./-/g' branches.txt
49+
50+
# This step connects to the staging server via SSH and lists the contents of the auto-builds directory for the current project.
51+
# The list of auto-builds is saved to a file named auto-builds.txt.
52+
- name: Get list of auto-builds
53+
run: ssh -p18765 [email protected] "cd /home/staging/shared/hub.wpforms.com/builds/athemes/${{ env.PROJECT_NAME }} && ls" > auto-builds.txt
54+
55+
# This step reads the auto-builds.txt file line by line.
56+
# For each auto-build, it checks if a corresponding branch exists in the branches.txt file.
57+
# If a branch does not exist, it deletes the auto-build directory from the staging server.
58+
- name: Delete auto-builds if they're not in the list of branches
59+
run: |
60+
while read -r dir; do
61+
echo "Checking auto-build: $dir"
62+
if [ "$dir" = "index.php" ]
63+
then
64+
continue
65+
fi
66+
if ! grep -q "$dir" branches.txt
67+
then
68+
echo "Deleting auto-build: $dir"
69+
ssh -n -p18765 [email protected] "cd /home/staging/shared/hub.wpforms.com/builds/athemes/${{ env.PROJECT_NAME }} && rm -rf $dir"
70+
fi
71+
done < auto-builds.txt

0 commit comments

Comments
 (0)