Skip to content

Commit 57b6bc8

Browse files
author
ricky.gummadi
committed
fix build
1 parent a5037cd commit 57b6bc8

File tree

3 files changed

+101
-88
lines changed

3 files changed

+101
-88
lines changed
Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: ipsimple.org - Build | Test | Deploy
1+
name: ipsimple.org - CI/CD Pipeline
22

33
on:
44
push:
@@ -21,17 +21,69 @@ jobs:
2121
- name: Install dependencies
2222
run: npm install
2323

24-
# - name: Run Linter
25-
# run: npm run lint
24+
- name: Fetch and tag the release
25+
id: tag_release
26+
run: |
27+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
28+
git config --global user.name "github-actions[bot]"
29+
30+
# Fetch tags from the remote repository
31+
git fetch --tags || { echo "Failed to fetch tags"; exit 1; }
32+
33+
# Get the latest tag
34+
LATEST_TAG=$(git describe --tags `git rev-list --tags --max-count=1` 2>/dev/null || echo "")
35+
36+
# If no tags are found, start from 1.0.0
37+
if [ -z "$LATEST_TAG" ]; then
38+
echo "No tags found. Starting from 1.0.0"
39+
NEW_TAG="1.0.0"
40+
else
41+
echo "Latest tag found: $LATEST_TAG"
42+
# Parse the latest version
43+
VERSION_REGEX="^([0-9]+)\.([0-9]+)\.([0-9]+)$"
44+
if [[ $LATEST_TAG =~ $VERSION_REGEX ]]; then
45+
MAJOR="${BASH_REMATCH[1]}"
46+
MINOR="${BASH_REMATCH[2]}"
47+
PATCH="${BASH_REMATCH[3]}"
48+
49+
# Increment the minor version
50+
MINOR=$((MINOR + 1))
51+
52+
# Reset minor and increment major if minor reaches 100
53+
if [ $MINOR -eq 100 ]; then
54+
MINOR=0
55+
MAJOR=$((MAJOR + 1))
56+
fi
57+
58+
# Form the new version tag
59+
NEW_TAG="$MAJOR.$MINOR.$PATCH"
60+
else
61+
echo "Latest tag is not in the expected format: $LATEST_TAG"
62+
exit 1
63+
fi
64+
fi
2665
27-
- name: List build directory contents
28-
run: ls -la build
66+
echo "Creating new tag: $NEW_TAG"
67+
# Create and push the new tag
68+
git tag $NEW_TAG || { echo "Failed to create tag $NEW_TAG"; exit 1; }
69+
git push origin $NEW_TAG || { echo "Failed to push tag $NEW_TAG"; exit 1; }
70+
71+
# Set the new tag as an output variable
72+
echo "::set-output name=new_tag::$NEW_TAG"
73+
74+
- name: Set version as environment variable
75+
run: echo "NEW_TAG=${{ steps.tag_release.outputs.new_tag }}" >> $GITHUB_ENV
76+
77+
- name: Run Linter
78+
run: npm run lint
2979

3080
- name: Build project
81+
env:
82+
NEW_TAG: ${{ steps.tag_release.outputs.new_tag }}
3183
run: npm run build
32-
84+
3385
- name: Run Tests
34-
run: npm test
86+
run: npm test
3587

3688
- name: Deploy to GitHub Pages
3789
uses: peaceiris/actions-gh-pages@v3
@@ -40,57 +92,6 @@ jobs:
4092
publish_dir: ./dist
4193
publish_branch: main
4294

43-
- name: Tag the release
44-
if: github.ref == 'refs/heads/dev'
45-
run: |
46-
git config --global user.email "github-actions[bot]@users.noreply.github.com"
47-
git config --global user.name "github-actions[bot]"
48-
49-
# Fetch tags from the remote repository
50-
git fetch --tags || { echo "Failed to fetch tags"; exit 1; }
51-
52-
# Get the latest tag
53-
LATEST_TAG=$(git describe --tags `git rev-list --tags --max-count=1` 2>/dev/null || echo "")
54-
55-
# If no tags are found, start from 1.0.0
56-
if [ -z "$LATEST_TAG" ]; then
57-
echo "No tags found. Starting from 1.0.0"
58-
NEW_TAG="1.0.0"
59-
else
60-
echo "Latest tag found: $LATEST_TAG"
61-
# Parse the latest version
62-
VERSION_REGEX="^([0-9]+)\.([0-9]+)\.([0-9]+)$"
63-
if [[ $LATEST_TAG =~ $VERSION_REGEX ]]; then
64-
MAJOR="${BASH_REMATCH[1]}"
65-
MINOR="${BASH_REMATCH[2]}"
66-
PATCH="${BASH_REMATCH[3]}"
67-
68-
# Increment the minor version
69-
MINOR=$((MINOR + 1))
70-
71-
# Reset minor and increment major if minor reaches 100
72-
if [ $MINOR -eq 100 ]; then
73-
MINOR=0
74-
MAJOR=$((MAJOR + 1))
75-
fi
76-
77-
# Form the new version tag
78-
NEW_TAG="$MAJOR.$MINOR.$PATCH"
79-
else
80-
echo "Latest tag is not in the expected format: $LATEST_TAG"
81-
exit 1
82-
fi
83-
fi
84-
85-
echo "Creating new tag: $NEW_TAG"
86-
# Create and push the new tag
87-
git tag $NEW_TAG || { echo "Failed to create tag $NEW_TAG"; exit 1; }
88-
git push origin $NEW_TAG || { echo "Failed to push tag $NEW_TAG"; exit 1; }
89-
90-
# Set the new tag as an environment variable
91-
echo "NEW_TAG=$NEW_TAG" >> $GITHUB_ENV
92-
93-
9495
- name: Comment on the commit
9596
uses: actions/github-script@v3
9697
with:
@@ -106,4 +107,3 @@ jobs:
106107
body: `Deployment successful with release ${newTag}!`
107108
};
108109
await github.repos.createCommitComment(comment);
109-

build/build.mjs

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,72 @@
1-
import fs from 'fs';
2-
import { PurgeCSS } from 'purgecss';
3-
import postcss from 'postcss';
1+
import fs from 'fs-extra';
2+
import path from 'path';
43
import cssnano from 'cssnano';
4+
import PurgeCSS from 'purgecss';
55
import { minify } from 'html-minifier-terser';
66
import imagemin from 'imagemin';
77
import imageminMozjpeg from 'imagemin-mozjpeg';
88
import imageminPngquant from 'imagemin-pngquant';
99

10+
// Get the version number from environment variable set by the CI/CD pipeline
11+
const version = process.env.NEW_TAG || '1.0.0';
12+
13+
const srcHtmlPath = path.join(__dirname, '../src/index.html');
14+
const distHtmlPath = path.join(__dirname, '../dist/index.html');
15+
const srcCssPath = path.join(__dirname, '../src/styles.css');
16+
const distCssPath = path.join(__dirname, '../dist/styles.min.css');
17+
const srcImagePath = path.join(__dirname, '../src/logo.jpeg');
18+
const distImagePath = path.join(__dirname, '../dist/logo.jpeg');
19+
const srcCnamePath = path.join(__dirname, '../src/CNAME');
20+
const distCnamePath = path.join(__dirname, '../dist/CNAME');
21+
1022
async function buildCSS() {
1123
const purgeCSSResult = await new PurgeCSS().purge({
12-
content: ['src/index.html'],
13-
css: ['src/styles.css'],
24+
content: [srcHtmlPath],
25+
css: [srcCssPath]
1426
});
1527

16-
const result = await postcss([cssnano]).process(purgeCSSResult[0].css, { from: undefined });
17-
18-
fs.writeFileSync('dist/styles.min.css', result.css);
28+
const minifiedCSS = await cssnano.process(purgeCSSResult[0].css, { from: undefined });
29+
await fs.outputFile(distCssPath, minifiedCSS.css);
1930
}
2031

21-
async function minifyHTML() {
22-
const html = fs.readFileSync('src/index.html', 'utf8');
23-
const minifiedHTML = await minify(html, {
32+
async function buildHTML() {
33+
let htmlContent = await fs.readFile(srcHtmlPath, 'utf8');
34+
35+
// Replace the version placeholder with the actual version number
36+
htmlContent = htmlContent.replace('VERSION_PLACEHOLDER', version);
37+
38+
const minifiedHTML = await minify(htmlContent, {
2439
collapseWhitespace: true,
2540
removeComments: true,
2641
minifyCSS: true,
27-
minifyJS: true,
42+
minifyJS: true
2843
});
2944

30-
fs.writeFileSync('dist/index.html', minifiedHTML);
45+
await fs.outputFile(distHtmlPath, minifiedHTML);
3146
}
3247

3348
async function compressImages() {
34-
await imagemin(['src/logo.jpeg'], {
35-
destination: 'dist/',
49+
await imagemin([srcImagePath], {
50+
destination: path.dirname(distImagePath),
3651
plugins: [
37-
imageminMozjpeg({ quality: 75 }),
38-
imageminPngquant({ quality: [0.6, 0.8] })
52+
imageminMozjpeg(),
53+
imageminPngquant()
3954
]
4055
});
4156
}
4257

43-
function copyCNAME() {
44-
fs.copyFileSync('src/CNAME', 'dist/CNAME');
58+
async function copyCNAME() {
59+
await fs.copy(srcCnamePath, distCnamePath);
4560
}
4661

4762
async function build() {
48-
// Ensure the dist directory exists
49-
if (!fs.existsSync('dist')) {
50-
fs.mkdirSync('dist');
51-
}
52-
5363
await buildCSS();
54-
await minifyHTML();
64+
await buildHTML();
5565
await compressImages();
56-
copyCNAME();
57-
console.log('Build completed successfully');
66+
await copyCNAME();
5867
}
5968

60-
build();
69+
build().catch(err => {
70+
console.error(err);
71+
process.exit(1);
72+
});

src/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,14 @@ <h2>Libraries</h2>
260260
<footer>
261261
<div class="container">
262262
<span class="copyright-text">
263-
© <script>document.write(new Date().getFullYear())</script>
263+
© <script>document.write(new Date().getFullYear())</script> v<span id="version-placeholder">VERSION_PLACEHOLDER</span>
264264
</span>
265265
<span class="text-center">IpSimple</span>
266266
<div class="social-links">
267267
<a href="https://github.com/ipsimple" class="social-icon"><i class="fab fa-github"></i></a>
268268
</div>
269269
</div>
270270
</footer>
271+
271272
</body>
272273
</html>

0 commit comments

Comments
 (0)