Last active
October 14, 2020 23:37
-
-
Save marlluslustosa/f2818a483765b1700c50b9d0a2faaf04 to your computer and use it in GitHub Desktop.
DevOps Workflow - build/push
This file contains hidden or 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: Workflow - sendgrid pull push | |
on: | |
push: | |
branches: | |
- master | |
pull_request: | |
env: | |
# Variavel de ambiente vista para qualquer job | |
# nome da imagem - altere para o nome correto | |
IMAGE_NAME: nome-imagem | |
jobs: | |
# Teste de build | |
# Ver mais https://docs.docker.com/docker-hub/builds/automated-testing/ | |
test: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Executando testes | |
# Testa se o arquivo 'builda' | |
run: | | |
if [ -f docker-compose.test.yml ]; then | |
docker-compose --file docker-compose.test.yml build | |
docker-compose --file docker-compose.test.yml run sut | |
else | |
docker build . --file Dockerfile | |
fi | |
# Push da imagem para o registry do github | |
push: | |
# Condicao para somente fazer o push se o job test executou com sucesso | |
needs: test | |
runs-on: ubuntu-18.04 | |
# Só executa se o evento atual for push (e não um PR) | |
if: github.event_name == 'push' | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Build image | |
# chamada da variavel IMAGE_NAME definida em env | |
run: docker build . --file Dockerfile --tag $IMAGE_NAME | |
- name: Login no GitHub Container Registry | |
# Personal Access Token (PAT) criado e adicionado no Actions Secrets. | |
run: echo "${{ secrets.CR_PAT }}" | docker login https://ghcr.io -u ${{ github.actor }} --password-stdin | |
- name: Push da imagem para GitHub Container Registry | |
run: | | |
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME | |
# Transformar nome da imagem de maiúsculas para minúsculas | |
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') | |
# Retira o prefixo git ref da versão | |
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') | |
# Retirar prefixo v da tag | |
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') | |
# Use tag latest como convenção para docker:latest | |
[ "$VERSION" == "master" ] && VERSION=latest | |
echo IMAGE_ID=$IMAGE_ID | |
echo VERSION=$VERSION | |
# comandos finais para push | |
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION | |
docker push $IMAGE_ID:$VERSION | |
email: | |
# Condicao para somente fazer o push se o anterior executou com sucesso | |
needs: push | |
runs-on: ubuntu-18.04 | |
steps: | |
- name: SendGrid Action | |
uses: mmichailidis/[email protected] | |
with: | |
sendgrid-token: ${{ secrets.SENDGRID_API_KEY }} | |
mail: [email protected],[email protected] #email de destino | |
#from definido no sendgrid - https://sendgrid.com/docs/API_Reference/SMTP_API/integrating_with_the_smtp_api.html | |
from: ${{ secrets.SENDGRID_MAILFROM }} | |
subject: Nova imagem no registry | |
text: Uma nova imagem foi enviada para o registry. Aguarde a notificação, do serviço Watch Tower, da implantação automática na produção! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment