Correção de release automático #23
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Auto Release on Commit | |
permissions: | |
contents: write # Permissões para acessar e modificar conteúdo do repositório | |
actions: read # Permissões para executar ações | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- 'lambda_functions/**' # Só dispara se houver mudanças nessa pasta | |
jobs: | |
create-release: | |
name: Create Release | |
runs-on: ubuntu-latest | |
steps: | |
# Checkout do código do repositório | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 5 | |
# Verifica se houve mudanças dentro de lambda_functions | |
- name: Check changes in lambda_functions | |
id: changes | |
run: | | |
if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ] && [ "$(git rev-list --count HEAD)" -gt 1 ]; then | |
if git diff --name-only HEAD~1 HEAD | grep '^lambda_functions/'; then | |
echo "has_changes=true" >> $GITHUB_ENV | |
else | |
echo "has_changes=false" >> $GITHUB_ENV | |
fi | |
else | |
echo "has_changes=false" >> $GITHUB_ENV | |
fi | |
# Condicional: Segue o fluxo apenas se houver mudanças na pasta | |
- name: Exit if no changes in lambda_functions | |
if: ${{ env.has_changes == 'false' }} | |
run: echo "No changes detected, exiting pipeline" && exit 0 | |
# Instala as dependências da pasta lambda_functions | |
- name: Install dependencies | |
working-directory: ./lambda_functions | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt -t . | |
# Define a versão para o release | |
- name: Set release version | |
id: get_version | |
run: | | |
latest_tag=$(git tag -l "lambda_functions_v*" --sort=-v:refname | head -n 1) | |
if [ -z "$latest_tag" ]; then | |
latest_tag="lambda_functions_v0.1" | |
fi | |
version=$(echo "$latest_tag" | sed 's/lambda_functions_v//') | |
IFS='.' read -r major minor <<< "$version" | |
minor=$((minor + 1)) | |
new_version="lambda_functions_v${major}.${minor}" | |
echo "New version is $new_version" | |
echo "new_version=$new_version" >> $GITHUB_ENV | |
# Cria um arquivo ZIP com os artefatos da pasta lambda_functions (se houver mudanças) | |
- name: Zip lambda_functions folder | |
if: ${{ env.has_changes == 'true' }} | |
run: | | |
cd lambda_functions | |
zip -r ../${{ env.new_version }}.zip . | |
# Verifica se a tag já existe | |
- name: Check if tag exists | |
id: tag_exists | |
run: | | |
if git rev-parse "refs/tags/${{ env.new_version }}" >/dev/null 2>&1; then | |
echo "Tag exists" | |
echo "exists=true" >> $GITHUB_ENV | |
else | |
echo "Tag does not exist" | |
echo "exists=false" >> $GITHUB_ENV | |
fi | |
# Cria o release no GitHub (se não existir uma com mesmo nome) | |
- name: Create Release | |
id: create_release | |
if: ${{ env.has_changes == 'true' && env.exists == 'false' }} | |
uses: actions/create-release@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
tag_name: ${{ env.new_version }} | |
release_name: ${{ env.new_version }} | |
body_path: ./release_notes.md | |
draft: false | |
prerelease: false | |
# Sobe o pacote de artefatos no release (se houver mudanças) | |
- name: Upload Release Asset | |
if: ${{ env.has_changes == 'true' }} | |
uses: actions/upload-release-asset@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
upload_url: ${{ steps.create_release.outputs.upload_url }} | |
asset_path: ${{ env.new_version }}.zip | |
asset_name: ${{ env.new_version }}.zip | |
asset_content_type: application/zip | |
# Remove o arquivo .zip local após o release (se for criado) | |
- name: Clean up | |
if: ${{ env.has_changes == 'true' }} | |
run: rm ${{ env.new_version }}.zip |