diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..959c61b --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,44 @@ +name: Docker + +on: + workflow_call: + +jobs: + + docker: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: setup Docker Build + uses: docker/setup-buildx-action@v2.0.0 + + - name: Download a Build Artifact + uses: actions/download-artifact@v2.1.1 + with: + # Artifact name + name: programa + + - name: Docker Login + # You may pin to the exact commit or the version. + # uses: docker/login-action@49ed152c8eca782a232dede0303416e8f356c37b + uses: docker/login-action@v2.0.0 + with: + # Username used to log against the Docker registry + username: luc4592 + # Password or personal access token used to log against the Docker registry + password: ${{ secrets.PASSWD }} + + - name: Build and push Docker images + # You may pin to the exact commit or the version. + # uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 + uses: docker/build-push-action@v4.0.0 + with: + # Build's context is the set of files located in the specified PATH or URL + context: . + # Path to the Dockerfile + file: ./Dockerfile + # Push is a shorthand for --output=type=registry + push: true + # List of tags + tags: luc4592/go_ci:${{ github.ref_name }} diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5b4c352..c7d99cc 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -2,17 +2,24 @@ name: Go on: push: - branches: [ Aula_1 ] + branches: [ '*' ] pull_request: - branches: [ Aula_1 ] + branches: [ '*' ] jobs: test: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + env: + HOST: localhost + PORT: 5432 + USER: root + PASSWORD: root + DBNAME: root strategy: matrix: go_version: ['1.18', '1.17', '>=1.18'] + os: ['ubuntu-latest', 'ubuntu-20.04'] steps: - uses: actions/checkout@v3 @@ -30,14 +37,36 @@ jobs: - name: Test run: go test -v main_test.go + - name: Snyk + # You may pin to the exact commit or the version. + # uses: snyk/actions@b98d498629f1c368650224d6d212bf7dfa89e4bf + uses: snyk/actions@0.4.0 + with: + # Which Snyk command to run, defaults to test + command: # Additional arguments to pass to Snyk + #args: # optional + # Output a snyk.json file with results if running the test command + #json: # optional + build: needs: test - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: ['ubuntu-latest', 'ubuntu-18.04'] + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Build run: go build -v main.go + + + - name: Upload a Build Artifact + uses: actions/upload-artifact@v3.1.2 + with: + # Artifact name + name: programa + # A file, directory or wildcard pattern that describes what to upload + path: main + + docker: + needs: build + uses: ./.github/workflows/docker.yml + secrets: inherit diff --git a/.github/workflows/snyk.yml b/.github/workflows/snyk.yml new file mode 100644 index 0000000..f97728c --- /dev/null +++ b/.github/workflows/snyk.yml @@ -0,0 +1,12 @@ +name: Snyk +on: push +jobs: + security: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + - name: RunSnyk to check for vulnerabilities + uses: snyk/actions/maven@master + continue-on-error: true + env: + SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1ed234b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM ubuntu:latest + +EXPOSE 8000 + +WORKDIR /app + +ENV HOST=localhost PORT=5432 + +ENV USER=root PASSWORD=root DBNAME=root + +COPY ./main.exe main + +CMD [ "./main" ] diff --git a/database/db.go b/database/db.go index 01b3d50..b5e279b 100644 --- a/database/db.go +++ b/database/db.go @@ -2,7 +2,7 @@ package database import ( "log" - + "os" "github.com/guilhermeonrails/api-go-gin/models" "gorm.io/driver/postgres" "gorm.io/gorm" @@ -14,7 +14,8 @@ var ( ) func ConectaComBancoDeDados() { - stringDeConexao := "host=localhost user=root password=root dbname=root port=5432 sslmode=disable" + + stringDeConexao := "host="+os.Getenv("HOST")+" user="+os.Getenv("USER")+" password="+os.Getenv("PASSWORD")+" dbname="+os.Getenv("DBNAME")+" port="+os.Getenv("PORT")+" sslmode=disable" DB, err = gorm.Open(postgres.Open(stringDeConexao)) if err != nil { log.Panic("Erro ao conectar com banco de dados")