optimize shell script #2154
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: CI | |
on: | |
pull_request: | |
types: [opened, synchronize, reopened] | |
push: | |
branches: | |
- '*' | |
tags: | |
- '*' | |
env: | |
GOPROXY: https://proxy.golang.org | |
GO111MODULE: on | |
tf_version: "1.3.0" | |
tf_init_cli_options: "-input=false" | |
tf_validation_cli_options: "" | |
tf_plan_cli_options: "-lock=false -input=false" | |
tf_apply_cli_options: "-auto-approve -input=false" | |
jobs: | |
tests: | |
env: | |
GOPATH: ${{ github.workspace }} | |
GOBIN: ${{ github.workspace }}/bin | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: ${{ env.GOPATH }}/src/XDPoSChain | |
strategy: | |
matrix: | |
include: | |
- name: A-B tests | |
script: go run build/ci.go test -coverage $(go list ./... | grep "github.com/XinFinOrg/XDPoSChain/[a-b].*") | |
- name: C-[a-m] tests | |
script: go run build/ci.go test -coverage $(go list ./... | grep "github.com/XinFinOrg/XDPoSChain/c[a-m].*") | |
- name: C-[n-o] tests | |
script: go run build/ci.go test -coverage $(go list ./... | grep "github.com/XinFinOrg/XDPoSChain/c[n-o].*") | |
- name: C-[p-z] tests | |
script: go run build/ci.go test -coverage $(go list ./... | grep "github.com/XinFinOrg/XDPoSChain/c[p-z].*") | |
- name: D-I tests | |
script: go run build/ci.go test -coverage $(go list ./... | grep "github.com/XinFinOrg/XDPoSChain/[d-i].*") | |
- name: J-N tests | |
script: go run build/ci.go test -coverage $(go list ./... | grep "github.com/XinFinOrg/XDPoSChain/[j-n].*") | |
- name: O-R tests | |
script: go run build/ci.go test -coverage $(go list ./... | grep "github.com/XinFinOrg/XDPoSChain/[o-r].*") | |
- name: S tests | |
script: go run build/ci.go test -coverage $(go list ./... | grep "github.com/XinFinOrg/XDPoSChain/s.*") | |
- name: T-Z tests | |
script: go run build/ci.go test -coverage $(go list ./... | grep "github.com/XinFinOrg/XDPoSChain/[t-z].*") | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v4 | |
with: | |
path: ${{ env.GOPATH }}/src/XDPoSChain | |
- name: Set up Go | |
uses: actions/setup-go@v5 | |
with: | |
cache: false | |
go-version: '1.21.x' | |
- name: Run tests | |
run: ${{ matrix.script }} | |
env: | |
GO111MODULE: auto | |
tag_build: | |
runs-on: ubuntu-latest | |
needs: tests | |
if: startsWith(github.ref, 'refs/tags/') | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Login to Docker Hub | |
run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin | |
- name: Build Docker image | |
run: docker build -t xinfinorg/xdposchain:${GITHUB_REF#refs/tags/} -f cicd/Dockerfile . | |
- name: Push Docker image | |
run: docker push xinfinorg/xdposchain:${GITHUB_REF#refs/tags/} | |
devnet_build_push: | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/dev-upgrade' && !startsWith(github.ref, 'refs/tags/') | |
needs: tests | |
outputs: | |
output1: ${{ steps.docker.outputs.image_name }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Login to Docker Hub | |
run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin | |
- name: Build and Push Docker images | |
id: docker | |
run: | | |
git_hash=$(git rev-parse --short "$GITHUB_SHA") | |
image_name=xinfinorg/devnet:dev-upgrade-${git_hash} | |
docker pull xinfinorg/devnet:latest | |
docker tag xinfinorg/devnet:latest xinfinorg/devnet:previous | |
docker rmi xinfinorg/devnet:latest | |
docker build -t xinfinorg/devnet:latest -f cicd/Dockerfile . | |
docker tag xinfinorg/devnet:latest $image_name | |
docker push $image_name | |
docker push xinfinorg/devnet:latest | |
docker push xinfinorg/devnet:previous | |
echo "image_name=$image_name" | |
echo "image_name=$image_name" >> "$GITHUB_OUTPUT" | |
devnet_terraform_apply: | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/dev-upgrade' && !startsWith(github.ref, 'refs/tags/') | |
needs: devnet_build_push | |
environment: devnet | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Terraform Apply | |
run: | | |
git_hash=$(git rev-parse --short "$GITHUB_SHA") | |
cd cicd/devnet/terraform | |
terraform init ${{ env.tf_init_cli_options }} | |
terraform apply -var "docker_tag=dev-upgrade-${git_hash}" ${{ env.tf_apply_cli_options }} | |
devnet_k8_apply: | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/dev-upgrade' && !startsWith(github.ref, 'refs/tags/') | |
needs: devnet_build_push | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up kubectl | |
uses: azure/setup-kubectl@v4 | |
with: | |
version: 'v1.19.11' | |
- name: Decode and configure kubeconfig | |
run: | | |
mkdir -p $HOME/.kube | |
echo "${{ secrets.KUBE_CONFIG }}" | base64 --decode > $HOME/.kube/config | |
chmod 600 $HOME/.kube/config | |
git_hash=$(git rev-parse --short "$GITHUB_SHA") | |
kubectl set image deployment/devnet1 devnet1=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet2 devnet2=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet3 devnet3=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet4 devnet4=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet5 devnet5=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet6 devnet6=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet7 devnet7=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet8 devnet8=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet9 devnet9=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet10 devnet10=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet11 devnet11=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet12 devnet12=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet13 devnet13=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet14 devnet14=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet15 devnet15=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet16 devnet16=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet17 devnet17=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/devnet18 devnet18=xinfinorg/devnet:dev-upgrade-${git_hash} | |
rpc_k8_apply: | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/dev-upgrade' && !startsWith(github.ref, 'refs/tags/') | |
needs: devnet_build_push | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up kubectl | |
uses: azure/setup-kubectl@v4 | |
with: | |
version: 'v1.19.11' | |
- name: Decode and configure kubeconfig | |
run: | | |
mkdir -p $HOME/.kube | |
echo "${{ secrets.KUBE_CONFIG }}" | base64 --decode > $HOME/.kube/config | |
chmod 600 $HOME/.kube/config | |
git_hash=$(git rev-parse --short "$GITHUB_SHA") | |
kubectl set image deployment/devnetrpc devnetrpc=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/testnetrpc testnetrpc=xinfinorg/devnet:dev-upgrade-${git_hash} | |
kubectl set image deployment/mainnetrpc mainnetrpc=xinfinorg/devnet:dev-upgrade-${git_hash} | |
rpcnode_terraform_apply: | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/dev-upgrade' && !startsWith(github.ref, 'refs/tags/') | |
needs: devnet_build_push | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Terraform Apply | |
run: | | |
cd cicd/terraform | |
terraform init ${{ env.tf_init_cli_options }} | |
terraform apply ${{ env.tf_apply_cli_options }} | |
- name: Update RPC nodes image | |
uses: dawidd6/action-ansible-playbook@v2 | |
with: | |
playbook: playbooks/update-image.yaml | |
directory: ./cicd/ansible | |
key: ${{secrets.SSH_PRIVATE_KEY_DEVNET}} | |
options: | | |
--inventory inventory.yaml | |
--extra-vars network=ec2_rpcs | |
--extra-vars rpc_image=${{ needs.devnet_build_push.outputs.output1 }} | |
devnet_dev-upgrade_node: | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/dev-upgrade' && !startsWith(github.ref, 'refs/tags/') | |
needs: rpcnode_terraform_apply | |
environment: devnet | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: ECS Update | |
run: | | |
aws ecs update-service --region ap-southeast-1 --cluster devnet-xdcnode-cluster --service ecs-service-rpc1 --force-new-deployment --no-cli-pager | head -n 10; | |
testnet_dev-upgrade_node: | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/dev-upgrade' && !startsWith(github.ref, 'refs/tags/') | |
needs: rpcnode_terraform_apply | |
environment: testnet | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: ECS Update | |
run: | | |
aws ecs update-service --region ap-southeast-1 --cluster testnet-xdcnode-cluster --service ecs-service-testnet-rpc1 --force-new-deployment --no-cli-pager | head -n 10; | |
mainnet_dev-upgrade_node: | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/dev-upgrade' && !startsWith(github.ref, 'refs/tags/') | |
needs: rpcnode_terraform_apply | |
environment: mainnet | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: ECS Update | |
run: | | |
aws ecs update-service --region ap-southeast-1 --cluster mainnet-xdcnode-cluster --service ecs-service-mainnet-rpc1 --force-new-deployment --no-cli-pager | head -n 10; | |
devnet_send_notification: | |
runs-on: ubuntu-latest | |
needs: devnet_terraform_apply | |
if: github.ref == 'refs/heads/dev-upgrade' && !startsWith(github.ref, 'refs/tags/') | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Send deployment notification | |
run: | | |
curl --location --request POST "66.94.98.186:8080/deploy?environment=devnet&service=xdc&version=${GITHUB_SHA}" |