Skip to content

Commit f233d32

Browse files
authored
Merge pull request #398 from awesomemotive/feature/394-modular-workflows
Reusable workflows for Botiga
2 parents 2e51407 + f091469 commit f233d32

File tree

7 files changed

+216
-262
lines changed

7 files changed

+216
-262
lines changed

.github/workflows/auto-testing-builds.yml

Lines changed: 13 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ name: Auto-testing-builds & Deployments
55
on:
66
push:
77

8+
env:
9+
builds-directory: ~/builds/athemes
10+
811
# Defines the jobs that will be executed as part of this workflow.
912
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+
create-testing-build:
14+
uses: awesomemotive/Botiga/.github/workflows/build.yml@main
15+
16+
upload-testing-build:
17+
name: Prepare and upload builds in hub.wpforms.com/builds
18+
runs-on: self-hosted
19+
needs: create-testing-build
1320
if: "!contains(github.event.head_commit.message, 'ci skip')"
1421

1522
# Defines the sequence of steps that make up the \'build\' job.
@@ -18,114 +25,22 @@ jobs:
1825
- name: GitHub Environment Variables Action
1926
uses: FranzDiebold/[email protected]
2027

21-
# Sets a project name based on the GitHub repository name.
22-
# It extracts the repository name, converts it to lowercase, and saves it as an environment variable PROJECT_NAME.
2328
- name: Set Project Name
2429
run: |
2530
repo_full_name="${{ github.repository }}"
2631
repo_short_name="${repo_full_name##*/}"
2732
echo "PROJECT_NAME=${repo_short_name,,}" >> $GITHUB_ENV
2833
29-
# Checks out the repository\'s code so the workflow can access it.
30-
- name: Checkout code
31-
uses: actions/checkout@v5
32-
33-
# Sets up PHP version 8.0 and installs Composer.
34-
- name: Setup PHP 8.0
35-
uses: shivammathur/setup-php@v2
36-
with:
37-
php-version: 8.0
38-
tools: composer
39-
40-
# Installs Node.js version 21.
41-
- name: Install Node.js
42-
uses: actions/setup-node@v2
43-
with:
44-
node-version: 21
45-
46-
# Installs PHP dependencies for a production environment using Composer.
47-
# --no-dev: Skips development dependencies.
48-
# --ignore-platform-reqs: Ignores PHP version and extension requirements.
49-
# --prefer-dist: Downloads and unpacks archives instead of cloning from version control.
50-
# --no-scripts: Prevents execution of scripts defined in composer.json.
51-
# --optimize-autoloader: Converts PSR-0/4 rules into classmap rules for faster class loading.
52-
- name: Install dependencies
53-
run: composer install --no-dev --ignore-platform-reqs --prefer-dist --no-scripts --optimize-autoloader
54-
55-
# Installs Node.js dependencies using npm.
56-
- name: Install nodejs dependencies
57-
run: npm install
58-
59-
# Compiles and bundles assets for production.
60-
- name: Generate production build
61-
run: npm run production
62-
63-
# Generates translation files for the project.
64-
- name: Generate translation files
65-
run: npm run translate
66-
67-
# Removes unnecessary files and directories to create a clean build for deployment.
68-
# This includes development-related files, version control directories, and source assets.
69-
- name: Clean unneeded files
70-
run: |
71-
[ -d .github ] && rm -r .github
72-
[ -d .git ] && rm -r .git
73-
[ -d assets/sass ] && rm -r assets/sass
74-
[ -d assets/js/src ] && rm -r assets/js/src
75-
[ -d node_modules ] && rm -r node_modules
76-
[ -d tests ] && rm -r tests
77-
[ -d .cursor ] && rm -r .cursor
78-
[ -f .gitattributes ] && rm .gitattributes
79-
[ -f .gitignore ] && rm .gitignore
80-
[ -f phpcs.xml ] && rm phpcs.xml
81-
[ -f wpgulp.config.js ] && rm wpgulp.config.js
82-
[ -f gulpfile.babel.js ] && rm gulpfile.babel.js
83-
[ -f composer.json ] && rm composer.json
84-
[ -f composer.lock ] && rm composer.lock
85-
[ -f package.json ] && rm package.json
86-
[ -f readme.md ] && rm readme.md
87-
[ -f package-lock.json ] && rm package-lock.json
88-
89-
# Creates a new directory named after the project to hold the build files.
90-
- name: Create directory for the build
91-
run: mkdir ${{ env.PROJECT_NAME }}
92-
93-
# Moves all the cleaned project files into the newly created build directory.
94-
- name: Move files to the build directory
95-
run: rsync -av --progress --exclude=${{ env.PROJECT_NAME }} . ${{ env.PROJECT_NAME }}/
96-
97-
# Creates a zip archive of the build directory.
98-
- name: Create Artifact
99-
run: zip -qq -r "${{ env.PROJECT_NAME }}.zip" ${{ env.PROJECT_NAME }}/
100-
101-
# The following steps are for uploading the build to hub.wpforms.com.
102-
# Sets up an SSH key to securely connect to the deployment server.
103-
# The private key is stored as a secret.
104-
- name: Setup SSH key for hub.wpforms.com upload
105-
run: |
106-
mkdir -p ~/.ssh
107-
echo "${{ secrets.WPFORMS_TEST_DOCKER_SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
108-
chmod 600 ~/.ssh/id_ed25519
109-
chmod 700 ~/.ssh
110-
111-
# Creates the necessary directory structure on the local runner before uploading.
112-
- name: Create builds directory structure
113-
run: |
114-
mkdir -p builds/${{ env.PROJECT_NAME }}/${{ env.CI_REF_NAME_SLUG }}
115-
cp ${{ env.PROJECT_NAME }}.zip builds/${{ env.PROJECT_NAME }}/${{ env.CI_REF_NAME_SLUG }}/
116-
117-
# Uploads the build to the deployment server using rsync over SSH.
34+
# Uploads the build to the server using rsync over SSH.
11835
# --rsh: Specifies the remote shell to use (ssh with specific options).
11936
# -o StrictHostKeyChecking=no: Disables host key checking.
120-
# -p18765: Specifies the port for the SSH connection.
12137
# --ignore-times: Doesn\'t skip files that have the same size and modification time.
12238
# --archive: Archive mode, equivalent to -rlptgoD.
12339
# --verbose: Increases verbosity.
12440
# --compress: Compresses file data during the transfer.
12541
# --human-readable: Outputs numbers in a human-readable format.
12642
# --progress: Shows progress during transfer.
12743
# --whole-file: Copies files whole (without delta-xfer algorithm).
128-
# --remove-source-files: Removes files from the source directory after they are transferred.
12944
- name: Upload to hub.wpforms.com
13045
run: |
13146
rsync --rsh="ssh -o StrictHostKeyChecking=no -p18765" \
@@ -136,14 +51,5 @@ jobs:
13651
--human-readable \
13752
--progress \
13853
--whole-file \
139-
--remove-source-files \
140-
builds/ \
141-
[email protected]:/home/staging/shared/hub.wpforms.com/builds/athemes/
142-
143-
# Uploads the generated zip file as a workflow artifact.
144-
# This allows the build to be downloaded from the GitHub Actions run page.
145-
- name: Upload file list as artifact
146-
uses: actions/upload-artifact@v4
147-
with:
148-
name: ${{ env.PROJECT_NAME }}.zip
149-
path: ${{ env.PROJECT_NAME }}.zip
54+
${{ env.builds-directory }}/${{ env.PROJECT_NAME }}/${{ env.CI_REF_NAME_SLUG }}/${{ env.PROJECT_NAME }}.zip \
55+
[email protected]:/home/staging/shared/hub.wpforms.com/builds/athemes/${{ env.PROJECT_NAME }}/${{ env.CI_REF_NAME_SLUG }}/

.github/workflows/auto_builds_cleanup.yml

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@ on:
77
types: [ closed ]
88
branches-ignore: [ main ]
99

10+
env:
11+
builds-directory: ~/builds/athemes
12+
1013
jobs:
1114
cleaning:
1215
# The type of runner that the job will run on.
13-
runs-on: ubuntu-22.04
16+
runs-on: self-hosted
1417

1518
steps:
1619
# This action exposes GitHub environment variables to be used in subsequent steps.
1720
- name: GitHub Environment Variables Action
1821
uses: FranzDiebold/[email protected]
1922

20-
# This step installs the SSH key to allow the runner to authenticate with the staging server.
21-
- name: Install SSH Key
22-
uses: shimataro/ssh-key-action@v2
23-
with:
24-
key: ${{ secrets.WPFORMS_TEST_DOCKER_SSH_PRIVATE_KEY }}
25-
known_hosts: 'just-a-placeholder-so-we-dont-get-errors'
26-
2723
# This step sets a project name environment variable based on the repository name.
2824
# It takes the full repository name (e.g., "owner/repo"), extracts the repository name part,
2925
# converts it to lowercase, and saves it as PROJECT_NAME in the GitHub environment.
@@ -33,19 +29,10 @@ jobs:
3329
repo_short_name="${repo_full_name##*/}"
3430
echo "PROJECT_NAME=${repo_short_name,,}" >> $GITHUB_ENV
3531
36-
# This step sets up an SSH key to allow the runner to authenticate with the staging server.
37-
# It creates the .ssh directory, writes the private key from secrets, and sets the correct permissions.
38-
- name: Setup SSH key for hub.wpforms.com upload
39-
run: |
40-
mkdir -p ~/.ssh
41-
echo "${{ secrets.WPFORMS_TEST_DOCKER_SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
42-
chmod 600 ~/.ssh/id_ed25519
43-
chmod 700 ~/.ssh
44-
45-
- name: Adding Known Hosts
46-
run: ssh-keyscan -p 18765 -H ${{ secrets.WPFORMS_STAGING_HOST }} >> ~/.ssh/known_hosts
32+
- name: Delete merged branch from local builds directory
33+
run: rm -r ${{ env.builds-directory }}/${{ env.PROJECT_NAME }}/${{ env.CI_REF_NAME_SLUG }}
4734

4835
# This step connects to the staging server via SSH and deletes the build directory
4936
# associated with the closed pull request's branch.
50-
- name: Delete builds from merged branch
37+
- name: Delete merged branch from hub.wpforms.com/builds
5138
run: ssh -p18765 staging@${{ secrets.WPFORMS_STAGING_HOST }} "cd /home/staging/shared/hub.wpforms.com/builds/athemes/${{ env.PROJECT_NAME }} && rm -rf ${{ env.CI_ACTION_REF_NAME_SLUG }}"

.github/workflows/auto_builds_cleanup_daily.yml

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ name: Auto-builds Daily Cleanup
1010
schedule:
1111
- cron: '1 0 * * *' # Runs the workflow every day at 00:01.
1212

13+
env:
14+
builds-directory: ~/builds/athemes
15+
1316
jobs:
1417
cleaning:
1518
name: Delete builds for non existing branches
16-
runs-on: ubuntu-22.04
19+
runs-on: self-hosted
1720

1821
steps:
1922

@@ -25,13 +28,6 @@ jobs:
2528
- name: GitHub Environment Variables Action
2629
uses: FranzDiebold/[email protected]
2730

28-
# This step installs the SSH key to allow the runner to authenticate with the staging server.
29-
- name: Install SSH Key
30-
uses: shimataro/ssh-key-action@v2
31-
with:
32-
key: ${{ secrets.WPFORMS_TEST_DOCKER_SSH_PRIVATE_KEY }}
33-
known_hosts: 'just-a-placeholder-so-we-dont-get-errors'
34-
3531
# This step extracts the repository name from the full repository path and sets it as a PROJECT_NAME environment variable.
3632
# e.g. if the repository is aThemes/merchant, the project name will be "merchant".
3733
- name: Set Project Name
@@ -40,19 +36,6 @@ jobs:
4036
repo_short_name="${repo_full_name##*/}"
4137
echo "PROJECT_NAME=${repo_short_name,,}" >> $GITHUB_ENV
4238
43-
# This step sets up an SSH key to allow the workflow to connect to the staging server.
44-
# The private key is stored as a secret in the GitHub repository.
45-
- name: Setup SSH key for hub.wpforms.com upload
46-
run: |
47-
mkdir -p ~/.ssh
48-
echo "${{ secrets.WPFORMS_TEST_DOCKER_SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
49-
chmod 600 ~/.ssh/id_ed25519
50-
chmod 700 ~/.ssh
51-
52-
# This step adds the known hosts to the SSH key.
53-
- name: Adding Known Hosts
54-
run: ssh-keyscan -p 18765 -H ${{ secrets.WPFORMS_STAGING_HOST }} >> ~/.ssh/known_hosts
55-
5639
# This step fetches the latest list of remote branches, formats the branch names, and saves them to a file named branches.txt.
5740
# The formatting removes the "origin/" prefix and replaces slashes and dots with hyphens.
5841
- name: Prepare list of branches
@@ -85,3 +68,18 @@ jobs:
8568
ssh -n -p18765 staging@${{ secrets.WPFORMS_STAGING_HOST }} "cd /home/staging/shared/hub.wpforms.com/builds/athemes/${{ env.PROJECT_NAME }} && rm -rf $dir"
8669
fi
8770
done < auto-builds.txt
71+
72+
# The list of auto-builds is saved to a file named auto-builds.txt.
73+
- name: Get list of local builds
74+
run: cd ${{ env.builds-directory }}/${{ env.PROJECT_NAME }} && ls > local-builds.txt
75+
76+
- name: Delete local builds if they're not in the list of branches
77+
run: |
78+
while read -r dir; do
79+
echo "Checking auto-build: $dir"
80+
if ! grep -q "$dir" branches.txt
81+
then
82+
echo "Deleting local-build: $dir"
83+
cd ${{ env.builds-directory }}/${{ env.PROJECT_NAME }} && rm -rf $dir"
84+
fi
85+
done < local-builds.txt

.github/workflows/build.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# GitHub Actions Workflow for bundling production theme zip file and attach it as an artifact in the workflow
2+
3+
name: Build project artifact
4+
on:
5+
workflow_call:
6+
7+
env:
8+
builds-directory: ~/builds/athemes
9+
10+
jobs:
11+
create-build-artifact:
12+
runs-on: self-hosted
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v2
17+
18+
# This action exposes GitHub environment variables, making them available in subsequent steps.
19+
- name: GitHub Environment Variables Action
20+
uses: FranzDiebold/[email protected]
21+
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+
- name: Setup PHP 8.0
29+
uses: shivammathur/setup-php@v2
30+
with:
31+
php-version: 8.0
32+
tools: composer
33+
34+
- name: Install Node.js
35+
uses: actions/setup-node@v2
36+
with:
37+
node-version: '21'
38+
39+
- name: Install dependencies
40+
run: composer install --no-dev --ignore-platform-reqs --prefer-dist --no-scripts --optimize-autoloader
41+
42+
- name: Install nodejs dependencies
43+
run: npm install
44+
45+
- name: Generate build assets
46+
run: npm run production
47+
48+
- name: Generate translation files
49+
run: npm run translate
50+
51+
- name: Clean unneeded files
52+
run: |
53+
[ -f .gitattributes ] && rm .gitattributes
54+
[ -f .gitignore ] && rm .gitignore
55+
[ -f phpcs.xml ] && rm phpcs.xml
56+
[ -f wpgulp.config.js ] && rm wpgulp.config.js
57+
[ -f gulpfile.babel.js ] && rm gulpfile.babel.js
58+
[ -f composer.json ] && rm composer.json
59+
[ -f composer.lock ] && rm composer.lock
60+
[ -f package.json ] && rm package.json
61+
[ -f package-lock.json ] && rm package-lock.json
62+
[ -f readme.md ] && rm readme.md
63+
[ -f playwright.config.js ] && rm playwright.config.js
64+
[ -d .github ] && rm -r .github
65+
[ -d .git ] && rm -r .git
66+
[ -d .cursor ] && rm -r .cursor
67+
[ -d assets/sass ] && rm -r assets/sass
68+
[ -d assets/js/src ] && rm -r assets/js/src
69+
[ -d node_modules ] && rm -r node_modules
70+
[ -d e2etests ] && rm -r e2etests
71+
72+
- name: Create directory for build zip
73+
run: mkdir ${{ env.PROJECT_NAME }}
74+
75+
- name: Move files to botiga (lowercase) directory
76+
run: mv * ${{ env.PROJECT_NAME }} 2> /dev/null || true
77+
78+
- name: Create Zip
79+
run: zip -qq -r "${{ env.PROJECT_NAME }}.zip" ${{ env.PROJECT_NAME }}/
80+
81+
- name: Prepare builds directory
82+
run: mkdir -p ${{ env.builds-directory }}/${{ env.PROJECT_NAME }}/${{ env.CI_REF_NAME_SLUG }}
83+
84+
- name: Move zip to build directory
85+
run: mv ${{ env.PROJECT_NAME }}.zip ${{ env.builds-directory }}/${{ env.PROJECT_NAME }}/${{ env.CI_REF_NAME_SLUG }}
86+
87+
- name: Upload artifact
88+
uses: actions/upload-artifact@v4
89+
with:
90+
name: ${{ env.PROJECT_NAME }}
91+
path: .
92+
93+
- name: Remove the (temporary) zip directory
94+
run: rm -r ${{ env.PROJECT_NAME }}

0 commit comments

Comments
 (0)