Created
October 13, 2020 04:36
-
-
Save marlluslustosa/5efa141603c11a4519bbff8f1a743d2b to your computer and use it in GitHub Desktop.
workflow - build/push
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: Workflow - build/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 | |
# procura vulnerabilidades na imagem gerada no passo anterior | |
scan: | |
# somente faz scan se o job anterior concluiu com êxito | |
needs: push | |
name: Vulnerability Scan | |
runs-on: ubuntu-18.04 | |
steps: | |
- name: Pull docker image | |
id: pullimage | |
run: | | |
docker pull ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME | |
# setar variavel local para repassar ao trivy | |
echo ::set-output name=nome_imagem::$IMAGE_NAME | |
# cria issue no repositório, se encontrar vulnerabilidades | |
- uses: homoluctus/[email protected] | |
id: trivy | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
# imagem que foi 'pusheada' no job anterior | |
image: ghcr.io/${{ github.repository_owner }}/${{ steps.pullimage.outputs.nome_imagem }} | |
issue_title: Alerta de segurança | |
# comenta issue, citando reponsavel para resolucao | |
- name: responder issue | |
env: | |
REPO: ${{ github.event.repository.name }} | |
OWNER: ${{ github.event.repository.owner.login }} | |
ISSUE_NUMBER: ${{ steps.trivy.outputs.issue_number }} | |
run: | | |
# se variavel de retorno html_url (do step trivy) contiver algo, isso quer dizer que foi criada uma issue, entao, comente-a. | |
if [ ! -z "${{ steps.trivy.outputs.html_url }}" ]; then | |
curl -s -X POST https://api.github.com/repos/$OWNER/$REPO/issues/$ISSUE_NUMBER/comments -d '{"body":"Ei, @marlluslustosa, pode dar uma olhada nessa issue?"}' -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment