From d54fc311cfb108977bab97ee6d1ff4cb67070d4d Mon Sep 17 00:00:00 2001 From: danperezsan Date: Thu, 6 Jun 2024 00:33:35 +0200 Subject: [PATCH 1/2] Adds github actions to build and push images based on branches and tags --- .github/workflows/build_main.yml | 34 +++++++++++++++++++++++++ .github/workflows/build_prod.yml | 34 +++++++++++++++++++++++++ .github/workflows/build_tag.yml | 43 ++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 .github/workflows/build_main.yml create mode 100644 .github/workflows/build_prod.yml create mode 100644 .github/workflows/build_tag.yml diff --git a/.github/workflows/build_main.yml b/.github/workflows/build_main.yml new file mode 100644 index 0000000..7f88843 --- /dev/null +++ b/.github/workflows/build_main.yml @@ -0,0 +1,34 @@ +name: Build and Push Dev Image + +on: + push: + branches: + - 'main' + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract branch name + id: extract_branch + run: echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV + + - name: Build and push Docker images + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: mesoform/postgres-ha:latest \ No newline at end of file diff --git a/.github/workflows/build_prod.yml b/.github/workflows/build_prod.yml new file mode 100644 index 0000000..bfd2449 --- /dev/null +++ b/.github/workflows/build_prod.yml @@ -0,0 +1,34 @@ +name: Build and Push Dev Image + +on: + push: + branches: + - 'pg-v*' + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract branch name + id: extract_branch + run: echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV + + - name: Build and push Docker images + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: mesoform/postgres-ha:${{ env.BRANCH_NAME }} \ No newline at end of file diff --git a/.github/workflows/build_tag.yml b/.github/workflows/build_tag.yml new file mode 100644 index 0000000..64fb1f9 --- /dev/null +++ b/.github/workflows/build_tag.yml @@ -0,0 +1,43 @@ +name: Build and Push Release Docker Images + +on: + push: + tags: + - 'v*' + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract version parts + id: extract_version + run: | + VERSION=${GITHUB_REF#refs/tags/v} + MAJOR_MINOR=$(echo $VERSION | awk -F '-' '{print $1}') + MAJOR=$(echo $MAJOR_MINOR | awk -F '.' '{print $1}') + echo "VERSION=$VERSION" >> $GITHUB_ENV + echo "MAJOR_MINOR=$MAJOR_MINOR" >> $GITHUB_ENV + echo "MAJOR=$MAJOR" >> $GITHUB_ENV + + - name: Build and push Docker images + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: | + my-dockerhub-username/my-image:release-${{ env.VERSION }} + my-dockerhub-username/my-image:release-${{ env.MAJOR_MINOR }} + my-dockerhub-username/my-image:${{ env.MAJOR }}-latest From 238cca4a89343132e1ae3f0a501cc014f2cd39e1 Mon Sep 17 00:00:00 2001 From: danperezsan Date: Thu, 6 Jun 2024 00:53:02 +0200 Subject: [PATCH 2/2] Adds github actions to build and push images based on branches and tags --- .github/workflows/build_dev.yml | 2 +- .github/workflows/build_main.yml | 15 ++++++--------- .github/workflows/build_prod.yml | 15 ++++++++------- .github/workflows/build_tag.yml | 6 +++--- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/.github/workflows/build_dev.yml b/.github/workflows/build_dev.yml index 2d1d986..ee6d2c2 100644 --- a/.github/workflows/build_dev.yml +++ b/.github/workflows/build_dev.yml @@ -31,4 +31,4 @@ jobs: with: context: . push: true - tags: mesoform/postgres-ha:${{ env.BRANCH_NAME }} \ No newline at end of file + tags: mesoform/postgres-ha:${{ env.BRANCH_NAME }} diff --git a/.github/workflows/build_main.yml b/.github/workflows/build_main.yml index 7f88843..ddc4593 100644 --- a/.github/workflows/build_main.yml +++ b/.github/workflows/build_main.yml @@ -1,12 +1,13 @@ -name: Build and Push Dev Image +name: Build and Push Docker Image on Merged Pull Request on: - push: - branches: - - 'main' + pull_request: + types: [closed] jobs: build: + if: ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main' && startsWith(github.event.pull_request.head.ref, 'pg-v') }} + runs-on: ubuntu-latest steps: @@ -22,13 +23,9 @@ jobs: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - - name: Extract branch name - id: extract_branch - run: echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV - - name: Build and push Docker images uses: docker/build-push-action@v5 with: context: . push: true - tags: mesoform/postgres-ha:latest \ No newline at end of file + tags: mesoform/postgres-ha:latest diff --git a/.github/workflows/build_prod.yml b/.github/workflows/build_prod.yml index bfd2449..cb51f64 100644 --- a/.github/workflows/build_prod.yml +++ b/.github/workflows/build_prod.yml @@ -1,12 +1,13 @@ -name: Build and Push Dev Image +name: Build and Push Docker Image on Merged Pull Request on: - push: - branches: - - 'pg-v*' + pull_request: + types: [closed] jobs: build: + if: ${{ github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'dev-') && startsWith(github.event.pull_request.base.ref, 'pg-v') }} + runs-on: ubuntu-latest steps: @@ -22,13 +23,13 @@ jobs: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - - name: Extract branch name + - name: Extract target branch name id: extract_branch - run: echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV + run: echo "TARGET_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV - name: Build and push Docker images uses: docker/build-push-action@v5 with: context: . push: true - tags: mesoform/postgres-ha:${{ env.BRANCH_NAME }} \ No newline at end of file + tags: mesoform/postgres-ha:${{ env.TARGET_BRANCH }} diff --git a/.github/workflows/build_tag.yml b/.github/workflows/build_tag.yml index 64fb1f9..b4d20e9 100644 --- a/.github/workflows/build_tag.yml +++ b/.github/workflows/build_tag.yml @@ -38,6 +38,6 @@ jobs: context: . push: true tags: | - my-dockerhub-username/my-image:release-${{ env.VERSION }} - my-dockerhub-username/my-image:release-${{ env.MAJOR_MINOR }} - my-dockerhub-username/my-image:${{ env.MAJOR }}-latest + mesoform/postgres-ha:release-${{ env.VERSION }} + mesoform/postgres-ha:release-${{ env.MAJOR_MINOR }} + mesoform/postgres-ha:${{ env.MAJOR }}-latest