diff --git a/.forgejo/workflows/pipeline.yml b/.forgejo/workflows/pipeline.yml new file mode 100644 index 0000000..c8cdf0a --- /dev/null +++ b/.forgejo/workflows/pipeline.yml @@ -0,0 +1,74 @@ +name: CI/CD Pipeline +on: + push: + branches: + - main +env: + FORGEJO_BASE_URL: ${{ vars.FORGEJO_BASE_URL }} +jobs: + build-and-deploy: + runs-on: ubuntu latest + steps: + - name: Checkout Code + uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: '1.21' + - name: Build Go Binary + run: go build -o loic + - name: Check if Package Exists + id: check_package_exists + uses: actions/http@v1 + with: + url: ${{ env.FORGEJO_BASE_URL }}/api/v1/repos/{{ .Identity }}/loic-go/packs/generic/loic-binary + method: GET + headers: | + Authorization: token ${{ secrets.FORGEJO_API_TOKEN }} + - name: Create Package if it Doesn't Exist + if: steps.check_package_exists.outcome == 'failure' + uses: actions/http@v1 + with: + url: ${{ env.FORGEJO_BASE_URL }}/api/v1/repos/{{ .Identity }}/loic-go/packs/generic + method: POST + body: '{ "name": "loic-binary", "description": "Loic Go Binary" }' + headers: | + Authorization: token ${{ secrets.FORGEJO_API_TOKEN }} + - name: Create New Version + id: create_version + uses: actions/http@v1 + with: + url: ${{ env.FORGEJO_BASE_URL }}/api/v1/repos/{{ .Identity }}/loic-go/packs/generic/loic-binary/versions + method: POST + body: '{ "name": "${{ github.sha }}"}' + headers: | + Authorization: token ${{ secrets.FORGEJO_API_TOKEN }} + - name: Get Version ID + id: get_version_id + run: | + echo "version_id=$(jq -r '.id' <<< "${{ steps.create_version.body }}")" >> $GITHUB_OUTPUT + - name: Upload Asset + uses: actions/http@v1 + with: + url: ${{ env.FORGEJO_BASE_URL }}/api/v1/repos/{{ .Identity }}/loic-go/packs/generic/loic-binary/versions/${{ steps.get_version_id.outputs.version_id }}/assets + method: POST + body: ./loic + headers: | + Authorization: token ${{ secrets.FORGEJO_API_TOKEN }} + Content-Type: application/octet-stream + - name: Build and Push Docker Image + run: | + docker build -t loic:${{ github.sha }} . + echo "$FORGEJO_API_TOKEN" | docker login -u {{ .Identity }} --password-stdin ${{ env.FORGEJO_BASE_URL }} + docker tag loic:${{ github.sha }} ${{ env.FORGEJO_BASE_URL }}/{{ .Identity }}/loic:${{ github.sha }} + docker push ${{ env.FORGEJO_BASE_URL }}/{{ .Identity }}/loic:${{ github.sha }} + - name: Update Deployment Manifest + run: | + sed -i "s|image: .*|image: ${{ env.FORGEJO_BASE_URL }}/{{ .Identity }}/loic:${{ github.sha }}|" k8s/deployment.yaml + git config --global user.email "ci@edp.local" + git config --global user.name "CI Bot" + git add k8s/deployment.yaml + git commit -m "Update image to ${{ github.sha }}" + git push + env: + FORGEJO_API_TOKEN: ${{ secrets.FORGEJO_API_TOKEN }}