Jenkins pipeline中读写文件

文章详细描述了一个使用pipeline进行文件读写操作的示例,特别强调了如何在Nginx.conf中正确处理$uri以避免其被识别为变量,同时展示了gzip配置选项。

下面是一个读写文件的示例,并且保证了nginx.conf中的$uri不被识别为变量

pipeline {
    agent any
	stages {
		stage('Write and Read File') {
			steps {
                script {
                    echo "Build Stage"
                    def content = """
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #gzip
    #开启gzip功能
    gzip on; 
    #开启gzip静态压缩功能
    gzip_static on; 
    #gzip缓存大小
    gzip_buffers 4 16k;
    #gzip http版本
    gzip_http_version 1.1;
    #gzip 压缩级别 1-10 
    gzip_comp_level 5;
    #gzip 压缩类型
    gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; 
    gzip_vary on; 
    location / {
        alias   /usr/share/nginx/html/;
        index  index.html index.htm;
        try_files \$uri \$uri/ /index.html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
                    """
                    writeFile(
                        file: 'default.conf',
                        text: content
                    )
                    def fileContent = readFile(file: 'default.conf')
                    echo "${fileContent}"
                }
            }
        }
	}

}

例2(避免缩进错误):

stage('Build and Push image') {
	steps {
		dir("${service_name}"){
			script{
				def composefileContent = """
				version: '3'
				services:
					${service_name}:
						image: 192.100.30.160:9000/python/${service_name}:latest
						container_name: ${service_name}
						restart: always
						ports:
							- ${service_port}:${service_port}
						volumes:
							- ${publish_path}/code:/app
						entrypoint: [${commandParts.split(' ').collect { "\"$it\"" }.join(', ')}]
				""".stripIndent()
				writeFile(file: 'docker-compose.yml', text: composefileContent)
				println composefileContent
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值