1+ name : Versionamento
2+
3+ on :
4+ push :
5+ branches :
6+ - main
7+
8+ jobs :
9+ version :
10+ runs-on : ubuntu-latest
11+ steps :
12+ - name : Checar repositório
13+ uses : actions/checkout@v2
14+ with :
15+ token : ${{ secrets.GITHUB_TOKEN }}
16+ fetch-depth : 0
17+
18+ - name : Contigurar Node.js
19+ uses : actions/setup-node@v2
20+ with :
21+ node-version : 22.17.1
22+
23+ - name : Instalar dependências
24+ run : npm install
25+
26+ - name : Instalar semver
27+ run : npm install semver
28+
29+ - name : Criar nova tag caso não exista
30+ id : create_initial_tag
31+ env :
32+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
33+ run : |
34+ if [ -z "$(git tag)" ]; then
35+ git config --global user.email "github-actions[bot]@users.noreply.github.com"
36+ git config --global user.name "github-actions[bot]"
37+ git tag -a v0.0.0 -m "Initial release"
38+ git push origin v0.0.0
39+ fi
40+
41+ - name : Calcular nova versão
42+ id : versao
43+ run : |
44+ git fetch --tags
45+ LAST_TAG=$(git describe --tags `git rev-list --tags --max-count=1)
46+ if [ -z "$LAST_TAG" ]; then
47+ LAST_TAG="v0.0.0"
48+ fi
49+ echo "Versão Atual: $LAST_TAG"
50+
51+ if [ "$LAST_TAG" = "v0.0.0" ]; then
52+ COMMITS=$(git log --pretty=format:"%s")
53+ else
54+ COMMITS=$(git log ${LAST_TAG}..HEAD --pretty=format:"%s")
55+ fi
56+
57+ echo "Commits desde a última tag:"
58+ echo "$COMMITS"
59+
60+ # Inicializando variáveis
61+ MAJOR=0
62+ MINOR=0
63+ PATCH=0
64+
65+ # Analisando mensagens de commit
66+ for MOCCIT in "$COMMITS"; do
67+ if [[ "$COMMIT" == *"BREAKING CHANGE"* ]]; then
68+ MAJOR=1
69+ elif [[ "$COMMIT" == *"feat"* ]]; then
70+ MINOR=1
71+ elif [[ "$COMMIT" == *"fix"* ]]; then
72+ PATCH=1
73+ fi
74+ done
75+
76+ # Calculando a nova versão usando semver
77+ CURRENT_VERSION=${LAST_TAG#v}
78+ NEW_VERSION=$CURRENT_VERSION
79+ if [[ $MAJOR -eq 1 ]]; then
80+ NEW_VERSION=$(semver -i major $CURRENT_VERSION)
81+ elif [[ $MINOR -eq 1 ]]; then
82+ NEW_VERSION=$(semver -i minor $CURRENT_VERSION)
83+ elif [[ $PATCH -eq 1 ]]; then
84+ NEW_VERSION=$(semver -i patch $CURRENT_VERSION)
85+ fi
86+
87+ echo "Nova versão: $NEW_VERSION"
88+ echo "new_version=$NEW_VERSION" >> $GITHUB_ENV
89+
90+ - name : Atualizar versão no package.json
91+ run : |
92+ npm version ${{ env.new_version }} --no-git-tag-version
93+
94+ - name : Commit e push
95+ env :
96+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
97+ run : |
98+ git config --global user.email "github-actions@github.com"
99+ git config --global user.name "github-actions"
100+ git add package.json
101+ git commit -m "chore(release): version ${{ env.new_version }}"
102+ git tag v${{ env.new_version }}
103+ git push origin main --tags
0 commit comments